title: Git Hub Actions Authentication with maven to private package registry of other repository
body: <p>The project setup is as followed:</p>
  <ol>
  <li>Project A (Some Java Application)</li>
  <li>Project B (Some Java Library)</li>
  </ol>
  <p>Both are built with maven. Project B publishes its package to the github package registry of its own repository. Project A has a dependency to the artifact from Project B in the pom.xml which looks somewhat like this:</p>
  <pre><code>&lt;dependency&gt;
     &lt;groupId&gt;com.company&lt;/groupId&gt;
     &lt;artifactId&gt;library-project-b&lt;/artifactId&gt;
     &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>Project A also includes the following repository reference to the package registry of Project B:</p>
  <pre><code>&lt;repository&gt;
     &lt;id&gt;github-library-project-b&lt;/id&gt;
     &lt;name&gt;Project B Github packages repositories&lt;/name&gt;
     &lt;url&gt;https://maven.pkg.github.com/organization/library-project-b&lt;/url&gt;
     &lt;releases&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
     &lt;/releases&gt;
     &lt;snapshots&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
     &lt;/snapshots&gt;
  &lt;/repository&gt;
  </code></pre>
  <p>For authenticating to the package registry of Porject B, Project A also includes a settings.xml which has the credentials set via environment variables that are provided via secrets from Github actions:</p>
  <pre><code>&lt;server&gt;
     &lt;id&gt;github-library-project-b&lt;/id&gt;
     &lt;username&gt;${env.USER_PACKAGE_READ}&lt;/username&gt;
     &lt;password&gt;${env.TOKEN_PACKAGE_READ}&lt;/password&gt;
  &lt;/server&gt;
  </code></pre>
  <p>Inside the Continuous Integration Workflow in github actions I have the following step which maps the secrets from the repository for the registry access of project B to the environment variables which are used within the settings.xml and starts the build via mvn command:</p>
  <pre><code>- name: Build Package
    env:
       USER_PACKAGE_READ: ${{ secrets.USER_PACKAGE_READ }}
       TOKEN_PACKAGE_READ: ${{ secrets.TOKEN_PACKAGE_READ }}
    run: mvn -B --settings settings.xml clean package --file pom.xml -DskipTests
  </code></pre>
  <p>As I have read, that I can't use the GITHUB_TOKEN to access a package registry of another repository as the one the git hub action is run within, I have created a Personal Access token on my user account. (With package_read permission)</p>
  <hr />
  <p>But no matter what I try - within the build stage, I will always end up with the following 401 error on the maven build command error trying to download the artifact:</p>
  <pre><code>Error:  Failed to execute goal on project PROjECT-A: Could not resolve dependencies for project com.company:ms-gp-events:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.company:library-project-b:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.company:library-project-b:0.0.1-SNAPSHOT: Could not transfer artifact com.company:library-project-b:0.0.1-SNAPSHOT from/to github-library-project-b (https://maven.pkg.github.com/organization/library-project-b): Authentication failed for https://maven.pkg.github.com/company/library-project-b/com/company/library-project-b/0.0.1-SNAPSHOT/library-project-b-0.0.1-SNAPSHOT.pom 401 Unauthorized -&gt; [Help 1]
  </code></pre>
  <hr />
  <p>Has anyone a working example of a github actions workflow with maven that references a dependency which is stored within a github package registry from another private repository? I'm confused and frustrated that something so simple, takes so much time to setup.</p>