-->

Saturday, August 5, 2023

[Solved] Failed to deploy artifacts: Could not transfer artifact gitlab-maven status code: 401, reason phrase: Unauthorized (401)

Error:-

While pushing the artifacts jar files to the gitlab project package registry got error Unauthorized (401) as mentioned below

[ERROR] Failed to execute goal:- Failed to deploy artifacts: Could not transfer artifact ExampleApp:jar:1.1 from/to gitlab-maven (https://gitlab-dedicated.com/api/v4/projects/1520/packages/maven): status code: 401, reason phrase: Unauthorized (401) ->

Scenario:-

It was a maven project with private runners running and pushing the artifacts to the project package registry. Usually we are able to achieve this without any issue using the deploy token with mvn clean deploy goal in other projects without any issue. But the lately the project was different because it was having the multiple artifacts created via rest api request and using the maven to push to the gitlab package registry due to which we cannot run mvn clean deploy.  so instead we end up using the mvn deploy:deploy-file to push the artifacts resulting in the authentication issue even with the deploy token when the deploy token itself is having the permission on the package registry as per the gitlab documentation.

Cause:-

Even after trying all the tokens not able to push to the gitlab package registry. It was because of how our settings.xml file has been written. If you just pushing the packages directly in package registry you need to follow the following gitlab documentation

https://docs.gitlab.com/ee/user/packages/maven_repository/


Solution :-

So the above documentation clearly gives the settings.xml as

 <settings>  
  <servers>  
   <server>  
    <id>gitlab-maven</id>  
    <configuration>  
     <httpHeaders>  
      <property>  
       <name>REPLACE_WITH_NAME</name>  
       <value>REPLACE_WITH_TOKEN</value>  
      </property>  
     </httpHeaders>  
    </configuration>  
   </server>  
  </servers>  
 </settings>  

However if you checkout the name it says REPLACE_WITH_NAME. So if you quickly going through the documentation you will understood it as the username of the token which you would have provided at the time of the deploy token creation. However thats where the fine prints come into the picture as

The <name> field must be named to match the token you chose.

Even this might skip the checkpoint as name is mentioned but it specifically says that the name should be of deploy-token as

Token typeName must beToken
Personal access tokenPrivate-TokenPaste token as-is, or define an environment variable to hold the token
Deploy tokenDeploy-TokenPaste token as-is, or define an environment variable to hold the token
CI Job tokenJob-Token${CI_JOB_TOKEN}


which means you dont have to use the username instead use Deploy-Token. Now once i used the Deploy-Token username than my issue of the 401 unauthorized got resolved and i was able to upload the artifact into the gitlab registry. So finally my settings.xml file looks like this

   <servers>  
     <server>  
      <id>gitlab-maven</id>  
      <configuration>  
       <httpHeaders>  
        <property>  
          <name>Deploy-Token</name>  
          <value><deploy-token></value>  
        </property>  
       </httpHeaders>  
      </configuration>  
     </server>  
   <profiles>  
     <profile>  
       <id>gitlab</id>  
       <activation>  
         <activeByDefault>true</activeByDefault>  
       </activation>  
       <repositories>  
         <repository>  
           <id>gitlab-maven</id>  
           <url>https://gitlab-dedicated.com/api/v4/groups/56789/-/packages/maven</url>  
           <releases>  
             <enabled>true</enabled>  
           </releases>  
           <snapshots>  
             <enabled>true</enabled>  
           </snapshots>  
         </repository>  
       </repositories>  
     </profile>  
   </profiles>  
 </settings>  


0 comments:

Post a Comment