Issue
I have created credentials (method: username and password)
in Jenkins with name wwe_hello
.
For test, i created pipeline with name test
:
pipeline {
agent {label 'slave1'}
environment {
CREDS = credentials("wwe_hello")
}
stages {
stage('WWE') {
steps {
sh 'echo "$CREDS"'
}
}
}
}
In result i have:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on slave1 in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: wwe_hello
Finished: FAILURE
Where, i have error. I am doing everything according to working examples and documentation. But i don't understand, why it does not work.
Solution
Look at the pipeline step documentation for withCredentials. You don't need to create the environment variable with env - withCredentials does it for you:
pipeline {
agent any
stages {
stage('only') {
steps {
withCredentials([
usernamePassword(
credentialsId: 'CRED',
usernameVariable: 'USER',
passwordVariable: 'PASS'
)]) {
sh '''
echo "The username is: ${USER}"
echo "The password is : ${PASS}"
'''
}
}
}
}
}
Answered By - Rich Duncan
Answer Checked By - David Marino (JavaFixing Volunteer)