gitlab-auto-merge
我的 GitHub 源码地址:https://github.com/LinWanCen/gitlab-auto-merge
线上自动创建并接受 GitLab 合并请求,不拉代码快速合并 Auto create and accept MR (Merge Requests) for GitLab online
使用指南
配置秘钥
在用户资料里看用户ID,在设置里配置“访问令牌” (access_tokens),勾选 api 权限
设置
用户ID
访问令牌
配置到环境变量
/etc/profile.d/gitlab_api.sh
代码语言:javascript复制export GITLAB_URL=http://你的URL前缀没有尾斜杠
export GITLAB_API_URL=${GITLAB_URL}/api/v4/projects
# ${GITLAB_URL}/profile
export GITLAB_USER_ID=你的用户ID(数字)
# ${GITLAB_URL}/profile/personal_access_tokens
export GITLAB_PRIVATE_TOKEN=你的Token
放置文件与授权
代码见文末
代码语言:javascript复制/var/lib/jenkins/workspace/tool/createMR.sh
/var/lib/jenkins/workspace/tool/acceptMR.sh
chmod 755 /var/lib/jenkins/workspace/tool/createMR.sh
chmod 755 /var/lib/jenkins/workspace/tool/acceptMR.sh
Jenkins 执行脚本
代码语言:javascript复制source /etc/profile 1>/dev/null 2>&1
export CI_PROJECT_ID=你的项目ID(数字)
export CI_COMMIT_REF_NAME=源分支名
export TARGET_BRANCH=目标分支名
/var/lib/jenkins/workspace/tool/createMR.sh
/var/lib/jenkins/workspace/tool/acceptMR.sh
项目ID
脚本代码
createMR.sh
/var/lib/jenkins/workspace/tool/createMR.sh
代码语言:javascript复制#!/usr/bin/env bash
set -e
# https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
echo "
curl -X POST "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests"
--header "PRIVATE-TOKEN: ******"
--header "Content-Type: application/json"
--data
"{
\"id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"title\": \"jenkins auto merge requests\",
\"assignee_id\":\"${GITLAB_USER_ID}\"
}"
--silent
"
createMR=`curl -X POST "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests"
--header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}"
--header "Content-Type: application/json"
--data "{
"id": ${CI_PROJECT_ID},
"source_branch": "${CI_COMMIT_REF_NAME}",
"target_branch": "${TARGET_BRANCH}",
"title": "jenkins auto merge requests",
"assignee_id":"${GITLAB_USER_ID}"
}"
--silent`
echo "$createMR
"
createMR_iid=$(echo $createMR | cut -d ':' -f 3 | cut -d ',' -f 1)
if [[ $createMR_iid != *[0-9] ]]; then
exit 1
fi
acceptMR.sh
/var/lib/jenkins/workspace/tool/acceptMR.sh
代码语言:javascript复制#!/usr/bin/env bash
set -e
# https://docs.gitlab.com/ee/api/merge_requests.html#list-project-merge-requests
echo "
curl "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests?state=opened&source_branch=${CI_COMMIT_REF_NAME}"
--header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}"
--silent
"
listMR=`curl "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests?state=opened&source_branch=${CI_COMMIT_REF_NAME}"
--header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}"
--silent`
echo "$listMR
"
listMR_iid=$(echo $listMR | cut -d ':' -f 3 | cut -d ',' -f 1)
if [[ $listMR_iid != *[0-9] ]]; then
exit 1
fi
# https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
echo "
curl -X PUT "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests/$listMR_iid/merge"
--header "PRIVATE-TOKEN: ******"
--silent
"
acceptMR=`curl -X PUT "${GITLAB_API_URL}/${CI_PROJECT_ID}/merge_requests/$listMR_iid/merge"
--header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}"
--silent`
echo "$acceptMR
"
acceptMR_iid=$(echo $acceptMR | cut -d ':' -f 3 | cut -d ',' -f 1)
if [[ $acceptMR_iid != *[0-9] ]]; then
exit 1
fi