How does `withCredentials` differ from `credentials()` in `environment`?
IntermediateConceptDeclarative pipeline syntax
Answer
withCredentials binds secrets only for the enclosed block, supports more binding types and lets you choose variable names.
withCredentials([
usernamePassword(credentialsId: 'nexus', usernameVariable: 'NX_USER', passwordVariable: 'NX_PASS'),
file(credentialsId: 'kubeconfig-prod', variable: 'KUBECONFIG'),
sshUserPrivateKey(credentialsId: 'deploy-key', keyFileVariable: 'KEY'),
string(credentialsId: 'slack-webhook', variable: 'SLACK_URL')
]) {
sh 'kubectl --kubeconfig "$KUBECONFIG" get ns'
}
Narrow scope is safer: a secret bound at pipeline level is visible to every step and every script.
More on Declarative pipeline syntax
What is the minimum valid declarative pipeline?BeginnerWhat agent types exist in declarative pipelines?IntermediateWhen do you use `agent none` at the top level?IntermediateHow do nested sequential stages work and why are they useful?IntermediateList the `post` conditions and when each fires.IntermediateWhat are the most useful `when` conditions?Intermediate