Chanafot
Chanafot

Reputation: 796

enviroment variables on jenkins pipeline for user and password

I´m quite new on using groovy for jenkins pipeline. I have a pipeline that is already running performing some steps like running unitest, sonnarqube anaysis etc. One of the steps is to upload artifact to artifactory using curl -u. I don´t want to show user and pass on output script, so I´m using credential plug in In which I stored user and password and has the ID. But I don´t know how to pass that to the sh command using variables. This is what I have now in that step using withCredentials .

stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'willy11', passwordVariable: 'hello123')])
sh "sudo curl -u ${willy11}:{$hello123} -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'" 

I don´t know how to pass or define values of usernameVariable and passwordVariable to use on the curl command. The way it is now, I get on output script:

java.lang.IllegalStateException: **There is no body to invoke**
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:95)

enter image description here

How can I achieve this? the credential plug in, I read that supposedly puts *** on the output of the script, is this how this work? should I declare "will11" and "hello123" elsewere and use as env variables?

Thank you.

def call(body) {

def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

def artifactName = 'extractor'
def artifactExt = '.war'
def artifactVersion = '0.0.1'

def buildPath = 'target/'
def warFile = artifactName + '-' + artifactVersion + artifactExt
def warPath = buildPath + warFile
def warNoVersion = artifactName + artifactExt

def deployPath = '/var/lib/tomcat8/webapps/'
def deployFile = deployPath + warNoVersion

node {
    // Clean workspace before doing anything
    //deleteDir()

    try {

        stage ('Code Checkout') {
            git branch: 'master',
                credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
                url: 'ssh://[email protected]/xxxxxxx/xxxxxxxctor'
        }


stage ('Configure Application') {
            configFileProvider([configFile(fileId: config.confiFileId, variable: 'CONFIG_FILE_PATH')]) {
                sh 'cp $CONFIG_FILE_PATH resources/config.properties'
            }
            sh "echo 'job_name: $JOB_NAME' > WebContent/version.txt"
            sh "echo 'job_number: $BUILD_NUMBER' >> WebContent/version.txt"
        }

        stage ('Run Unitests') {
            sh 'mvn test'
        }

        /*stage ('SonarQube analysis') {
          withSonarQubeEnv('xxxxxxxxxxxxxxxx) {
            sh 'mvn sonar:sonar'
          }  
        }*/

        stage ('Compile and Build WAR') {
            sh 'mvn clean compile war:war'
        }

        stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
            sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 

        }




    } catch (err) {
        notifyBuild('FAILURE', config.slackChannel)
        throw err
    }
}

Upvotes: 0

Views: 6547

Answers (1)

Leonardo Vitali
Leonardo Vitali

Reputation: 305

When you use the credentials binding plugin the credentials will be bound to environment variables and the code to be executed must be inside the curly braces of the withCredentials statement, this is what we've missed.

So use:

 stage ('Upload war to Artifactory') {
       withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
           sh ("sudo curl -u $USER:$PASSWORD -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'")
       }
 }

instead of:

 stage ('Upload war to Artifactory') {
     withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
         sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 

 }

Upvotes: 1

Related Questions