我如何从Jenkins推送git?

STN*_*STN 6 jenkins jenkins-plugins

以下代码是Jenkins中的"Execute Shell"构建步骤.该作业来自包含文件的仓库ranger-policies/policies.json.我想要做的是更新该文件(在这种情况下使用curl命令),然后将更改提交到源代码控制并更新远程仓库.通过SSH使用SSH密钥,作业配置页面的"源代码管理"部分中的远程仓库成功提取作业.但是,当作业到达"执行Shell"步骤中的"git push origin master"行时,我收到一个Permission denied (publickey)错误,好像那些允许我成功提取回购的相同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
Run Code Online (Sandbox Code Playgroud)

STN*_*STN 10

我最终搞清楚如何让它发挥作用.解决方案涉及使用SSH代理插件.这是一个逐步描述我是如何做到的,希望它可以帮助其他人:

  1. 首先,创建一个新的管道作业.
  2. 然后,正如Jenkins文档中的这篇文章中所暗示的那样,转到主屏幕以获取新的管道作业,然后单击"Pipeline Syntax".选择"git:Git"作为"Sample Step",然后在"Repository URL"字段中输入要推送到的git repo.然后从"Credentials"下拉列表中为该repo选择相应的有效SSH密钥.一切都应该看起来像这样: 在此输入图像描述 抓取"credentialsId"的值,在上面的屏幕截图中以红色突出显示.你以后需要它.

  3. 安装"Workspace Cleanup Plugin"(https://wiki.jenkins.io/display/JENKINS/Workspace+Cleanup+Plugin,可选)和"SSH Agent插件"(https://jenkins.io/doc/pipeline/步骤/ ssh-agent /,不是可选的,这个过程需要工作).

  4. 现在回到你的新管道的工作,然后点击"配置",这将带你到你定义作业的画面.将以下代码放入"管道"部分("定义"应设置为"管道脚本"):https://gist.github.com/ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8
  5. 将您的"credentialsId"放入上面Jenkins文件中的相应位置,并修复repo名称以定位您想要的repo,您​​应该很高兴.

相关文件:

  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