欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-08 19:53:29
一.介绍 前一篇,介绍了ASP.NET Core部署到K8S上,下面介绍我们在发布新一版本中怎么通过Gitlab CI自动给镜像打版本并部署到K8S上. 二.我们通过GitLab CI/CD 变量 不废话,先上代码: 上面的.gitlab-ci.yml 可以看到平常开发人员提交代码先 build , ......

一.介绍

   前一篇,介绍了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

 

上一篇: 进去发现,死刑

下一篇: 不糗