英文:
Deploy to Maven Packages via GitHub Actions
问题
The error you're encountering indicates an authentication issue when trying to deploy artifacts to GitHub Packages. To resolve this, you need to use a GitHub token for authentication. Here's what you should do:
-
Create a Personal Access Token (PAT) on GitHub:
- Go to your GitHub account settings.
- Navigate to "Developer settings" > "Personal access tokens."
- Click "Generate token" and give it the necessary permissions (e.g., repo, write:packages).
- Make sure to copy the token value because you won't be able to see it again.
-
Add the GitHub Token as a secret in your repository:
- In your GitHub repository, go to "Settings" > "Secrets."
- Click "New repository secret."
- Name it something like
GH_TOKEN
and paste the token value as the secret's content.
-
Modify your GitHub Actions workflow YAML to use the secret:
Update your workflow YAML like this:
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.GH_TOKEN }}
Replace GH_TOKEN
with the name of the secret you created in step 2.
With this setup, your workflow should authenticate correctly using the GitHub token and resolve the 401 Unauthorized error.
英文:
So, I have a GitHub Repository with compiled Maven projects. I setup a GitHub Actions to deploy them to GitHub Packages. This is my current 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
As one can see, 'server-username' and 'server-password' are still blank, which is my main problem. I read here: https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven that I have to add this but I don't know what username and password to use except my own. But I want to avoid that.
The deploy.sh has the following content:
#!/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
In this case 'OWNER' and 'REPOSITORY' are purposely empty.
I already tried to use Actions secrets or Deploy keys but I don't really understand them and the documentation is not really explaining anything. When I tried these solution it failed with an 401 Unauthorized error code.
The question is which credentials do I have to use? The documentation confuses me and I don't know if my approach is optimal. It would be really nice if someone could help me. Thanks in advance!
UPDATE
New workflow I tried:
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 }}
And this is the error I always get:
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]
答案1
得分: 1
"The correct term is 'publish' here, not 'deploy'."
"actions/setup.java
should be actions/setup-java
, i.e., replace .
with -
."
"You need to follow Publishing packages to GitHub Packages."
"Regarding 'server-username' and 'server-password,' you don't have to configure these for GitHub Packages as they already have the correct defaults, i.e., 'GITHUB_ACTOR' and 'GITHUB_TOKEN' respectively. See Maven options."
"Refer to the github
context to configure 'OWNER' and 'REPOSITORY' values."
英文:
The correct term is "publish" here, not "deploy".
actions/setup.java
should be actions/setup-java
i.e. replace .
with -
.
You need to follow Publishing packages to GitHub Packages.
Regarding server-username
and server-password
, you don't have to configure these for GitHub Packages as they already have the correct defaults i.e. GITHUB_ACTOR
and GITHUB_TOKEN
respectively, see Maven options.
Refer to the github
context to configure OWNER
and REPOSITORY
values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论