通过 GitHub Actions 部署到 Maven 包

问题描述 投票:0回答:1

所以,我有一个 GitHub 存储库,其中包含已编译的 Maven 项目。我设置了 GitHub Actions 以将它们部署到 GitHub Packages。这是我当前的 release-package.yml:

name: Maven Package

on: push

jobs:
  publish-gpr:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - uses: actions/setup.java@v3
        with:
          java-version: '11'
          distribution: 'adopt'
          server-id: github
          server-username: ...
          server-password: ...
      - name: Publish Package
        run: |
          chmod +x ./deploy.sh
          ./deploy.sh
        shell: bash

可以看到,'server-username' 和 'server-password' 仍然是空白,这是我的主要问题。我在这里阅读:https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven我必须添加这个但我不知道用户名和密码是什么使用除了我自己的。但我想避免这种情况。 deploy.sh 包含以下内容:

#!/bin/bash

OWNER=...
REPOSITORY=...

filelist=$(find . -type f -path "*.pom")
for file in $filelist
do
    data=$(python getdata.py $file)
    groupId=$(echo $data | cut -d' ' -f1)
    artifactId=$(echo $data | cut -d' ' -f2)
    version=$(echo $data | cut -d' ' -f3)

    mvn deploy:deploy-file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dfile=$file -Durl=https://maven.pkg.github.com/$OWNER/$REPOSITORY
done

在这种情况下,“OWNER”和“REPOSITORY”是有意为空的。

我已经尝试使用 Actions secrets 或 Deploy 密钥,但我并不真正理解它们,文档也没有真正解释任何内容。当我尝试这些解决方案时,它失败了,并显示 401 未经授权的错误代码。

问题是我必须使用哪些凭据?文档让我感到困惑,我不知道我的方法是否最佳。如果有人能帮助我,那就太好了。提前致谢!

更新

我试过的新工作流程:

name: Maven Package

on: push

jobs:
  publish-gpr:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write
      packages: write
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Publish Package
        run: |
          chmod +x ./deploy.sh
          ./deploy.sh
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

这是我经常遇到的错误:

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact <artifact> from/to remote-repository (https://maven.pkg.github.com/<repo-path>): authentication failed for https://maven.pkg.github.com/<repo-path>/<path-to-jar>, status: 401 Unauthorized -> [Help 1]
java maven github-actions
1个回答
0
投票

这里正确的术语是“发布”,而不是“部署”。

actions/setup.java
应该是
actions/setup-java
即用
.
替换
-
.

你需要按照Publishing packages to GitHub Packages.

关于

server-username
server-password
,您不必为 GitHub Packages 配置它们,因为它们已经具有正确的默认值,即分别为
GITHUB_ACTOR
GITHUB_TOKEN
,请参阅 Maven 选项

参考

github
上下文来配置
OWNER
REPOSITORY
值。

© www.soinside.com 2019 - 2024. All rights reserved.