从Github Action部署到Github Package Registry

问题描述:

我想从公共仓库的GitHub Action部署到GitHub Package Registry.

I would like to deploy to a GitHub Package Registry from a GitHub Action of a public repo.

我有一个用于工作流程的yml文件:

I have a yml file for a workflow:

name: My CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Install dependencies
      run: lein deps
    - name: Run tests
      run: lein test
    - name: Generate pom
      run: lein pom
    - name: Deploy
      run: mvn deploy

我使用Leiningen构建项目并生成POM文件.然后,我想使用Maven将工件部署到GitHub Package Registry.

I use Leiningen to build the project and generate a POM file. Then I would like to use Maven to deploy the artifact to the GitHub Package Registry.

这在Deploy命令上失败(我已经用...替换了个人信息):

This fails on the Deploy command (I have replaced personal information with ...):

[WARNING] Could not transfer metadata ... from/to github (https://maven.pkg.github.com/.../...): Not authorized
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  19.343 s
[INFO] Finished at: 2019-08-29T13:08:42Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project ...: Failed to retrieve remote metadata .../maven-metadata.xml: Could not transfer metadata ... from/to github (https://maven.pkg.github.com/.../...): Not authorized -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
##[error]Process completed with exit code 1.

我看到身份验证失败.我也尝试过此步骤,但结果相同:

I see that authentication failed. I have also tried with this step with the same results:

run: mvn deploy -Dserver.username=... -Dserver.password=${{ secrets.GITHUB_TOKEN }} -DskipTests

我不想提供用户名/密码或令牌,因为这是一个公共存储库.有办法发布吗?

I do not want to supply username/password or token as this is a public repository. Is there a way to publish anyway?

谢谢!

要使其正常运行,您需要做两件事:

To make it work, you need to do two things:

  1. 将以下内容添加到您的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>

来源: https://help.github.com/en/articles/configuring-apache-maven-for-use-with-github-package-registry#publishing-a-package

  1. 在构建操作中使用用户名/密码设置Maven设置文件. 就我而言,我做了这样的事情:
  1. Setup a Maven settings file with the username/password from within your build action. In my case I did something like this:

name: Java CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Deploy to Github Package Registry
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        mkdir ~/.m2
        echo "<settings><servers><server><id>github</id><username>OWNER</username><password>${GITHUB_TOKEN}</password></server></servers></settings>" > ~/.m2/settings.xml
        mvn deploy

不幸的是,我认为您不能将用户名/密码作为参数传递给Maven,因此您需要设置设置文件. 来源:是吗可以在命令行的Maven Deploy中传递密码吗?

Unfortunately, I don't think you can pass the username/password as arguments to Maven and so you need to set up the settings file instead. source: Is it possible to pass a password in Maven Deploy in the command line?

最后,我确认这仅适用于非SNAPSHOT工件.当我尝试部署SNAPSHOT版本时,它失败,并显示400错误.

Lastly, I confirm that this only works for non-SNAPSHOT artifacts. When I try deploying a SNAPSHOT version it fails with a 400 error as described.