三.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 …命令改变镜像,同分支名更新重新拉取镜像部署。