三.Gitlab Flow小結
整個過程貫徹了git flow 預釋出分支release,hotfix的核心用法, 同時在部署方式上也有一定的改進。
- alpha上使用git預釋出分支名release-1.0.0作為鏡像Tag,切出release分支即形成同tag名鏡像,自動部署
- alpha(develop)上有release-1.0.0,release-1.0.1 這樣版本遞增的tag; 但是如果針對某一release-版本bugfix,鏡像tag不會變,代碼會更新,這裡其實與docker tag的用法有點不符;
- 在kubernetes deploy我暫時使用兩個不同指令來更新鏡像.
kubectl set image …;
kubectl roloout restart deploy …
對develop release分支形成的鏡像tag上可嘗試帶上CI_COMMIT_SHORT_SHA 來規避這種騷操作。
prod上要求從release分支上打出git标簽,同時要求手動點選部署,多步驟操作確定部署是受控可預期,并且可復原
內建測試采用docker-compose部署; alpha,prod是采用k8s部署; 從上面的Gitlab flow 知道:
- Git develop分支、release-分支、tag标簽、master分支會打出容器鏡像,
- Git develop分支代碼(ImageTag:develop)(隻)會自動部署內建測試環境,
- Git release- 分支(ImageTag:release-1.0.0)(隻)會自動部署到alpha,
- Git tag标簽(ImageTag:v1.0.0) 手動點選部署到prod
-
stages:
-
- build
-
- build_image
-
- deploy
-
variables:
-
deploy_path:
"/home/eap/website"
-
build:
-
stage: build
-
script:
-
- pwd
-
-
"for d in $(ls app/src);do echo $d;pro=$(pwd)/app/src/$d/$d.csproj; dotnet build $pro; done"
-
tags:
-
-
my-tag
-
build_image:EAPWebsite:
-
stage: build_image
-
script:
-
- dotnet publish app/src/EAP.Web/EAP.Web.csproj -c release -o container/app/publish/
-
- docker build -t $DOCKER_REGISTRY_HOST/eap/website:$CI_COMMIT_REF_NAME container/app
-
- docker login $DOCKER_REGISTRY_HOST -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
-
- docker push $DOCKER_REGISTRY_HOST/eap/website:$CI_COMMIT_REF_NAME
-
tags:
-
-
my-tag
-
only:
-
- tags
-
- develop
-
- master
-
-
/^release-.*$/i
-
deploy:intergate-test:
-
stage: deploy
-
script:
-
- ssh -t [email protected]
"cd /home/eap/website && export TAG=$CI_COMMIT_REF_NAME && docker-compose pull website && docker-compose -f docker-compose.yml up -d"
-
tags:
-
-
my-tag
-
only:
-
- develop # 開發階段,intergate Test環境隻會部署ImageTag:develop鏡像
-
deploy:alpha:
-
stage: deploy
-
script:
-
- ssh -t [email protected]
"sudo kubectl set image deployment/eap-website eap-website=repository.****.com:8443/eap/website:${CI_COMMIT_REF_NAME} && sudo kubectl rollout status deployment/eap-website"
-
tags:
-
-
my-tag
-
only:
-
-
/^release-.*$/i # alpha環境隻部署以ImageTag:release-開頭鏡像
-
deploy:prod:
-
stage: deploy
-
script:
-
- ssh -t [email protected]
"sudo kubectl set image deployment/eap-website eap-website=repository.****.com:8443/eap/website:${CI_COMMIT_REF_NAME} && sudo kubectl rollout restart deployment/eap-website"
-
tags:
-
-
my-tag
-
only:
-
- tags
-
- master
-
when: manual # prod環境,人工點選部署
- 使用ssh遠端部署
- 基于docker-compose完成的Gitlab-ci
- 在kubernetes環境,我是使用kubectl set image …指令改變鏡像,同分支名更新重新拉取鏡像部署。