15

我想从公共存储库的 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

我使用 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: mvn deploy -Dserver.username=... -Dserver.password=${{ secrets.GITHUB_TOKEN }} -DskipTests

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

谢谢!

4

5 回答 5

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

更多信息:

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

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

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

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

这是一个真实的例子

于 2020-03-14T12:25:14.420 回答
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>

来源: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,因此您需要设置设置文件。来源:是否可以在命令行中在 Maven Deploy 中传递密码?

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

于 2019-09-20T04:18:52.520 回答
5

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
于 2020-09-12T16:12:52.607 回答
4

我的项目也有类似的问题。每次我跑mvn deploy,它都失败了:

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

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

也许它也对你有用。

于 2019-09-18T11:47:56.163 回答
4

好吧,根据:

我认为你可以简单地这样做:

  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>

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

于 2019-09-26T03:12:04.927 回答