4

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.

1 Answer 1

7

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'
    }
Sign up to request clarification or add additional context in comments.

12 Comments

thx for the advise, I will give it a try, but can you tell me how the git command would have too look like if I had to add a tag to an ssh type link?
@Elydasian "add a tag to an SSH type link"? What do you mean? Like a Host entry in a ~/.ssh/config file?
well, if I would use the line that I posted above in my question would push a tag to my git repo (using http protocol), how would the same line would look like if I were to use the ssh protocol? (I hope this makes sense :))
@Elydasian OK, got it. the set-url would be: git remote set-url --push origin [email protected]/scme/ci/ci.git
thx, that worked! Also, I am looking into this answer stackoverflow.com/questions/44377238/…, posted by tianzhipeng, would you mind taking a look? I am close to implementing the ssh key, but I dont know what is the keyFileVariable: 'keyfile' used for?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.