To checkout a Git SCM repository in a Jenkins pipeline using credentials, you can use the withCredentials block and the git step.
Here's an example of how to do this:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
withCredentials([usernamePassword(credentialsId: 'your-credential-id', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
git url: 'https://github.com/your/repo.git', branch: 'master', credentialsId: 'your-credential-id', username: "${GIT_USERNAME}", password: "${GIT_PASSWORD}"
}
}
}
// other stages here
}
}
In this example, we use the withCredentials block to define the credentials to be used for the Git checkout. The usernamePassword credential type is used, which requires a credential ID to be specified.
Then, within the steps block of the Checkout stage, we use the git step to checkout the repository. The url parameter specifies the URL of the repository, the branch parameter specifies the branch to checkout, and the credentialsId parameter specifies the ID of the credentials to use.
Finally, we use the ${GIT_USERNAME} and ${GIT_PASSWORD} variables to provide the username and password to the git step, respectively. These variables are set by the withCredentials block and contain the username and password values for the specified credentials ID.
Comments (0)