当前位置: 首页 > 工具软件 > Drone-CI > 使用案例 >

Drone CI K8S Tag管理

葛磊
2023-12-01

一、前言

Drone集成K8S时,若docker image始终设置为latest,是无法触发K8S滚动升级机制的;
测试环境:可采用${DRONE_BUILD_NUMBER}内置变量作为docker image版本号;
生产环境:可采用源代码git tag作为docker image版本号,对应内置变量${DRONE_TAG};
注意事项:为避免测试环境和生产环境docker image镜像混入,建议测试环境和生产环境设置不同的docker image版本号规则,为了进一步保险起见,可以设置为不同的镜像名称,如果增加文件夹名称区分或镜像名称区分;例如
my-registry.com/test/my-app
my-registry.com/prod/my-app

二、测试环境

1.docker部署

可使用latest作为docker image版本号

  ci_release:
    image: plugins/docker
    dockerfile: docker/my-app/Dockerfile
    registry: my-registry.com
    repo: my-registry.com/test/my-app
    tags: latest
    secrets: [docker_username, docker_password]
    when:
      branch: ci_app

  ci_deploy:
    image: appleboy/drone-ssh
    host: 192.168.1.200
    port: 22
    username: root
    secrets: [ssh_password]
    script:
      - "docker stop my-app"
      - "docker rm my-app"
      - "docker rmi  my-registry.com/test/my-app"
      - "docker pull my-registry.com/test/my-app"
      - "docker run --name my-app -p 8080:8080 --restart=always -d  my-registry.com/test/my-app"
    when:
      branch: ci_app

2.K8S部署

不可使用latest作为docker image版本号,需借助Drone内置变量${DRONE_BUILD_NUMBER}

  ci_release:
    image: plugins/docker
    dockerfile: docker/my-app/Dockerfile
    registry: my-registry.com
    repo: my-registry.com/test/my-app
    tags: ${DRONE_BUILD_NUMBER}
    secrets: [docker_username, docker_password]
    when:
      branch: ci_app

  ci_deploy:
    image: quay.io/honestbee/drone-kubernetes
    kubernetes_server: https://192.168.1.100:6444
    kubernetes_token: xxxxxx
    namespace: test
    deployment: my-app
    repo: my-registry.com/test/my-app
    container: my-app
    tag: ${DRONE_BUILD_NUMBER}
    when:
      branch: ci_app

三、生产环境

不可使用latest作为docker image版本号,需借助Drone内置变量${DRONE_BUILD_NUMBER}${DRONE_TAG};生产环境推荐使用${DRONE_TAG}内置变量,与源代码tag分支保持一致,方便定位代码版本。

   bd_release:
    image: plugins/docker
    dockerfile: docker/my-app/Dockerfile
    registry: my-registry.com
    repo: my-registry.com/prod/my-app
    tags: ${DRONE_TAG:latest}
    secrets: [docker_username, docker_password]
    when:
      event: tag
      branch: refs/tags/v-*
      
  bd_deploy:
    image: quay.io/honestbee/drone-kubernetes
    secrets: [plugin_namespace, plugin_kubernetes_server, plugin_kubernetes_token]
    deployment: my-app
    repo: my-registry.com/prod/my-app
    container: my-app
    tag: ${DRONE_TAG}
    when:
      event: tag
      branch: refs/tags/v* 

参考:
Configure | Drone
Docker Pipeline | Drone
基于 Drone + Gogs 构建私有 CI/CD 平台 | Kubernetes 篇

 类似资料: