0
votes

I have a custom Nexus for project related utility jars. I have a Jenkins to build these jars.

I want to use gradle in the project and a Jenkinsfile to define the pipeline. As the last stage, I want the build job to deploy the artifact to my Nexus.

Since I don't want the credentials for the Nexus in my source repository (and therefor neither in the gradle script nor in the Jenkinsfile), I set up a credentials entry in the Jenkins Credentials plugin, containing user and password for the Nexus.

How do I refer to these credentials from within the Jenkinsfile such that I can use them to deploy to the Nexus?

Currently, my deploy stage looks like this:

stage('Publish') {
    sh "gradle upload"
}

Can I use gradle for this purpose at all? I know I can use it locally, but it would be ok for me to use a different command from within the Jenkinsfile.

In gradle, I use this to define the task:

def nexusUser = "$System.env.NEXUS_USER"
def nexusPass = "$System.env.NEXUS_PASS"

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://nexus.example.com/repository/maven-releases/") {
                authentication(userName: nexusUser, password: nexusPass)
            }
        }
    }
}

(I set the user and password as environment variables locally. That's not an option on the Jenkins, of course.)

2
Why is using environment variables not going to work? - mkobit
Totally agree you can include environment variables in your jenkins installation. - Carlos Cavero
You can also use the Global Credentials provided by Jenkins - Carlos Cavero
@mkobit: Environment variables would certainly work. It does locally, too. I just don't want to put credentials into env vars that are visible for anyone else. But maybe there is a way to solve this with env variables that I don't see and hence my hesitation to use them. @Carlos: Can you give an example of what you mean with the Global Credentials provided by Jenkins? Could you formulated that as an answer? Thank you for the quick response! - inedible
@inedible if you use withCredentials(){} env vriabels will be visiable only inside its block {} - Vitalii Vitrenko

2 Answers

2
votes

You can set external variables in ~/.gradle/gradle.properties nexusUser=superuser nexusPass=superpassword

1
votes

You can use withCredentials(){} step to inject environment variables which will be visible only inside {} block.