【问题标题】:How do I push to git from Jenkins?我如何从 Jenkins 推送到 git?
【发布时间】:2017-11-20 20:46:47
【问题描述】:

以下代码是 Jenkins 中的“执行 Shell”构建步骤。该作业从包含文件ranger-policies/policies.json 的存储库中提取。我想做的是更新该文件(在这种情况下使用 curl 命令),然后将更改提交到源代码管理并更新远程存储库。该作业使用 SSH 密钥通过 SSH 从作业配置页面的“源代码管理”部分中的远程存储库成功提取。但是,当作业到达“执行 Shell”步骤中的“git push origin master”行时,我收到 Permission denied (publickey) 错误,好像那些允许我成功提取 repo 的 SSH 密钥在想要推送时的“执行 Shell”步骤。

curl -X GET --header "text/json" -H "Content-Type: text/json" -u user:pass "http://my-url.com/exportJson" > ranger-policies/policies.json

git add ranger-policies/policies.json
git commit -m "udpate policies.json with latest ranger policies `echo "$(date +'%Y-%m-%d')"`"
git push origin master

【问题讨论】:

    标签: jenkins jenkins-plugins


    【解决方案1】:

    我最终弄清楚了如何让它发挥作用。该解决方案涉及使用 SSH 代理插件。以下是描述我如何做到这一点的分步说明,希望对其他人有所帮助:

    1. 首先,创建一个新的管道作业。
    2. 然后,正如 Jenkins 文档中的 this 帖子中所暗示的,转到新管道作业的主屏幕,然后单击“管道语法”。选择“git:Git”作为“示例步骤”,然后在“存储库 URL”字段中输入要推送到的 git 存储库。然后从“凭据下拉列表”中为该存储库选择相应的有效 SSH 密钥。一切都应该看起来像这样: 获取“credentialsId”的值,在上面的屏幕截图中以红色突出显示。你稍后会需要它。

    3. 安装“Workspace Cleanup Plugin”(https://wiki.jenkins.io/display/JENKINS/Workspace+Cleanup+Plugin,可选)和“SSH 代理插件”(https://jenkins.io/doc/pipeline/steps/ssh-agent/,非可选,此过程运行所必需)。

    4. 现在回到您的新管道作业并点击“配置”,这将带您进入您定义作业的屏幕。将以下代码放入“管道”部分(“定义”应设置为“管道脚本”):https://gist.github.com/ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8
    5. 将您的“credentialsId”放入上述 Jenkinsfile 中的适当位置,并修改存储库名称以定位您想要的存储库,您应该一切顺利。

    相关文档:

    1. https://jenkins.io/doc/pipeline/examples/#push-git-repo
    2. https://gist.github.com/blaisep/eb8aa720b06eff4f095e4b64326961b5#file-jenkins-pipeline-git-cred-md
    3. https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=269000&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-269000

    【讨论】:

      【解决方案2】:

      根据gist,您需要设置远程源网址:

      git remote set-url origin git@github.com:username/your-repository.git

      【讨论】:

      • 我刚刚尝试根据您的建议添加该行,但我得到了相同的错误,就好像没有任何改变一样。我认为这个问题在某种程度上与在“执行外壳”步骤运行的上下文中不可用的 SSH 密钥有关。我可以将有效的 SSH 密钥直接复制到 Execute Shell 步骤中,但这会带来安全隐患。我希望能够使用 Jenkins 已经存储在其数据库中的有效 SSH 密钥——它允许我在配置页面的“源代码管理”面板中成功获取存储库。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 2014-01-06
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多