我对 gitlab 和 gitlab CI 非常陌生,我已经建立了一个正在成功完成的管道。
我的主分支和开发分支受到保护,因此需要合并请求,以便组中的另一个开发人员可以在合并之前查看代码和评论。
我想知道是否可以在此管道的末尾生成此合并请求。 gitlab 存储库中是否有此设置,或者我是否必须创建一个脚本来实现此目的?
边注 :
就在发布此内容之前,我遇到了gitlab 文档的这一部分 https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
我在 ubuntu 18.04 上使用 gitlab-runner 11.0.0
为了实现我的简单需求,我只是在管道中添加了最后一个阶段,该阶段本质上执行改编自的 bash 脚本这个帖子 https://about.gitlab.com/2017/09/05/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/.
编辑:
根据@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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)