从 Github Action 部署到 Github Package Registry

erd*_*dos 15 github maven github-actions github-package-registry

我想从公共存储库的 GitHub 操作部署到 GitHub 包注册表。

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

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
Run Code Online (Sandbox Code Playgroud)

我使用 Leiningen 来构建项目并生成一个 POM 文件。然后我想使用 Maven 将工件部署到 GitHub 包注册表。

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

[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.
Run Code Online (Sandbox Code Playgroud)

我看到身份验证失败。我也尝试过这一步,结果相同:

run: mvn deploy -Dserver.username=... -Dserver.password=${{ secrets.GITHUB_TOKEN }} -DskipTests
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

lms*_*ant 10

为了让它工作,你需要做两件事:

  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>
Run Code Online (Sandbox Code Playgroud)

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

  1. 在构建操作中使用用户名/密码设置 Maven 设置文件。就我而言,我做了这样的事情:
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
Run Code Online (Sandbox Code Playgroud)

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

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


sch*_*rer 10

TL;DR:只需提交以下内容.github/workflows/mavenpublish.yml并通过 GitHub 网页创建一个版本即可触发该过程:

name: Maven Package

on:
  release:
    types: [created]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - 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 -p ~/.m2
          echo "<settings><servers><server><id>gh</id><username>$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $1}')</username><password>\${env.GITHUB_TOKEN}</password></server></servers></settings>" > ~/.m2/settings.xml
          REPO="gh::default::https://maven.pkg.github.com/${GITHUB_REPOSITORY}"
          mvn deploy -DaltReleaseDeploymentRepository="${REPO}" -DaltSnapshotDeploymentRepository="${REPO}"
Run Code Online (Sandbox Code Playgroud)

更多信息:

我之前为 Jenkins构建过同样的东西并且可以告诉你,你不需要在你的 repo 中创建settings.xml或调整你pom.xml的。

您甚至可以避免将您的 GitHub 令牌写入settings.xml(更安全)。

此外,您不需要手动添加您的 repo 和用户名,这些可以从环境中读取。

如果您希望它基于推送,只需将后面的行更改on:[push].

这是一个现实生活中的例子


Lia*_*iam 6

2020年有一个更简单的方法。

首先,将分发配置添加到 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>
Run Code Online (Sandbox Code Playgroud)

必须id是github。

二、运用于actions/setup-java@v1行动

steps:
  - uses: actions/checkout@v2

  - uses: actions/setup-java@v1
    with:
      java-version: 1.8

  - name: Publish to GitHub Packages
    env:
      GITHUB_TOKEN: ${{ github.token }}
    run: mvn deploy
Run Code Online (Sandbox Code Playgroud)


小智 5

我的项目也有类似的问题。每次我运行时mvn deploy,都会失败:

无法从/到 github 传输元数据(https://maven.pkg.github.com/.../..):400

然而,我一时兴起,将项目的版本号从0.0.3-SNAPSHOT更改为0.0.4,之后,它就起作用了。

也许它也适合你。