secrets
Handling Secrets in Jenkins
-
Use Jenkins Credentials store (recommended)
- Username/password, secret text, SSH keys, file credentials.
- Add credentials via Jenkins UI or via Jenkins Configuration as Code.
-
Use
withCredentialsin pipelines:
withCredentials([string(credentialsId: 'MY_SECRET', variable: 'TOKEN')]) {
sh 'echo $TOKEN | some-command'
}
withCredentials([usernamePassword(credentialsId: 'REG_CREDS', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
sh 'echo $PASS | docker login -u $USER --password-stdin registry'
}
-
Avoid printing secrets to logs. Use
maskor credential bindings and avoidechoof secret variables. -
For file-based secrets, use file credentials and reference the path provided in the pipeline.
-
For Kubernetes deployments, prefer injecting secrets via Kubernetes Secrets and using service accounts.