【问题标题】:mvn deploy in Github actions gives 401 UnauthorizedGithub 操作中的 mvn deploy 给出 401 Unauthorized
【发布时间】:2022-03-23 16:50:50
【问题描述】:

我曾多次尝试将工件从 .github/workflows 部署到 https://maven.pkg.github.com,但都导致 401 Unauthorized 错误。

      - name: Setup java for mvn deploy
        uses: actions/setup-java@v1
        with:
          java-version: 8

      - name: Deploy kaldi-linux.zip
        working-directory: kaldi
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: |
          cp ../.github/kaldi/* .
          perl -pi -e 's/^(\s{4}<version>).*(<\/version>)/${1}$ENV{"KALDI_VERSION"}${2}/g' pom.xml
          mvn deploy
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy)
 on project kaldi: Failed to deploy artifacts: Could not transfer artifact org.kaldi:kaldi:pom:da93074
 from/to temp (https://maven.pkg.github.com/nalbion/vosk-api): Transfer failed for 
https://maven.pkg.github.com/nalbion/vosk-api/org/kaldi/kaldi/da93074/kaldi-da93074.pom 401 Unauthorized -> [Help 1]

在此处记录:https://github.com/nalbion/vosk-api/runs/683615393?check_suite_focus=true

我的理解是actions/setup-java 应该提供settings.xml 和环境变量,并且部署应该是直截了当的。有没有我没有做的事情需要做?

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    我可以通过第一手经验告诉你,将包部署到 github 的 maven repos 并不容易。忘记包部分中显示的“简单”步骤,该过程比这更复杂。不过还是可以的。

    对于初学者,您将需要一个 settings.xml 文件。这是我的最小设置:

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
    
      <servers>
        <server>
          <id>github</id>
          <configuration>
            <httpHeaders>
              <property>
                <name>Authorization</name>
                <value>Bearer <GITHUB_TOKEN_GOES HERE></value>
              </property>
            </httpHeaders>
          </configuration>
        </server>
      </servers>
    </settings>
    

    我猜java 操作会覆盖settings.xml,但以上是必需的。另一种选择是使用用户名/密码的方法,但这种方法很快就过时了,取而代之的是不记名令牌,就像我上面所做的那样。

    现在要使用源部署快照版本,这是我必须使用的命令:

    mvn clean source:jar deploy -DuniqueVersion=false -Dmaven.source.useDefaultManifestFile=true -Dmaven.source.includePom=true -Dmaven.install.skip=true -DdeplyAtEnd=true -DaltDeploymentRepository='github::default::https://maven.pkg.github.com/OWNER/REPOSITORY'
    

    根据publishing guide,假设您的pom.xml 中有以下内容:

    <distributionManagement>
       <repository>
         <id>github</id>
         <name>GitHub OWNER Apache Maven Packages</name>
         <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
       </repository>
    </distributionManagement>
    

    您可以将命令更改为:

    mvn clean source:jar deploy -DuniqueVersion=false -Dmaven.source.useDefaultManifestFile=true -DdeplyAtEnd=true -Dmaven.source.includePom=true -Dmaven.install.skip=true
    

    【讨论】:

    【解决方案2】:

    如果有人使用 maven-settings-action (https://github.com/s4u/maven-settings-action):

    此操作还会覆盖 settings.xml,因此需要以下配置:

    - uses: s4u/maven-settings-action@v2.5.0
            with:
              servers: '[
              {"id": "github","configuration": {"httpHeaders": {"property": {"name": "Authorization","value": "Bearer ${{ secrets.GITHUB_TOKEN }}"}}}}]'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2018-03-24
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 2020-03-05
      相关资源
      最近更新 更多