Pom*_*m12 27
目前还不可能,因为GitPublisher
插件(以前负责标记/合并/推送自由式作业的插件)尚未更新为与Jenkins管道兼容.您可以在管道插件兼容性页面和专用GitPublisher Jira问题上关注该问题.
所以看起来你唯一的选择是实际发现你的标记/合并命令...但是,请注意你仍然可以从一些Jenkins内置功能中受益,例如使用Git repo的凭据,这使得它成为可能.非常简单,然后根据您的需要进行标记/合并.
示例结账:
git url: "ssh://jenkins@your-git-repo:12345/your-git-project.git",
credentialsId: 'jenkins_ssh_key',
branch: develop
Run Code Online (Sandbox Code Playgroud)
然后标签/合并/推送将非常简单:
sh 'git tag -a tagName -m "Your tag comment"'
sh 'git merge develop'
sh 'git commit -am "Merged develop branch to master'
sh "git push origin master"
Run Code Online (Sandbox Code Playgroud)
我希望有一天GitPublisher将在管道兼容版本中发布,但是现在这个解决方法应该可以.
and*_*ala 20
如果您所使用的是git凭据,则可以使用此代理链接中的SSH代理插件:https://issues.jenkins-ci.org/browse/JENKINS-28335?focusCommentId = 260925&page = com.atlassian.jira. plugin.system.issuetabpanels%3Acomment-一个tabpanel#评论-260925
sshagent(['git-credentials-id']) {
sh "git push origin master"
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我被迫使用HTTPS。我通过以下方法解决了它:
然后我可以在那之后用git push推送东西。
像这样:
sh 'git config --global credential.helper cache'
sh 'git config --global push.default simple'
checkout([
$class: 'GitSCM',
branches: [[name: branch]],
extensions: [
[$class: 'CloneOption', noTags: true, reference: '', shallow: true]
],
submoduleCfg: [],
userRemoteConfigs: [
[ credentialsId: 'bitbucketUsernamePassword', url: cloneUrl]
]
])
sh "git checkout ${branch}" //To get a local branch tracking remote
Run Code Online (Sandbox Code Playgroud)
然后,我可以做类似的事情:
sh 'git push'
Run Code Online (Sandbox Code Playgroud)
是的!!经过几天的努力,我最终得到了这些对我有用的脚本化管道脚本的简单代码块。
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh("git push origin <local-branch>:<remote-branch>")
}
Run Code Online (Sandbox Code Playgroud)
好享受!!
小智 5
我不得不做一个类似的任务,并设法让它与这个变体一起工作:https : //issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=320383&page=com.atlassian.jira。 plugin.system.issuetabpanels%3Acomment-tabpanel#comment-320383
withCredentials([sshUserPrivateKey(credentialsId: 'ci', keyFileVariable: 'SSH_KEY')]) {
sh 'echo ssh -i $SSH_KEY -l git -o StrictHostKeyChecking=no \\"\\$@\\" > local_ssh.sh'
sh 'chmod +x local_ssh.sh'
withEnv(['GIT_SSH=./local_ssh.sh']) {
sh 'git push origin develop'
}
}
Run Code Online (Sandbox Code Playgroud)
而ci
是您在 Jenkins 中设置的凭据的 ID。当环境变量SSH_KEY
和 local_ssh.sh 添加到当前目录时,ssh 密钥的路径变得可用。
就我而言,我想通过 SSH 推送到 CodeCommit 存储库。sshagent
不起作用,因为它没有设置User
。这最终完成了工作:
withCredentials([sshUserPrivateKey(credentialsId: CODECOMMIT_CREDENTIALS_ID, keyFileVariable: 'SSH_KEY', usernameVariable: 'SSH_USER')]) {
withEnv(["GIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=no -o User=${SSH_USER} -i ${SSH_KEY}"]) {
sh 'git push ${CODECOMMIT_URL} ${COMMIT_ID}:refs/heads/${BRANCH}'
}
}
Run Code Online (Sandbox Code Playgroud)
这个线程真的很有帮助。我的Jenkins凭据是用户名/密码,因此我使用了:
withCredentials([usernamePassword(credentialsId: 'fixed', usernameVariable: 'username', passwordVariable: 'password')]){
{
sh("git push http://$username:$password@git.corp.mycompany.com/repo")
}
Run Code Online (Sandbox Code Playgroud)
用户名和密码在日志中均被遮盖:
+ git push http://****:****@git.corp.mycompany.com/repo
Run Code Online (Sandbox Code Playgroud)
小智 5
在基本 Jenkins 和 git 功能的帮助下找到了解决方案
stage('Git Push'){
steps{
script{
GIT_CREDS = credentials(<git creds id>)
sh '''
git add .
git commit -m "push to git"
git push https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@bitbucket.org/<your repo>.git <branch>
'''
}
}
}
Run Code Online (Sandbox Code Playgroud)
https://www.jenkins.io/doc/book/using/using-credentials/ git push 命令中的用户名和密码