2
votes

Using the Jenkins Artifactory Plugin and Gradle, I am able to deploy to my Artifactory instance successfully. However, I have not been able to use the credentials entered into Jenkins configuration to resolve the artifact from the same repository.

Here is the build.gradle, adopted right from Artifactory's "Generate Build Script" feature in Artifactory.

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'
buildscript {
    repositories {
        maven {
            url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }

    }
    dependencies {
        //Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.3"
    }
}

allprojects {
    apply plugin: "com.jfrog.artifactory"
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
    resolve {
        repository {
            repoKey = 'libs-release'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
}


dependencies {
    compile group: "com.myorg", name: "internal-library", version: '1.0'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

Job configuration on Jenkins: Job configuration

Jenkins Artifactory plugin configuration: enter image description here

Error:

FAILURE: Build failed with an exception.

* Where:
Build file '/var/lib/jenkins/jobs/deleteme/workspace/build.gradle' line: 10

* What went wrong:
A problem occurred evaluating root project 'untitled2'.
> Could not find property 'artifactory_user' on Credentials [username: null].

Locally, resolving artifacts works (artifactory_user etc are resolved, because I configured them in my local ~/.gradle/gradle.properties). However, builds fail, because artifactory_user is not defined. Of course, I can configure a gradle.properties for Jenkins to use (and this works), however how is this supposed to work? It appears the Jenkins Artifactory Plugin Gradle integration is supposed to somehow communicate resolver credentials. Is there a way to do this without replicating the credentials in two places on Jenkins?

2

2 Answers

2
votes

tl;dr username = "${project.getProperty('artifactory.publish.username')}"

Note: I haven't tested this on a Jenkins machine yet

The Jenkins artifactory plugin's gradle integration appears to do two things

  • Inject the artifactory configuration using a [gradle init script]
  • Provide build and artifactory configuration information

Both involve writing files to a temporary folder (i.e. /tmp on linux). If you have access to your build server you will probably have a lot of buildInfo\d{19}.properties and init-artifactory\d{19}gradle files in your temp folder.

Looking inside one of these buildInfo files reveals the resolve and publish credentials are stored in artifactory.resolve.username and artifactory.publish.username, respectively.

Trying to setting and trying to use ${artifactory.publish.username} directly doesn't work on my local machine; I assume it has to do with gradle trying to access the property before it is set. But the project.getProperty method works.

buildscript {
    repositories {
        maven {
            url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
            credentials {
                username = "${project.getProperty('artifactory.resolve.username')}"
                password = "${project.getProperty('artifactory.resolve.password')}"
            }
        }
    }
}

artifactory {
    publish {
        contextUrl = "${project.getProperty('artifactory.publish.contextUrl')}"
        repository {
            repoKey = 'libs-release-local'
            username = "${project.getProperty('artifactory.publish.username')}"
            password = "${project.getProperty('artifactory.publish.password')}"
            maven = true

        }
    }
    resolve {
        repository {
            contextUrl = "${project.getProperty('artifactory.resolve.contextUrl')}"
            repoKey = 'libs-release'
            username = "${project.getProperty('artifactory.resolve.username')}"
            password = "${project.getProperty('artifactory.resolve.password')}"
            maven = true

        }
    }
}

You will have to update your local ~/.gradle/gradle.properties accordingly

artifactory.publish.contextUrl=<artifactory-url>
artifactory.publish.username=<username>
artifactory.publish.password=<password>
artifactory.resolve.contextUrl=<artifactory-url>
artifactory.resolve.username=<username>
artifactory.resolve.password=<password>
0
votes

I am also banging my head against the wall with this one...I tried all the combinations myself and the only one that seems to be working is adding the gradle.properties to the jenkins server.

Finally I ended up not using the artifactory gradle plugin from jenkins alltogether.R ather add it to the build.gradle and simply call artifactoryPublish from Jenkins Server gradle plugin.