git 如何在成功构建时使 Gitlab runner 将代码合并到一个分支中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42113402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make Gitlab runner merge code into a branch on a successful build
提问by StLia
Well the title is pretty much self-explanatory.
嗯,标题是不言自明的。
In summary, I want a branch (i.e. dev) to be merged to another branch (i.e. production) IF the build is successful.
总之,如果构建成功,我希望将一个分支(即 dev)合并到另一个分支(即生产)。
回答by Jakub Kania
The easiest solution is to make a Merge Request and click the "Merge When Pipeline Succeeds" button, this will merge the branch after the build. This is the one I would recommend.
最简单的解决方案是发出合并请求并单击“管道成功时合并”按钮,这将在构建后合并分支。这是我会推荐的。
Below is the working solution that I do not recommend for an automatic merge. It requires you to create a deploy key with write access and save the private key as a project variable GITLAB_DEPLOY KEY
, also do ssh-keyscan
on the server and save it to GITLAB_PUBLIC_KEY
variable.
以下是我不推荐用于自动合并的工作解决方案。它要求您创建一个具有写访问权限的部署密钥并将私钥保存为项目变量GITLAB_DEPLOY KEY
,也可以ssh-keyscan
在服务器上进行并将其保存到GITLAB_PUBLIC_KEY
变量中。
mergetomaster:
stage: deploy
image: alpine
only:
- dev
script:
- apk add --update git openssh-client
- mkdir ~/.ssh
- echo $GITLAB_DEPLOY_KEY > ~/.ssh/id_rsa
- chmod 400 ~/.ssh/id_rsa
- echo $GITLAB_PUBLIC_KEY > ~/.ssh/known_hosts
// Steal the identity of person that triggered the build
- git config --global user.email "$GITLAB_USER_EMAIL"
- git config --global user.name "$GITLAB_USER_ID"
- git remote set-url origin <ssh-repository-url>
- git checkout master
- git reset --hard origin/master
- git merge $CI_BUILD_REF
- git push origin master
回答by karser
I tried @jakub-kania solution but I was always getting id_rsa invalid format
. I think that gitlab secret variables are screwed somehow.
我尝试过 @jakub-kania 解决方案,但我总是得到id_rsa invalid format
. 我认为 gitlab 秘密变量不知何故被搞砸了。
I made it working by directly passing the deployment key into ssh-add without creating ssh keys. Here is working solution:
我通过将部署密钥直接传递到 ssh-add 而不创建 ssh 密钥来使其工作。这是工作解决方案:
merge to master:
stage: deploy
image: alpine
only:
- dev-branch
before_script:
- apk add --update git openssh-client
- mkdir ~/.ssh
- ssh-keyscan -p 2222 <gitlab.domain.com> > ~/.ssh/known_hosts
- eval `ssh-agent -s`
- ssh-add <(echo "$GITLAB_DEPLOY_KEY")
- ssh -T git@<gitlab.domain.com> -p 2222
- git config --global user.email "$GITLAB_USER_EMAIL"
- git config --global user.name "$GITLAB_USER_ID"
- git remote set-url origin ssh://git@<gitlab.domain.com>:2222/path/to/repo.git
script:
- git checkout master
- git reset --hard origin/master
- git merge $CI_BUILD_REF
- git push origin master
回答by Fairy
There is no easy way to do this as of GitLab version 8.15. The only way to do this is to leverage the API and webhooks.
从 GitLab 8.15 版开始,没有简单的方法可以做到这一点。做到这一点的唯一方法是利用 API 和 webhook。
This is the basic gist of what you have to do:
这是您必须执行的操作的基本要点:
1.Create a webhookwhich hooks push events.
2.Check if the push belongs to the branch you want to do the merging on.
2.检查推送是否属于您要合并的分支。
3.Create a mergerequest and immediately accept itwith the option "merge_when_build_succeeds": true
.
3.创建一个合并请求并立即使用选项接受它"merge_when_build_succeeds": true
。
This way it will merge the the branch, should the build succeed. Not really the most comfortable thing to setup but it should work.
这样,如果构建成功,它将合并分支。设置起来并不是最舒服的事情,但它应该可以工作。