我对 gitlab 和 gitlab CI 非常陌生,我已经建立了一个正在成功完成的管道。
我的主分支和开发分支受到保护,因此需要合并请求,以便组中的另一个开发人员可以在合并之前查看代码和评论。
我想知道是否可以在此管道的末尾生成此合并请求。 gitlab 存储库中是否有此设置,或者我是否必须创建一个脚本来实现此目的?
旁注:
就在发布这篇文章之前,我遇到了 gitlab 文档的这一部分
我在 ubuntu 18.04 上使用 gitlab-runner 11.0.0
为了实现我的简单需求,我只是在管道中添加了最后一个阶段,该阶段本质上执行改编自这篇文章的 bash 脚本。
编辑: 根据@Yuva 的要求
# Create a pull request on pipeline success
create_merge_request:
stage: createMR
tags:
- autoMR
script:
- 'echo Merge request opened by $GITLAB_USER_NAME '
- ~/commit.sh
并在commit.sh中
#!/bin/bash
# This script was adapted from:
# https://about.gitlab.com/2017/09/05/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/
# TODO determine URL from git repository URL
[[ $HOST =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"
# The branch which we wish to merge into
TARGET_BRANCH=develop;
# The user's token name so that we can open the merge request as the user
TOKEN_NAME=`echo ${GITLAB_USER_LOGIN}_COMMIT_TOKEN | tr "[a-z]" "[A-Z]"`
# See: http://www.tldp.org/LDP/abs/html/parameter-substitution.html search ${!varprefix*}, ${!varprefix@} section
PRIVATE_TOKEN=`echo ${!TOKEN_NAME}`
# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"project_id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"remove_source_branch\": false,
\"force_remove_source_branch\": false,
\"allow_collaboration\": true,
\"subscribed\" : true,
\"title\": \"${GITLAB_USER_NAME} merge request for: ${CI_COMMIT_REF_SLUG}\"
}";
# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
--header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
--header "Content-Type: application/json" \
--data "${BODY}";
echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_SLUG} for user ${GITLAB_USER_LOGIN}";
exit;
fi
echo "No new merge request opened"
简短回答:当然 - 一切皆有可能。 GitLab 有一个很棒的 API(包括创建 MR)。但我认为走这条路是不好的。您应该按照设计的那样使用 GitLab。您开始合并请求的时间太晚了。在开始任何工作之前启动它,您的合并请求将在分支的整个持续时间内保持打开状态。
长答案: 这是理想的 GitLab 工作流程:
这从根本上落后于 GitHub 的工作方式(我来自 GitHub),在 GitHub 上你无法有告诉人们你正在做什么。
听起来您对利用 API 很感兴趣。有一个名为“python-gitlab”的Python包实际上可以正常工作http://python-gitlab.readthedocs.io/en/stable/gl_objects/mrs.html
import gitlab
import os
origin = "https://gitlab.example.com"
# Private token is set as an env var
gl = gitlab.Gitlab(origin, private_token, api_version='4')
gl.auth()
def create_merge_request(origin, private_token):
mr = project.mergerequests.create({'source_branch': 'cool_feature',
'target_branch': 'master',
'title': 'merge cool feature',
'labels': ['label1', 'label2']})
mr.assignee_id = gl.users.get(2).id # Assign it to coworker
def lookup_last_pipeline(origin, private_token):
current_pipeline_id = os.environ['CI_PIPELINE_ID']
pipelines = gl.projects.get(os.environ['CI_PROJECT_ID']).pipelines.list()
for pipeline in pipelines:
if pipeline.status == 'success' and pipeline.id == current_pipeline_id:
create_merge_request()
这当然是一个示例,您必须根据您的具体需求进行调整。
不使用 GitLab API
通过电子邮件打开带有补丁的合并请求文档GitLab 长期以来一直支持通过电子邮件打开合并请求,但在发送电子邮件之前,分支必须已经存在于服务器上。现在,您只需一封电子邮件即可通过附加一个或多个补丁文件来打开合并请求 (
.patch
)。
补丁文件是系统之间共享和传输更改的标准。在 GitLab 的未来版本中,我们将在此基础上构建分布式合并请求请参阅
和问题。