谷歌還是比必應要好用一點。
在前公司,我根據主流的git flow 給團隊搭建了一套devops流程,運行在 docker & k8s上。
在現代devops流程中,一般推薦使用git分支名或者git tag作為鏡像的tag名。
在實際操作中, 我遇到了一個流程阻塞。
根據git flow的規范,我們一般會打出feature/xxx,fix/issue234,release/x.x.x 這樣的分支名, 當然我們還會產生x.y.z 這樣的git tag名。
但是docker build -t
產生鏡像tag的規定,除了-,_,.
鏡像tag不允許使用其他特殊字符
A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag name may not start with a period or a dash and may contain a maximum of 128 characters
我當時是讓團隊小伙伴不要打出含有 /
的分支名, 如上圖中所示的release-1.0.0
等,但是我始終覺得不是一個常規操作,因為這破壞了一以貫之的git flow命名規范,而且需要在團隊內做技術性約束。
當git flow分支命名與docker image tag分支有沖突,該怎么辦?
面向谷歌編程,面向Stackoverflow編程啊。
策略1: 腳本手動替換
在Gitlab-ci中,我們使用:
docker build . -t image_name:$CI_COMMIT_REF_NAME | sed 's/[^a-zA-Z0-9]/-/g'
release/v1.0.0 會被轉換為 release-v1-0-1
CI_COMMIT_REF_NAME
: The branch or tag name for which project is built.
策略2: gitlab-ci內置變量CI_COMMIT_REF_SLUG
CI_COMMIT_REF_SLUG
:CI_COMMIT_REF_NAME in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.
很明顯,CI_COMMIT_REF_SLUG是正解,后續在devops中針對git branch/git tag 可采用此ci變量。
本文基本沒啥技能點, 單純記錄在Devops路上的一個小插曲,前人栽樹后人乘涼;
順便表明一個態度,希望在流暢、自然的開發流程上深耕。
后續大家有意的話,可以結合 《基於容器和K8s的 Devops 探索和落地實踐》 了解一個常規/有效/可落地的Devops流程。
引用鏈接
[1] Stackoverflow: https://stackoverflow.com/questions/62905914/turning-a-git-branch-name-into-a-valid-docker-image-tag
[2] CI_COMMIT_REF_SLUG: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
[3] 基於容器和K8s的 Devops 探索和落地實踐: https://www.cnblogs.com/JulianHuang/p/13676065.html