Discover how to keep your secrets safe and your Jenkins pipelines smooth with one simple block!
Why WithCredentials block usage in Jenkins? - Purpose & Use Cases
Imagine you need to run a Jenkins pipeline that accesses a private server or repository. You have to manually type your username and password every time or store them in plain text files on the build machine.
This manual way is risky and slow. Typing credentials each time wastes time and can cause typos. Storing passwords in plain text files is unsafe and can lead to leaks if someone accesses the machine.
The withCredentials block in Jenkins securely injects your secret credentials into the pipeline only when needed. It keeps passwords hidden and automatically cleans them up after use, making your pipeline safer and easier to manage.
sh 'git clone https://username:password@private-repo.git'withCredentials([usernamePassword(credentialsId: 'my-creds', usernameVariable: 'USER', passwordVariable: 'PASS')]) { sh 'git clone https://${USER}:${PASS}@private-repo.git' }
You can safely automate tasks that require secrets without risking exposure or manual errors.
A team automates deployment to a cloud server. Using withCredentials, the pipeline accesses the server with stored SSH keys securely, avoiding manual password entry and keeping keys safe.
Manual credential handling is slow and unsafe.
withCredentials securely injects secrets only when needed.
This makes pipelines safer and easier to automate.