fs314
fs314

Reputation: 109

Jenkins - translate withCredentials into declarative style syntax

I have a jenkins pipeline which is written using a scripted syntax and I need to make this into a new pipeline using a declarative style.

This is my fist project with jenkins and I am stuck at how to translate the withCredentials syntax in declarative jenkins.

The original(scripted) pipeline looks something like this:

stage('stage1') {
     steps {
        script {
            withCredentials([usernamePassword(credentialsId: CredentialsAWS, passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
                parallel (
                    something: {
                         sh 'some commands where there is no reference to the above credentials'
                    }
                )
            }
        }
     }
}

So far, I have set the credentials in question as an environment variable but since in the original pipeline these are not referenced in the command but they just wrap around the command as 'withCredentials', I am not sure how to achieve the same result. Any idea how to do this?

Upvotes: 7

Views: 8551

Answers (2)

Dmitriy Tarasevich
Dmitriy Tarasevich

Reputation: 1242

First of all take a look at official documentation

For your case pipeline will look like:

pipeline {
    agent any
    environment { 
        YOUR_CRED = credentials('CredentialsAWS') 
    }
    stages {
        stage('Call username and password from YOUR_CRED') {
            steps {
                echo "To call username use ${YOUR_CRED_USR}"
                echo "To call password use ${YOUR_CRED_PSW}"
            }
        }
    }
}

Upvotes: 14

Muzaffar khan
Muzaffar khan

Reputation: 37

  Jenkinsfile (Declarative Pipeline)
   pipeline {
      agent any
        parameters {
string(name: 'STATEMENT', defaultValue: 'hello; ls /', description: 'What should I say?')
 }
  stages {
    stage('Example') {
     steps {
    /* CORRECT */
    sh('echo ${STATEMENT}')
      }
     }
    }
   }

visit here

Upvotes: -2

Related Questions