codependent
codependent

Reputation: 24452

How to pass username password to Gradle to upload artifacts to Nexus from a Jenkins pipeline

I need to upload artifacts to Nexus from a Jenkins declarative pipeline. There is a Nexus plugin that I can't use because it doesn't allow to publish SNAPSHOTs. Therefore I have to upload them directly through the Gradle upload task. The problem is that I am getting a 401 error code from Nexus and it seems the nexus user and passwords aren't being passed from the pipeline to the ./gradlew upload task.

This is my current configuration:

Pipeline

withCredentials([usernamePassword(credentialsId: 'asdf23432-fe69-1234-2222-ffa65yteec2c', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
    sh "./gradlew upload --debug"
}

Gradle: In the ext section I try to access the NEXUS_USER and NEXUS_PASSWORD variables from the withCredentials step.

ext{
    nexusUser = System.properties['NEXUS_USER']
    nexusPassword = System.properties['NEXUS_PASSWORD']
}

uploadArchives {
    repositories {
        mavenDeployer {
            snapshotRepository(url: 'http://somerepo'){
                authentication(userName: nexusUser, password: nexusPassword)
            }
        }
    }
}

Upvotes: 0

Views: 3841

Answers (1)

Henry
Henry

Reputation: 43738

You can pass the variables as system properties using this:

withCredentials([usernamePassword(credentialsId: 'asdf23432-fe69-1234-2222-ffa65yteec2c', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
    sh "./gradlew -DNEXUS_PASSWORD=$NEXUS_PASSWORD -DNEXUS_USER=$NEXUS_USER upload --debug"
}

Upvotes: 1

Related Questions