You can also use the Jenkins credentials provider API to achieve this.
There are multiple advantages to using the credentials provider API rather than a script step:
- The credentials in your Jenkins pipeline script (the 'what') are decoupled from the logic that looks them up (the 'how'), keeping your scripts more clean and portable.
- You can have multiple credentials providers loaded if you need to read credentials from multiple locations.
- Credentials providers efficiently cache secret metadata, while ensuring that secret values stay out of memory until the moment they're needed.
- Jenkins automatically masks credential values that are printed in the build log, so it's harder to leak the values by accident.
- The Jenkins credentials UI and job builder UI show you which credentials are available, making it easier to craft job scripts.
It is true that there is some upfront complexity in setting up a credentials provider compared to a script step. However if you are using credentials in more than 1 or 2 scripts, or any of your credentials would have non-trivial consequences if they were mismanaged or leaked (e.g. Artifactory upload keys), I definitely think it's worth taking that cost now, in return for much easier credentials management and maintenance later.
See more info in the Jenkins documentation.
Example
If you want to use the credentials provider API with secrets that you've stored in Secrets Manager, you would use the AWS Secrets Manager Credentials Provider plugin. (Disclaimer: I am the maintainer of that plugin.)
First we install the AWS Secrets Manager Credentials Provider plugin on Jenkins, and grant Jenkins IAM access to Secrets Manager.
Then we upload a Jenkins username with password credential called 'artifactory' to Secrets Manager, which contains the username 'joe' (non-sensitive information) and the password 'supersecret' (sensitive information).
aws secretsmanager create-secret \
--name 'artifactory' \
--secret-string 'supersecret' \
--description 'Acme Corp Artifactory user' \
--tags 'Key=jenkins:credentials:username,Value=joe' 'Key=jenkins:credentials:type,Value=usernamePassword'
Then we bind the 'artifactory' credential in our Jenkinsfile.
pipeline {
agent any
environment {
ARTIFACTORY = credentials('artifactory')
}
stages {
stage('Foo') {
steps {
// Three environment variables are now available to use however you want:
//
// ARTIFACTORY=joe:supersecret
// ARTIFACTORY_USR=joe
// ARTIFACTORY_PSW=supersecret
sh './deploy'
}
}
}
}