前言
在使用 GitLab 時,創(chuàng)建 Merge Request 是最常用的功能之一,每天有大量的 Merge Request 被 Create、Review、Approve 和 Merge,盡管 GitLab 的產(chǎn)品經(jīng)理和 UX 設計師們已經(jīng)盡力的將 UI 設計的簡潔易懂好操作,并提供了一些諸如使用 Email、API、Web IDE、VS Code 插件等創(chuàng)建 Merge Request 的功能,但這些操作都逃不過:create new branch ==> git push ==> create merge request 這三步。
那么有沒有方法可以將這三步合并成一步呢?答案是有的,git push options 可以直接通過 git push 來創(chuàng)建 GitLab Merge Request。
Tips:在您向 GitLab 推送新分支完成后,GitLab 會在您的終端用鏈接提示您創(chuàng)建合并請求,效果如下:... remote: To create a merge request for my-new-branch, visit: remote: https://gitlab.example.com/my-group/my-project/merge_requests/new?merge_request%5Bsource_branch%5D=my-new-branch ?+點擊該鏈接 即可直接跳轉(zhuǎn) Merge Request 創(chuàng)建頁面。
版本要求
GitLab 自 11.7 版本開始支持 git push options,目前(GitLab 15.0)支持的 push options 有 CI/CD 操作 和 Merge Request 操作 兩種。
Git push options 僅適用于 Git 2.10 或更新版本。
對于 Git 版本 2.10 到 2.17,使用 --push-option:
git push --push-option=<push_option>
對于 2.18 及更高版本,您可以使用上述格式,或者更短的 -o:
git push -o <push_option>
創(chuàng)建 Merge Request
現(xiàn)在您就可以使用一行 git push 命令來完成推送代碼+創(chuàng)建 Merge Request 的操作了:
git push -o merge_request.create -o merge_request.target=my-target-branch
復制代碼
Tips: 通過使用多個 -o(或 --push-option)標志,您可以組合推送選項以一次完成多個任務。
可用選項
GitLab 提供了多種操作項來幫您完成 Merge Request 的創(chuàng)建。當然,您也可以通過 merge_request.description + Quick action 的方式完成更多的操作。
如果您使用要求文本中包含空格的推送選項,則需要將其括在引號 (") 中。如果沒有空格,您可以省略引號。一些示例:
git push -o merge_request.label="Label with spaces"
git push -o merge_request.label=Label-with-no-spaces
復制代碼
在 GitLab CI 中創(chuàng)建 Merge Request
目前網(wǎng)上對于在 GitLab CI 中創(chuàng)建 Merge Request 的方法,全是使用 curl 調(diào)用 GitLab API 來實現(xiàn)的。其實不必那么麻煩,git push options 一個操作即可解決。
Create Merge Request:
stage: push
image: alpine:latest
before_script:
- sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
- apk add --update git
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script: |
echo "create merge request"
git checkout -b auto-${CI_JOB_ID}
git add .
git commit -m "auto create merge request"
git push "https://${GITLAB_USER_LOGIN}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:auto-${CI_JOB_ID}" \
-o merge_request.create -o merge_request.target=develop -o merge_request.remove_source_branch \
-o merge_request.title="auto generator swagger api" -o merge_request.label="auto-generation" -o merge_request.assign="qk44077907"
這里的 $CI_GIT_TOKEN 需要先創(chuàng)建用戶訪問令牌,并將其添加到 CI/CD Variables 當中。如果使用的是項目訪問令牌,則需要將 ${GITLAB_USER_NAME} 和 ${GITLAB_USER_EMAIL} 配置為項目機器人用戶:
-
Name:project_{project_id}_bot
-
Email:project{project_id}_bot@noreply.{Gitlab.config.gitlab.host}
更多內(nèi)容見官方文檔。
CI/CD Push options
目前支持的 CI/CD push options 有兩個:跳過 CI Jobs 和 插入 CI/CD Variable,比較常用的是 插入 CI/CD Variable,可以用來測試一些 Variable 的效果。
使用 ci.skip 的示例:
git push -o ci.skip
為流水線傳遞一些 CI/CD 變量的示例:
git push -o ci.variable="MAX_RETRIES=10" -o ci.variable="MAX_TIME=600"
使用 git alias 簡化命令
一般來說使用 git push options 的場景都比較固定,可以考慮將很長的 push options 設置為 Git aliases 來簡化命令。
設置 Git alias:
git config --global alias.mwps "push -o merge_request.create -o merge_request.target=master -o merge_request.merge_when_pipeline_succeeds"
然后快速推送以默認分支為目標的本地分支,并在流水線成功時合并:
git mwps origin <local-branch-name>
結語
極狐 GitLab 文檔中心
現(xiàn)已正式上線,本文的大部分內(nèi)容來自使用 Git --> 推送選項部分。在開始動手工作之前仔細閱讀一下文檔是一個非常好的習慣,可以幫助您少走很多彎路。文章來源:http://www.zghlxwxcb.cn/news/detail-791625.html
參考資料
-
推送選項 - docs.gitlab.cn文章來源地址http://www.zghlxwxcb.cn/news/detail-791625.html
到了這里,關于極狐 GitLab 冷知識:使用 git push 創(chuàng)建 Merge Request的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!