Tag a Repo from a Jenkins Workflow Script

后端 未结 5 615
孤城傲影
孤城傲影 2020-12-15 22:08

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

5条回答
  •  悲哀的现实
    2020-12-15 22:27

    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.

提交回复
热议问题