In Jenkinsfile, I want to make an ssh key visible to all stages in the pipeline.
From the official document, I learned that:
environmentdirective is used to defy environment variables for used within Jenkinsfile- The scope of the variables defined depends on the placement of the
environmentdirective - One can set the some types of credentials inside
environmentdirective with the help of thecredentialshelper - The types of credentials supported by the helper are:
- Secret text
- Usernames and passwords
- Secret files
For other types of credentials, the document suggests using the snippet generator, which generates a step.
Example of an ssh key step
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_aws_to_bitbucket', keyFileVariable: 'BITBUCKET_PRV_KEY')]) {
// some block
}
This is meant to be used in a stage like:
pipeline {
agent {
// define agent details
}
stages {
stage('Example stage 1') {
steps {
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'jenkins-ssh-key-for-abc', \
keyFileVariable: 'SSH_KEY_FOR_ABC')]) {
//
}
withCredentials(bindings: [certificate(credentialsId: 'jenkins-certificate-for-xyz', \
keystoreVariable: 'CERTIFICATE_FOR_XYZ', \
passwordVariable: 'XYZ-CERTIFICATE-PASSWORD')]) {
//
}
}
}
stage('Example stage 2') {
steps {
//
}
}
}
}
Question
- If the steps are within a stage, are these credentials visible within other stages?
- If not, how to make these credentials global ~ visible within all stages
environment{}block with thecredentials()method provides an implicit globalwithCredentials– Ken Rachynski May 05 '23 at 21:04credentialsin theenvironment{}block was added in April 2017, which is before I wrote this answer, but it seems to have been poorly documented at the time, especially compared to thewithCredentialsstep which well predates Jenkins Pipelines. I'm editing my answer to improve its accuracy. – jayhendren May 11 '23 at 23:04