Answers: <p>Finally after many trial and errors, I understood that <a href="https://github.com/go-gitea/gitea/issues/24635" rel="nofollow noreferrer">Gitea doesn't support permission</a> changes for the <code>GITHUB_TOKEN</code> so I've been able to create a personnal access token with packages read/write access.</p>
  <p>Here is the final workflow :</p>
  <p>name: Build Java software package</p>
  <pre><code>on:
    push
  #    tags:
  #      - &quot;v*.*.*&quot;
  
  # Not tested but would probably work with GitHub but not Gitea yet
  #permissions:
  #  contents: read
  #  packages: write
  
  jobs:
      publish:
          runs-on: ubuntu-latest
          steps:
              - name: Checkout
                uses: https://github.com/actions/checkout@v3
                
              - name: Install Maven
                uses: https://github.com/stCarolas/setup-maven@v4
              
              - name: Install Java
                uses: https://github.com/actions/setup-java@v3
                with:
                  distribution: temurin
                  java-version: 18
                  cache: maven
                  overwrite-settings: true
                  server-id: example
              
              - name: Deploy
                run: mvn -B deploy
                env:
                  GITHUB_TOKEN: ${{ secrets.PACKAGES_TOKEN }}
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-07-02T17:02:41.643Z
Answers: 2023-07-02T20:07:12.243Z
Answers: 2023-07-02T20:07:12.243Z
Answers: Alexandre Lavoie
Body: <p>I'm new to CI/CD, Gitea/GitHub, Docker and Maven. I've setup a self-hosted Gitea server and Runner. Before migrating all my projects, I'm testing CI/CD. For this purpose I've made test projects and now facing a maven deploy issue.</p>
  <p>Here is the test project <code>pom.xml</code> (note : I've replaced real domain with <code>&quot;example&quot;</code>) :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.example.test&lt;/groupId&gt;
      &lt;artifactId&gt;test-library-java&lt;/artifactId&gt;
      &lt;version&gt;1.0.0&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;name&gt;TestLibraryJava&lt;/name&gt;
      &lt;description&gt;
          Test library for gitea actions deploy
      &lt;/description&gt;
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.7&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.7&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
      
      &lt;repositories&gt;
          &lt;repository&gt;
          &lt;id&gt;example&lt;/id&gt;
              &lt;url&gt;https://git.example.com/api/packages/example/maven&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
      &lt;distributionManagement&gt;
          &lt;repository&gt;
              &lt;id&gt;example&lt;/id&gt;
              &lt;url&gt;https://git.example.com/api/packages/example/maven&lt;/url&gt;
          &lt;/repository&gt;
          &lt;snapshotRepository&gt;
              &lt;id&gt;example&lt;/id&gt;
              &lt;url&gt;https://git.example.com/api/packages/example/maven&lt;/url&gt;
          &lt;/snapshotRepository&gt;
      &lt;/distributionManagement&gt;
  &lt;/project&gt;
  </code></pre>
  <p>Here is the test project <code>build.yml</code> :</p>
  <pre><code>name: Build Java software package
  
  on:
    push
  
  jobs:
      publish:
          runs-on: ubuntu-latest
          permissions:
              contents: read
              packages: write
          steps:
              - name: Checkout
                uses: https://github.com/actions/checkout@v3
                
              - name: Install Maven
                uses: https://github.com/stCarolas/setup-maven@v4
              
              - name: Install Java
                uses: https://github.com/actions/setup-java@v3
                with:
                  distribution: temurin
                  java-version: 18
                  cache: maven
                  overwrite-settings: true
                  server-id: example
              
              - run: cat ~/.m2/settings.xml
              
              - name: Deploy
                run: mvn -B deploy
  </code></pre>
  <p>The result from cat :</p>
  <pre><code>&lt;settings xmlns=&quot;http://maven.apache.org/SETTINGS/1.0.0&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd&quot;&gt;
    &lt;servers&gt;
      &lt;server&gt;
        &lt;id&gt;example&lt;/id&gt;
        &lt;username&gt;${env.GITHUB_ACTOR}&lt;/username&gt;
        &lt;password&gt;${env.GITHUB_TOKEN}&lt;/password&gt;
      &lt;/server&gt;
    &lt;/servers&gt;
  </code></pre>
  <p>The result from deploy (note : I've replaced real domain with <code>&quot;example&quot;</code>) :</p>
  <pre><code>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project test-library-java: Failed to deploy artifacts: Could not transfer artifact com.example.test:test-library-java:jar:1.0.0 from/to example (https://git.example.com/api/packages/example/maven): Failed to transfer file: https://git.example.com/api/packages/example/maven/com/example/test/test-library-java/1.0.0/test-library-java-1.0.0.jar. Return code is: 401, ReasonPhrase: Unauthorized. -&gt; [Help 1]
  </code></pre>
  <p>I've done some tests :</p>
  <p>Deploy from development workstation using <code>mvn deploy</code> and token in <code>settings.xml</code>, this works.</p>
  <p>Overriding container <code>settings.xml</code> with hardcoded password works</p>
  <pre><code>#            - name: Configure Maven
  #              env:
  #                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #              run: echo &quot;&lt;settings&gt;&lt;servers&gt;&lt;server&gt;&lt;id&gt;example&lt;/id&gt;&lt;username&gt;$GITHUB_ACTOR&lt;/username&gt;&lt;password&gt;TOKENFROMGITEA&lt;/password&gt;&lt;/server&gt;&lt;/servers&gt;&lt;/settings&gt;&quot; &gt; ~/.m2/settings.xml
  </code></pre>
  <p>So now I'm wondering if the <code>settings.xml</code> really have the environment variables replaced or if the actions provided GITHUB_TOKEN have the rights for packages/write. Also note that my user is the owner of the repository and admin of Gitea.</p>
  <p>Versions used :</p>
  <ul>
  <li>Gitea 1.20.0-rc2</li>
  <li>Docker 20.10.18</li>
  <li>Containerd 1.6.8 9cd3357</li>
  </ul>
  <p>Documentation used :</p>
  <ul>
  <li><a href="https://docs.gitea.com/next/usage/packages/maven" rel="nofollow noreferrer">Gitea Maven Package Registry</a></li>
  <li><a href="https://github.com/actions/setup-java" rel="nofollow noreferrer">GitHub actions/setup-java</a></li>
  <li><a href="https://github.com/go-gitea/gitea/issues/23575" rel="nofollow noreferrer">Gitea issue maven 401 closed</a></li>
  </ul>
  
Tags: <github><maven>
Title: Gitea/GitHub Actions maven deploy causing 401 unauthorized
UserDisplayName: Alexandre Lavoie
ThreadText: # Gitea/GitHub Actions maven deploy causing 401 unauthorized
  
  (Asked by: username_0 on 2023-06-30)
  
  I'm new to CI/CD, Gitea/GitHub, Docker and Maven. I've setup a self-hosted Gitea server and Runner. Before migrating all my projects, I'm testing CI/CD. For this purpose I've made test projects and now facing a maven deploy issue.
  
  Here is the test project `pom.xml` (note : I've replaced real domain with `"example"`) :
  
  ```
  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example.test</groupId>
      <artifactId>test-library-java</artifactId>
      <version>1.0.0</version>
      <packaging>jar</packaging>
      <name>TestLibraryJava</name>
      <description>
          Test library for gitea actions deploy
      </description>
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.source>1.7</maven.compiler.source>
          <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
      
      <repositories>
          <repository>
          <id>example</id>
              <url>https://git.example.com/api/packages/example/maven</url>
          </repository>
      </repositories>
      <distributionManagement>
          <repository>
              <id>example</id>
              <url>https://git.example.com/api/packages/example/maven</url>
          </repository>
          <snapshotRepository>
              <id>example</id>
              <url>https://git.example.com/api/packages/example/maven</url>
          </snapshotRepository>
      </distributionManagement>
  </project>
  
  ```
  
  Here is the test project `build.yml` :
  
  ```
  name: Build Java software package
  
  on:
    push
  
  jobs:
      publish:
          runs-on: ubuntu-latest
          permissions:
              contents: read
              packages: write
          steps:
              - name: Checkout
                uses: https://github.com/actions/checkout@v3
                
              - name: Install Maven
                uses: https://github.com/stCarolas/setup-maven@v4
              
              - name: Install Java
                uses: https://github.com/actions/setup-java@v3
                with:
                  distribution: temurin
                  java-version: 18
                  cache: maven
                  overwrite-settings: true
                  server-id: example
              
              - run: cat ~/.m2/settings.xml
              
              - name: Deploy
                run: mvn -B deploy
  
  ```
  
  The result from cat :
  
  ```
  <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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
      <server>
        <id>example</id>
        <username>${env.GITHUB_ACTOR}</username>
        <password>${env.GITHUB_TOKEN}</password>
      </server>
    </servers>
  
  ```
  
  The result from deploy (note : I've replaced real domain with `"example"`) :
  
  ```
  [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project test-library-java: Failed to deploy artifacts: Could not transfer artifact com.example.test:test-library-java:jar:1.0.0 from/to example (https://git.example.com/api/packages/example/maven): Failed to transfer file: https://git.example.com/api/packages/example/maven/com/example/test/test-library-java/1.0.0/test-library-java-1.0.0.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
  
  ```
  
  I've done some tests :
  
  Deploy from development workstation using `mvn deploy` and token in `settings.xml`, this works.
  
  Overriding container `settings.xml` with hardcoded password works
  
  ```
  #            - name: Configure Maven
  #              env:
  #                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #              run: echo "<settings><servers><server><id>example</id><username>$GITHUB_ACTOR</username><password>TOKENFROMGITEA</password></server></servers></settings>" > ~/.m2/settings.xml
  
  ```
  
  So now I'm wondering if the `settings.xml` really have the environment variables replaced or if the actions provided GITHUB\_TOKEN have the rights for packages/write. Also note that my user is the owner of the repository and admin of Gitea.
  
  Versions used :
  
  * Gitea 1.20.0-rc2
  * Docker 20.10.18
  * Containerd 1.6.8 9cd3357
  
  Documentation used :
  
  * [Gitea Maven Package Registry](https://docs.gitea.com/next/usage/packages/maven)
  * [GitHub actions/setup-java](https://github.com/actions/setup-java)
  * [Gitea issue maven 401 closed](https://github.com/go-gitea/gitea/issues/23575)
  
  **Tags:** github, maven
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-07-02. Score: 0 | Accepted answer
  
  Finally after many trial and errors, I understood that [Gitea doesn't support permission](https://github.com/go-gitea/gitea/issues/24635) changes for the `GITHUB_TOKEN` so I've been able to create a personnal access token with packages read/write access.
  
  Here is the final workflow :
  
  name: Build Java software package
  
  ```
  on:
    push
  #    tags:
  #      - "v*.*.*"
  
  # Not tested but would probably work with GitHub but not Gitea yet
  #permissions:
  #  contents: read
  #  packages: write
  
  jobs:
      publish:
          runs-on: ubuntu-latest
          steps:
              - name: Checkout
                uses: https://github.com/actions/checkout@v3
                
              - name: Install Maven
                uses: https://github.com/stCarolas/setup-maven@v4
              
              - name: Install Java
                uses: https://github.com/actions/setup-java@v3
                with:
                  distribution: temurin
                  java-version: 18
                  cache: maven
                  overwrite-settings: true
                  server-id: example
              
              - name: Deploy
                run: mvn -B deploy
                env:
                  GITHUB_TOKEN: ${{ secrets.PACKAGES_TOKEN }}
  
  ```
  
  
  ---
  
  