1. 程式人生 > >K8S+GitLab-自動化分布式部署ASP.NET Core(三) 更新鏡像版本並部署到K8S上

K8S+GitLab-自動化分布式部署ASP.NET Core(三) 更新鏡像版本並部署到K8S上

win clas windows 阿裏 pro token source .net core har

一.介紹

前一篇,介紹了ASP.NET Core部署到K8S上,下面介紹我們在發布新一版本中怎麽通過Gitlab CI自動給鏡像打版本並部署到K8S上.

二.我們通過GitLab CI/CD 變量

不廢話,先上代碼:

stages:
 - build
 - test
 - package
 - deploy
 - release


build:
 stage: build
 script:
  - cd Deploy.API
  - dotnet build


test:
  stage: test
  script: 
   - dotnet restore
   - dotnet build
   - cd minicover
   - dotnet restore
   - dotnet minicover instrument --workdir ../ --assemblies Deploy.API.UnitTests/bin/**/*.dll --sources Deploy.API/**/*.cs
   - dotnet minicover reset
   - cd ..
   - dotnet test --no-build Deploy.API.UnitTests/Deploy.API.UnitTests.csproj
   - cd minicover
   - dotnet minicover report --workdir ../ --threshold 10
   - dotnet minicover uninstrument --workdir ../
   


package:
  stage: package
  script: 
   - cd Deploy.API
   - docker build -t deploy:test . 
   #- docker login --username $ALIYUN_NAME --password $ALIYUN_TOKEN registry.cn-hangzhou.aliyuncs.com #阿裏雲倉庫
   #- docker tag deploy:test registry.cn-hangzhou.aliyuncs.com/windows_evan/deploy:test
   #- docker push registry.cn-hangzhou.aliyuncs.com/windows_evan/deploy:test
   - docker login --username=evan --password Pwd123456 192.168.0.203  #私有倉庫Harbor
   - docker tag deploy:test 192.168.0.203/myproject/deploy:test
   - docker push 192.168.0.203/myproject/deploy:test


deploy:
  stage: deploy
  script:
   - if [ $(docker ps -aq --filter name=userapi) ]; then docker rm -f userapi;fi 
  #- docker run -d -p 8003:80 --link db-mysql --network net_db --name userapi registry.cn-hangzhou.aliyuncs.com/windows_evan/deploy:test
   - docker run -d -p 8003:80 --link db-mysql --network net_db --name userapi 192.168.0.203/myproject/deploy:test


    
release:
  stage: release
  script: 
   - cd Deploy.API
   - sed -i ‘s/deploy:test/deploy:‘$CI_COMMIT_TAG‘/g‘ deploy.yaml
   - cat deploy.yaml
   - kubectl apply -f deploy.yaml
  only: 
   - tags 

package-release:
  stage: package
  script:  
   - cd Deploy.API
   - docker build -t deploy:$CI_COMMIT_TAG .  
   - docker login --username=evan --password Pwd123456 192.168.0.203
   - docker tag deploy:$CI_COMMIT_TAG 192.168.0.203/myproject/deploy:$CI_COMMIT_TAG
   - docker push 192.168.0.203/myproject/deploy:$CI_COMMIT_TAG
  only: 
   - tags 
 

上面的.gitlab-ci.yml 可以看到平常開發人員提交代碼先 build , test , package (tag:test) , deploy (docker) ,只有給項目創建tags才會執行package-release , release , 我們通過 sed 命令修改deploy.yaml文件裏的test版本.

三.相關鏈接參考

https://www.cnblogs.com/MrVolleyball/p/10315698.html 更新k8s鏡像版本的三種方式

GitLab CI/CD Variables

K8S+GitLab-自動化分布式部署ASP.NET Core(三) 更新鏡像版本並部署到K8S上