Pushing a modified file in workspace to Github

房东的猫 提交于 2019-12-25 08:29:25

问题


As part of my Jenkins pipeline build I checkout my repo (which copies to my workspace I can see). I then modify a file in my workspace, which I then would like to push back up to my Github repo. I am just updating a version number in a podspec file.

node {
  stage 'Update File'
   env.WORKSPACE = pwd()
   File file = new File("${env.WORKSPACE}/ios.podspec");
   fileText = file.text;
   regex = "(spec.version\\s.*\$)";
   fileText = fileText.replaceAll(regex, "spec.version               =   '${VERSION}'\n".trim());
   file.write(fileText);

}

How can I take that file and push it back up to my Git repo?


回答1:


sh "git checkout $branch"
sh "git add <your file>"
sh "git commit -m '...'"
sh "git push $url $branch"

The tricky part is to set the url with the relevant credentials I am using this method -

def getRemoteUrlWithCredentials(credentialsId) {
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
        def scmUrl = scm.getUserRemoteConfigs()[0].getUrl()
        scmUrl = scmUrl.substring(scmUrl.indexOf("github.com"))
        return "https://${GIT_USERNAME}:${GIT_PASSWORD}@${scmUrl}"
    }
}

where credentialId is your git credentialsId. You will need to add scm.getUserRemoteConfigs to the approve list in Manage Jenkins -> In Process Script Approval.

And last part - I am not sure if it's necessary but maybe you'd need to set the config user.email and user.name ->

def setupConfig(email, userName) {
    sh "git config user.email $email"
    sh "git config user.name $userName"
}


来源:https://stackoverflow.com/questions/41014216/pushing-a-modified-file-in-workspace-to-github

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!