I'm currently trying to tag a repo from a Jenkins Workflow script. I've tried using a sh
step but this runs into problems because of credentials not being set.
fatal: could not read Username for 'https://<repo>': Device not configured
Is there an existing step that can be used either to tag a repo or to get round the credentials problem?
Yes, we can add tags directly to GitHub. To sync the same with your local repository, you need to pull the changes using Git.
I've managed to get this working by using the withCredentials
step provided by the credentials binding plugin.
Its not great because it involved specifying it all in the URL but these values are masked in the console output.
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("git tag -a some_tag -m 'Jenkins'")
sh("git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO> --tags")
}
Here's an alternative that does not require knowing the URL of the remote:
try {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("${git} config credential.username ${env.GIT_USERNAME}")
sh("${git} config credential.helper '!echo password=\$GIT_PASSWORD; echo'")
sh("GIT_ASKPASS=true ${git} push origin --tags")
}
} finally {
sh("${git} config --unset credential.username")
sh("${git} config --unset credential.helper")
}
This works by having git read the username from the config, and let the credential helper supply the password only. The extra echo
at the end is for making the command that git passes as an argument to the helper not end up on the same line as the password.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With