将工作空间中的已修改文件推送到Github

时间:2016-12-07 09:50:36

标签: git jenkins jenkins-pipeline

作为我的Jenkins管道构建的一部分,我检查了我的仓库(我可以看到复制到我的工作区)。然后我在我的工作区中修改一个文件,然后我想将其推回到我的Github仓库。我只是在podspec文件中更新版本号。

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);

}

如何获取该文件并将其推回到我的Git仓库?

1 个答案:

答案 0 :(得分:1)

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

棘手的部分是使用相关凭据设置网址 我正在使用这种方法 -

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}"
    }
}

其中credentialId是你的git credentialsId。您需要将scm.getUserRemoteConfigs添加到Manage Jenkins中的批准列表 - &gt;在流程脚本批准中。

最后一部分 - 我不确定是否有必要,但也许你需要设置config user.email和user.name - &gt;

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