I have a bat script that is run on my jenkins pipeline.
It is used to tag a specific commit of my branch, and push it back to git. Looks like this:
"scriptCommand": https://%C_USERNAME%:%C_PASSWORD%@bitbucket.psr.io/scme/ci/ci.git\ngit push origin TAG1\ngit remote set-url --push origin https://bitbucket..psr.io/scme/ci/ci.git"
That command is made using the variables C_USERNAME, C_PASSWORD and the TAG1 The first two are taken from a usernamePassword type jenkins credentials, and are injected into the string, the TAG1 is a stimple string that should be used to get my git branch tagged
And that works, when I use the http link instead of the SSH link for my repo
I need to be able to handle the SSH link as well. For that, I researched on SSH private key, and turns out that the jenkins credentials have a specific field for it: SSH Username with private key
I made one such credential in jenkins, using my private key, and username
But now I am having trouble on how to create a batch line that works the same as the one above with the http link.
Needless to say, I have 0 exp on this matter.
Can some help me on this? And thank you for your time.
CodePudding user response:
The SSH URL would be like:
[email protected]/scme/ci/ci.git
But you might want to use a dedicated plugin, like publish over SSH, which you can include in a pipeline.
In your case:
git remote set-url --push origin [email protected]/scme/ci/ci.git
Using an SSH agent plugin
node {
sshagent (credentials: ['myKey']) {
sh 'git push [email protected]/scme/ci/ci.git aTag'
}
}
Assuming you have registered your private key in the Jenkins Global credentials (unrestricted), as an entry named 'myKey
'.
Using withCredentials
, as shown here, assuming Git 2.10 for GIT_SSH_COMMAND
:
withCredentials([sshUserPrivateKey(credentialsId: "myKey", keyFileVariable: 'key')]) {
//auth to git here then do some commands for example:
sh 'git commmit -am "hello my commit message'
sh 'GIT_SSH_COMMAND = "ssh -i $key"'
sh 'git push [email protected]/scme/ci/ci.git aTag'
}