从Github Action部署到Github Package Registry

时间:2019-08-29 13:43:15

标签: maven github github-actions github-package-registry

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

我有一个用于工作流程的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

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

此操作在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: mvn deploy -Dserver.username=... -Dserver.password=${{ secrets.GITHUB_TOKEN }} -DskipTests

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

谢谢!

5 个答案:

答案 0 :(得分:4)

要使其正常工作,您需要做两件事:

  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设置文件。 就我而言,我做了这样的事情:
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,因此您需要设置设置文件。 来源:Is it possible to pass a password in Maven Deploy in the command line?

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

答案 1 :(得分:4)

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}"

更多信息:

我在for Jenkins之前建立了同样的东西,可以告诉你不需要创建settings.xml或修改仓库中的pom.xml

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

此外,您无需手动添加您的存储库和用户名,这些都可以从环境中读取。

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

这里是real-life example

答案 2 :(得分:3)

我的项目遇到类似的问题。 每次我运行mvn deploy时,都会失败并显示:

  

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

但是,我一时兴起,将项目的版本号从 0.0.3-SNAPSHOT 更改为 0.0.4 ,然后它开始工作。

>

也许它也对您有用。

答案 3 :(得分:2)

根据:

我认为您可以这样做:

  1. 在下方添加到您的工作流程
- name: Deploy to Github Package Registry
    env:
      GITHUB_USERNAME: x-access-token
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    run:
      mvn --settings settings.xml deploy
  1. 然后将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
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <activeProfiles>
        <activeProfile>github</activeProfile>
    </activeProfiles>

    <profiles>
        <profile>
            <id>github</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://repo1.maven.org/maven2</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>github</id>
                    <name>GitHub OWNER Apache Maven Packages</name>
                    <url>https://maven.pkg.github.com/OWNER </url>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <servers>
        <server>
            <id>github</id>
            <username>${env.GITHUB_USERNAME}</username>
            <password>${env.GITHUB_TOKEN}</password>
        </server>
    </servers>
</settings>

它对我有用,我希望这会有所帮助。

答案 4 :(得分:1)

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>

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