gitlab CI/CD实践

gitlab-runner

CI/CD实际执行器,Saas架构。
支持的客户端有docker、kubernetes、Linux、Windows
https://docs.gitlab.com/runner/install/linux-manually.html
按照说明,一步步操作即可实现安装。

可能遇到的问题

  • Docker in Docker

由于gitlab-runner使用的执行器为容器,例如docker或者kubernetes。就可能遇到在Docker容器里面执行Docker命令的问题,即Docker in Docker。Docker官网给出的解决方案:https://hub.docker.com/_/docker/。具体操作见https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
Runner的配置样例:
样例的gitlab-runner为Linux部署
[runners.docker]
tls_verify = false
image = "docker:19.03.12"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/certs/client", "/cache"]
shm_size = 0

 docker run -p 9000:9000 -p 9001:9001 --name minio1 -v D:\data:/data -v D:\minio\config:/root/.minio -e "MINIO_ROOT_USER=minio" -e "MINIO_ROOT_PASSWORD=12345678"  minio/minio server /data  --console-address ":9001"

配置一个bucket


image.png

配置一个serviceAccount


image.png

Runner添加cache的配置:
[runners.cache]
Type = "s3"
Path = "node_cache"
[runners.cache.s3]
ServerAddress = "172.20.0.14:9000"
AccessKey = "PBCBXQJIW7LUOT931243"
SecretKey = "fDJag3laL2EiIbwzwC9WtfEOizvBg22NorwsJ+86"
BucketName = "test"
Insecure = true

  • Runner执行Kubectl命令

对于使用Kubernetes管理的集群,最后一步都是部署应用到k8s集群,一般都需要执行kubectl命令进行发布。gitlab的解决方案为使用代理。

image.png

这里可以使用代理的方式装Runner:
https://docs.gitlab.com/runner/install/kubernetes-agent.html
修改gitlab-ci.yaml:
https://docs.gitlab.com/ee/user/clusters/agent/ci_cd_workflow.html

简易完整gitlab-ci.yml 样例:

variables:
  # When using dind service, you must instruct Docker to talk with
  # the daemon started inside of the service. The daemon is available
  # with a network connection instead of the default
  # /var/run/docker.sock socket.
  DOCKER_HOST: tcp://docker:2376
  #
  # The 'docker' hostname is the alias of the service container as described at
  # https://docs.gitlab.com/ee/ci/services/#accessing-the-services.
  # If you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier,
  # the variable must be set to tcp://localhost:2376 because of how the
  # Kubernetes executor connects services to the job container
  # DOCKER_HOST: tcp://localhost:2376
  #
  # Specify to Docker where to create the certificates. Docker
  # creates them automatically on boot, and creates
  # `/certs/client` to share between the service and job
  # container, thanks to volume mount from config.toml
  DOCKER_TLS_CERTDIR: "/certs"
  # These are usually specified by the entrypoint, however the
  # Kubernetes executor doesn't run entrypoints
  # https://gitlab.com/gitlab-org/gitlab-runner/-/issues/4125
  DOCKER_TLS_VERIFY: 1
  DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"

stages:          # List of stages for jobs, and their order of execution
  - build
  - push
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: harbor.lib-mat.ac.cn/library/node-builder:latest
  script:
    - npm config set registry http://registry.npm.taobao.org/
    - npm install
    - npm run build
  cache:
    key: build-cache
    paths:
      - dist/

push-job:   # This job runs in the test stage.
  stage: push
  image: docker:19.03.12
  services:
    - docker:19.03.12-dind
  script:
    - docker login $HARBOR_URL -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
    - docker build -t $CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA .
    - docker tag $CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA $HARBOR_URL/apps/$CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA
    - docker push $HARBOR_URL/apps/$CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA
  cache:
    key: build-cache
    paths:
      - dist/

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  image:
    name: bitnami/kubectl:latest
    entrypoint: [ '' ]
  script:
    - kubectl config get-contexts
    - kubectl config use-context root/pipelin-test:gusulab
    - kubectl get pods

即使用工件又使用缓存:

variables:
  # When you use the dind service, you must instruct Docker to talk with
  # the daemon started inside of the service. The daemon is available
  # with a network connection instead of the default
  # /var/run/docker.sock socket. Docker 19.03 does this automatically
  # by setting the DOCKER_HOST in
  # https://github.com/docker-library/docker/blob/d45051476babc297257df490d22cbd806f1b11e4/19.03/docker-entrypoint.sh#L23-L29
  #
  # The 'docker' hostname is the alias of the service container as described at
  # https://docs.gitlab.com/ee/ci/services/#accessing-the-services.
  #
  # Specify to Docker where to create the certificates. Docker
  # creates them automatically on boot, and creates
  # `/certs/client` to share between the service and job
  # container, thanks to volume mount from config.toml
  DOCKER_TLS_CERTDIR: "/certs"
  CI_IMAGE_TAG: ""


stages:          # List of stages for jobs, and their order of execution
  - build
  - push
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: harbor.lab/library/node-builder@sha256:30212862eeb3477a6e6823ce13dd697af2e820e75d7cd1411a84a00452e72d07
  script:
    - export
    - npm config set registry http://registry.npm.taobao.org/
    - npm install
    - npm run build
    - echo 'Hello' > a.txt
  tags:
    - docker
  cache:
    key: build-cache
    paths:
      - dist/
  artifacts:
    name: "build-job-artifacts"
    paths:
      - a.txt
build_image:
  stage: push
  image: docker:19.03.12
  services:
      - docker:19.03.12-dind
  tags:
    - docker
  script:
    - ls /cache/cicd/test/node_modules
    - export
    - docker login $HARBOR_URL -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
    - docker build -t test:$CI_COMMIT_SHORT_SHA .
    - docker tag test:$CI_COMMIT_SHORT_SHA $HARBOR_URI/apps/test:$CI_COMMIT_SHORT_SHA
    - docker push $HARBOR_URI/apps/test:$CI_COMMIT_SHORT_SHA
  cache:
    key: build-cache
    paths:
      - dist/
pages:
  stage: deploy
  script:
    - mkdir .public
    - cp -r * .public
    - mv .public public
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  tags:
    - docker



下图可以简单观察cache和artifacts的先后关系


image.png

下图是工件上传后的结果:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容