是否可以 Git 使用 Jenkins 管道合并/推送
Is it possible to Git merge / push using Jenkins pipeline
我正在尝试使用 Jenkinsfile 创建 Jenkins 工作流程。我想让它做的就是监视 'develop' 分支的变化。当发生变化时,我希望它成为 git 标记并合并到 master。我正在使用 GitSCM Step,但它似乎唯一支持的是 git 克隆。我不想 shell 出去做标签/合并,但我看不出解决它的办法。有谁知道这是否可能?我正在为我的 Git 服务器使用 BitBucket(本地)。
目前无法实现,因为 GitPublisher
插件(以前负责 tagging/merging/pushing 自由式作业的插件尚未更新为与 Jenkins 管道兼容。您可以在 pipeline plugins compatibility page and the dedicated GitPublisher Jira issue.
上关注该问题
所以看来你唯一的选择就是 shell 执行你的 tag/merge 命令...但是请注意,你仍然可以从一些 Jenkins 内置功能中受益,例如为您的 Git 存储库使用凭据,这使得根据您的需要进行标记/合并变得非常简单。
签出示例:
git url: "ssh://jenkins@your-git-repo:12345/your-git-project.git",
credentialsId: 'jenkins_ssh_key',
branch: develop
然后标记/合并/推送将非常简单:
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"
我希望有一天 GitPublisher 将以管道兼容的版本发布,但目前这种解决方法应该可以。
如果您要的是 git 凭据,您可以像这样使用 SSH 代理插件 link:https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=260925&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-260925
sshagent(['git-credentials-id']) {
sh "git push origin master"
}
就我而言,我被迫使用 HTTPS。我通过以下方式解决了它:
- 正在创建 username/password 凭证 bitbucketUsernamePassword.
- 使用该凭据结帐。
- 结账前设置credential.helper。
- 正在执行 git 结帐分支 以获取本地分支跟踪远程。
然后我可以用 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
然后我可以做这样的事情:
sh 'git push'
我不得不做一个类似的任务,并设法让它与这个变体一起工作: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'
}
}
而 ci
是您在 Jenkins 中设置的凭证的 ID。当环境变量 SSH_KEY
和 local_ssh.sh 添加到当前目录时,ssh 密钥的路径变得可用。
是的,是的!!
经过几天的努力,我最终得到了这些对我有用的脚本化管道脚本的简单代码块。
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh("git push origin <local-branch>:<remote-branch>")
}
尽情享受吧!!
就我而言,我想通过 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}'
}
}
这个话题真的很有帮助。我的 Jenkins 凭据是 username/password 所以我使用了:
withCredentials([usernamePassword(credentialsId: 'fixed',
usernameVariable: 'username',
passwordVariable: 'password')]){
sh("git push http://$username:$password@git.corp.mycompany.com/repo")
}
用户名和密码在日志中都被隐藏了:
+ git push http://****:****@git.corp.mycompany.com/repo
在 blue ocean 上使用 sshagent(使用 https 授权)得到这个工作
sshagent(credentials: ["406ef572-9598-45ee-8d39-9c9a227a9227"]) {
def repository = "git@" + env.GIT_URL.replaceFirst(".+://", "").replaceFirst("/", ":")
sh("git remote set-url origin $repository")
sh("git tag --force build-${env.BRANCH_NAME}")
sh("git push --force origin build-${env.BRANCH_NAME}")
}
Litty Philips 的回答帮了我大部分的忙,但我还需要定义 GIT_SSH_COMMAND。
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh """
GIT_SSH_COMMAND = "ssh -i $SSH_KEY"
git push origin <local-branch>:<remote-branch>
"""
}
在基本 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>
'''
}
}
}
最近,git 插件添加了这个命令:
stage('git push') {
steps {
withCredentials([
gitUsernamePassword(credentialsId: 'github', gitToolName: 'Default')
]) {
sh "git push"
}
}
}
找不到相关文档。我在任何管道作业中的“管道语法”助手中发现了这一点。
我正在尝试使用 Jenkinsfile 创建 Jenkins 工作流程。我想让它做的就是监视 'develop' 分支的变化。当发生变化时,我希望它成为 git 标记并合并到 master。我正在使用 GitSCM Step,但它似乎唯一支持的是 git 克隆。我不想 shell 出去做标签/合并,但我看不出解决它的办法。有谁知道这是否可能?我正在为我的 Git 服务器使用 BitBucket(本地)。
目前无法实现,因为 GitPublisher
插件(以前负责 tagging/merging/pushing 自由式作业的插件尚未更新为与 Jenkins 管道兼容。您可以在 pipeline plugins compatibility page and the dedicated GitPublisher Jira issue.
所以看来你唯一的选择就是 shell 执行你的 tag/merge 命令...但是请注意,你仍然可以从一些 Jenkins 内置功能中受益,例如为您的 Git 存储库使用凭据,这使得根据您的需要进行标记/合并变得非常简单。
签出示例:
git url: "ssh://jenkins@your-git-repo:12345/your-git-project.git",
credentialsId: 'jenkins_ssh_key',
branch: develop
然后标记/合并/推送将非常简单:
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"
我希望有一天 GitPublisher 将以管道兼容的版本发布,但目前这种解决方法应该可以。
如果您要的是 git 凭据,您可以像这样使用 SSH 代理插件 link:https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=260925&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-260925
sshagent(['git-credentials-id']) {
sh "git push origin master"
}
就我而言,我被迫使用 HTTPS。我通过以下方式解决了它:
- 正在创建 username/password 凭证 bitbucketUsernamePassword.
- 使用该凭据结帐。
- 结账前设置credential.helper。
- 正在执行 git 结帐分支 以获取本地分支跟踪远程。
然后我可以用 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
然后我可以做这样的事情:
sh 'git push'
我不得不做一个类似的任务,并设法让它与这个变体一起工作: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'
}
}
而 ci
是您在 Jenkins 中设置的凭证的 ID。当环境变量 SSH_KEY
和 local_ssh.sh 添加到当前目录时,ssh 密钥的路径变得可用。
是的,是的!! 经过几天的努力,我最终得到了这些对我有用的脚本化管道脚本的简单代码块。
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh("git push origin <local-branch>:<remote-branch>")
}
尽情享受吧!!
就我而言,我想通过 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}'
}
}
这个话题真的很有帮助。我的 Jenkins 凭据是 username/password 所以我使用了:
withCredentials([usernamePassword(credentialsId: 'fixed',
usernameVariable: 'username',
passwordVariable: 'password')]){
sh("git push http://$username:$password@git.corp.mycompany.com/repo")
}
用户名和密码在日志中都被隐藏了:
+ git push http://****:****@git.corp.mycompany.com/repo
在 blue ocean 上使用 sshagent(使用 https 授权)得到这个工作
sshagent(credentials: ["406ef572-9598-45ee-8d39-9c9a227a9227"]) {
def repository = "git@" + env.GIT_URL.replaceFirst(".+://", "").replaceFirst("/", ":")
sh("git remote set-url origin $repository")
sh("git tag --force build-${env.BRANCH_NAME}")
sh("git push --force origin build-${env.BRANCH_NAME}")
}
Litty Philips 的回答帮了我大部分的忙,但我还需要定义 GIT_SSH_COMMAND。
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh """
GIT_SSH_COMMAND = "ssh -i $SSH_KEY"
git push origin <local-branch>:<remote-branch>
"""
}
在基本 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>
'''
}
}
}
最近,git 插件添加了这个命令:
stage('git push') {
steps {
withCredentials([
gitUsernamePassword(credentialsId: 'github', gitToolName: 'Default')
]) {
sh "git push"
}
}
}
找不到相关文档。我在任何管道作业中的“管道语法”助手中发现了这一点。