简体   繁体   中英

'Not authorized' errors after switching from vanilla Maven to Jenkins Artifactory Plugin

I am trying to use the Jenkins Artifactory Plugin to run a Maven build and publish the buildInfo to my Artifactory instance. All of this is scripted in a Jenkinsfile and executed as a pipeline build. Code as follows:

def server
def buildInfo
def rtMaven

server = Artifactory.server('arty')

rtMaven = Artifactory.newMavenBuild()
rtMaven.tool = 'Maven 3.3.9' // Tool name from Jenkins configuration
rtMaven.deployer releaseRepo: 'ci-test', snapshotRepo: 'ci-test', server: server
rtMaven.resolver releaseRepo: 'libs-release', snapshotRepo: 'libs-snapshot', server: server
rtMaven.deployer.deployArtifacts = false // Disable artifacts deployment during Maven run

buildInfo = Artifactory.newBuildInfo()

rtMaven.run pom: 'pom.xml', goals: "-e -B install".toString(), buildInfo: buildInfo
server.publishBuildInfo buildInfo

This results in many errors, similar to the following

[main] ERROR org.apache.maven.cli.MavenCli - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-install-plugin:jar:2.4: Could not transfer artifact org.apache.maven.plugins:maven-plugins:pom:23 from/to artifactory-release ( https://arty.example.com:8443/artifactory/libs-release ): Not authorized , ReasonPhrase: Unauthorized. -> [Help 1]

It is only when I switch to using the Artifactory plugin that the errors begin.

Before attempting this, I confirmed that Maven is correctly configured within Jenkins. Something like the following works just fine:

withMaven(maven: 'Maven 3.3.9') {
    sh "mvn -e -B ${args}"
}

Doing a mvn deploy will push the war file up to Artifactory.

I suspected it had something to do with my Maven settings. I have verified that Maven is working correctly as a Jenkins tool. I can supply a settings.xml in the .m2 folder, or a config file via Jenkins, something like:

withMaven(maven: 'Maven 3.3.9', mavenSettingsConfig: '10452c41-5bdb-4d11-b711-9b2d00751c2e') 

Both options work (and cause expected failures if I remove them).

What I have noticed is that the Artifactory Plugin allows me to specify the name of the Jenkins Tool ( Maven 3.3.9 ) but not the mavenSettingsConfig. To that end I also attempted wrapping the call to rtMaven.run in a configFileProvider block and passing the settings to mvn:

configFileProvider(
  [configFile(fileId: '10452c41-5bdb-4d11-b711-9b2d00751c2e', variable: 'MAVEN_SETTINGS')]) {
    rtMaven.run pom: 'pom.xml', goals: "-s $MAVEN_SETTINGS -e -B install".toString(), buildInfo: buildInfo
    server.publishBuildInfo buildInfo
    }

This too proved unsuccessful.

At this point, any suggestions would be helpful!

Version info:
Jenkins: 2.89.3
Jenkins Artifactory Plugin: 2.13.1
Maven: 3.3.9
Artifactory: 5.8.3 Pro

settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <localRepository>/home/jenkins-user/.m2/repository</localRepository> 
  <servers>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>central</id>
    </server>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>snapshots</id>
    </server>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>arty</id>
    </server>
  </servers>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>https://arty.example.com:8443/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>https://arty.example.com:8443/artifactory/libs-snapshot</url>
        </repository>
        <repository>
          <snapshots />
          <id>arty</id>
          <name>ci-test</name>
          <url>https://arty.example.com:8443/artifactory/ci-test</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>plugins-release</name>
          <url>https://arty.example.com:8443/artifactory/plugins-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>plugins-snapshot</name>
          <url>https://arty.example.com:8443/artifactory/plugins-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

In this case you specified your Artifactory Server "arty" in Jenkins configuration Configure System . There you also need to add credentials.

If you don't want to use Jenkins configuration you can hardcode all information in pipeline def server = Artifactory.newServer url: 'artifactory-url', username: 'username', password: 'password'

Or take credentials from Jenkins Credentials def server = Artifactory.newServer url: 'artifactory-url', credentialsId: 'credential'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM