Tag a Repo from a Jenkins Workflow Script

后端 未结 5 616
孤城傲影
孤城傲影 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:23

    If your git password contains special characters such as "%", ":", "@", or "/", passing ${env.GIT_PASSWORD} as part of the git url ie https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@ without doing any encoding is likely to result in an Invalid username or password error.

    To save any hassle using an inline credential.helper is a better way to go however the suggestion of !echo password=\$GIT_PASSWORD; echo' will result in a warning in your build logs warning: invalid credential line: get as the credential.helper is passed an argument to indicate the required operation (get,store,erase). In this case the credential helper is trying to interpret the get operation as a credential input. Valid inputs are protocol,host,path,username,password,url. See https://git-scm.com/docs/git-credential#IOFMT

    A better inline credential.helper would be !f() { echo password=\$GIT_PASSWORD; }; f This way the credential.helper operation get is ignored.

    Full example:

    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 '!f() { echo password=\$GIT_PASSWORD; }; f'")
        sh("GIT_ASKPASS=true ${git} push origin --tags")    
      }
    } finally {
        sh("${git} config --unset credential.username")
        sh("${git} config --unset credential.helper")
    }
    

提交回复
热议问题