bookmark_borderVariables and Credentials in Jenkins

Variables in Jenkins with environment

New Item – Pipeline

pipeline {
    agent any
    environment {
        global = 'global'
    }
    stages {
        stage('local_variable') {
            environment {
                local_one = 'local_one'
            }
            steps {
                echo local_one
                echo global
            }
        }
        stage('global_variable') {
            environment {
                local_two = 'local_two'
            }
            steps {
                echo local_two
                echo global
            }
        }
    }
}
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/var
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (local_variable)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
local_one
[Pipeline] echo
global
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (global_variable)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
local_two
[Pipeline] echo
global
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Credentials in Jenkins

Create Credentials

Manage Jenkins – Manage Credentials – System – Global credentials – Add Credentials

Use Credentials

New Item – Pipeline

Pipeline syntax generator

  • Dashboard – job_name – Pipeline Syntax
  • Sample Step : withCredentials: Bind credentials to variables
  • Bindings Username and password
pipeline {
    agent any
    stages {
        stage('credentials') {
            steps {
                withCredentials([usernamePassword(credentialsId: '5a04e3aa-b4bc-45f5-8a54-bde5363e7906', passwordVariable: 'user_password', usernameVariable: 'user_name')]) {
                    echo user_name
                    echo user_password
                }
            }
        }
    }
}
[Pipeline] Start of Pipeline (hide)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/credential
[Pipeline] {
[Pipeline] stage
[Pipeline] { (credentials)
[Pipeline] withCredentials
Masking supported pattern matches of $user_name or $user_password
[Pipeline] {
[Pipeline] echo
****
[Pipeline] echo
****
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Jenkins hide the user credentials, but we can use to access end point using the credentials

ANOTE.DEV