将工作区中的修改文件推送到Github [英] Pushing a modified file in workspace to Github

查看:167
本文介绍了将工作区中的修改文件推送到Github的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的Jenkins管道构建的一部分,我检查我的回购(复制到我可以看到的工作空间)。然后我修改了我的工作空间中的一个文件,然后我想要推回到我的Github回购。

 节点{
阶段'更新文件'
env.WORKSPACE = pwd()
文件文件=新文件($ {env.WORKSPACE} /ios.podspec);
fileText = file.text;
regex =(spec.version \\s。* \ $);
fileText = fileText.replaceAll(regex,spec.version ='$ {VERSION}'\\\
.trim());
file.write(fileText);

}

如何获取该文件并将其重新提交至我的Git repo?

解决方案

  shgit checkout $ branch
sh git添加<你的文件>
shgit commit -m'...'
shgit push $ url $ branch

棘手的部分是使用相关凭据设置url
我正在使用此方法 -

  DEF getRemoteUrlWithCredentials(credentialsId){
withCredentials([[$类: 'UsernamePasswordMultiBinding',credentialsId:credentialsId,usernameVariable: 'GIT_USERNAME',passwordVariable: 'GIT_PASSWORD']]){
DEF scmUrl = scm.getUserRemoteConfigs()[0] .getUrl()
scmUrl = scmUrl.substring(scmUrl.indexOf( github.com))
返回https://开头$ {GIT_USERNAME}:$ {GIT_PASSWORD} @ $ {scmUrl}
}
}

其中credentialId是你的git credentialsId。您需要将 scm.getUserRemoteConfigs 添加到Manage Jenkins中的批准列表中 - >处理脚本批准中。



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

  def setupConfig(email,userName){
shgit config user.email $ email
shgit config user.name $ userName
}


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?

解决方案

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

这篇关于将工作区中的修改文件推送到Github的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆