javamavencontinuous-integrationgithub-actionsgithub-packages

Github actions: authentication failed for <package> status: 401 Unauthorized -> [Help 1]


I am trying to setup a basic CI environement for a maven project.

Here is the github action yml boilerplate I am using:

name: Java CI with Maven

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'
        cache: maven
    - name: Build with Maven
      run: mvn -B package --file pom.xml

When executing the workflow, I get hit with this error:

Error:  
Failed to execute goal on project example: 
Could not resolve dependencies for project <dependency>: 
Failed to collect dependencies at <dependency>: 
Failed to read artifact descriptor for <dependency>: 
Could not transfer artifact <dependency> from/to github (https://maven.pkg.github.com/example/package/): 
authentication failed for https://maven.pkg.github.com/example/package/.../<dependency>.pom, 
status: 401 Unauthorized -> [Help 1]

I don't think there is anything inherently wrong with the workflow file however, my maven project uses a package which I deployed through github packages.

To my limited knowledge, I beleive that to import my deployed pacakge I need to have some credentials in .m2/settings.xml.

This works fine if I do it locally but how do I achieve this for a workflow?


Solution

  • It is an old one, but maybe it will help someone in the future. You need to pass to the action environment variable called GITHUB_TOKEN. So:

    name: Java CI with Maven
    
    on:
      push:
        branches: [ "master" ]
      pull_request:
        branches: [ "master" ]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
        - name: Set up JDK 11
          uses: actions/setup-java@v3
          with:
            java-version: '11'
            distribution: 'temurin'
            cache: maven
        - name: Build with Maven
          env:
            GITHUB_TOKEN: ${{ secrets.MY_TOKEN }}
          run: mvn -B package --file pom.xml
    

    You can read more about it here: https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven