Jenkins
環境docker化
版本2..263.4
gitlab
環境docker化
版本13..7
api v4
原因:說要按分支 按流程 合并 發版,python3 也有一堆api 測了下一堆報錯就不多說了,
下面還是可以優化一下的,多個項目的時候可能出現點一次隻執行了送出 ,還沒合并,那就再點一次
1gitlab配置令牌,不多說了給個api權限,百度一大堆
2
配置到環境變量 檔案都是docker裡配置 需要權限的話docker exec -it -u root id或者name bash
/etc/profile.d/gitlab_api.sh
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
Jenkins 建構執行shell腳本
#!/usr/bin/env bash
#新環境 重新整理 使用
source /etc/profile 1>/dev/null 2>&1
#項目id
export CI_PROJECT_ID=2
#項目的分支test
export CI_COMMIT_REF_NAME=test
#項目分支master
export TARGET_BRANCH=master
#建立送出分支 把test合并到master
/opt/createMR.sh
#停一秒,就怕請求合并還沒完全生效,導緻合并失敗,點選一次就合成成功
sleep 1s
#接受合并分支請求
sh /opt/acceptMR.sh
為啥加#!/usr/bin/env bash 是因為Build step 'Execute shell' marked build as failure這個報錯
最後一行sh 也是因為Build step 'Execute shell' marked build as failure其實可以sh都 美觀一點
/usr/bin/env: ‘bash\r’: No such file or directory 忘了又搞成win建立檔案了,vim可以修改 但還是用linux建立 複制粘貼快
/opt/createMR.sh
#!/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
/opt/acceptMR.sh
#!/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
最後給權限
chmod 755 /opt/createMR.sh
chmod 755 /opt/acceptMR.sh