小编典典

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

jenkins

作为我的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存储库中?


阅读 308

收藏
2020-07-25

共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凭据Id。您将需要添加scm.getUserRemoteConfigs到Manage
Jenkins->处理中脚本批准中的批准列表。

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

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