In general, I create credentials in Jenkins -> credentials page to access the credential in the pipeline.
How to create Credentials in Jenkins
1. Open Credential page (Jenkins --> Credential)
2. Create a credential with Username and password and define a valid ID (For Example:myCredentialId)
How to access the credential using withCredentials in the pipeline
pipeline {
agent any
stages {
stage("Access the credentials") {
steps {
script {
withCredentials([[
$class:'UsernamePasswordMultiBinding',
credentialsId: 'myCredentialId',
usernameVariable:'username',
passwordVariable:'token'
]]) {
sh '''
curl -u ${username}:${token} -L <replace your git URL> -o master.txt
cat master.txt
'''
}
}
}
}
}
}