Interview Prep/Jenkins/Declarative pipeline syntax

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.

All Jenkins questions Roadmap: Jenkins

More on Declarative pipeline syntax