以下代码是 Jenkins 中的“执行 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
我最终弄清楚了如何让它发挥作用。解决方案涉及使用 SSH 代理插件。以下是我的具体操作步骤,希望对其他人有所帮助:
- 首先,创建一个新的管道作业。
Then, as hinted at in this https://gist.github.com/blaisep/eb8aa720b06eff4f095e4b64326961b5#file-jenkins-pipeline-git-cred-md post from Jenkins' documentation, go to the home screen for your new pipeline job, and click on "Pipeline Syntax." Choose "git: Git" as the "Sample Step, and enter the git repo you want to push to in the "Repository URL" field. Then choose the corresponding valid SSH keys for that repo from the "Credentials dropdown." Everything should look like this:
Grab the value of "credentialsId", highlighted with red in the above screenshot. You'll need it later.
安装“工作区清理插件”(https://wiki.jenkins.io/display/JENKINS/Workspace+Cleanup+Plugin https://wiki.jenkins.io/display/JENKINS/Workspace+Cleanup+Plugin,可选)和“SSH代理插件”(https://jenkins.io/doc/pipeline/steps/ssh-agent/ https://jenkins.io/doc/pipeline/steps/ssh-agent/,不是可选的,是此过程正常运行所必需的)。
- 现在返回到新的管道作业并点击“配置”,这将带您到定义作业的屏幕。将以下代码放入“Pipeline”部分(“Definition”应设置为“Pipeline script”):https://gist.github.com/ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8 https://gist.github.com/ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8
- 将您的“credentialsId”放入上述 Jenkinsfile 中的适当位置,并修复存储库名称以定位您想要的存储库,然后就可以开始了。
相关文档:
- https://jenkins.io/doc/pipeline/examples/#push-git-repo https://jenkins.io/doc/pipeline/examples/#push-git-repo
- https://gist.github.com/blaisep/eb8aa720b06eff4f095e4b64326961b5#file-jenkins-pipeline-git-cred-md https://gist.github.com/blaisep/eb8aa720b06eff4f095e4b64326961b5#file-jenkins-pipeline-git-cred-md
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)