GitLab CI/CD+Docker的CI/CD部署工作流

GitLab CI/CD+Docker的CI/CD部署工作流

      • 准备环境
      • 使用Docker安装gitlab-runner
      • 进入gitlab-runner的容器并注册gitlab-runner
      • 编写.gitlab-ci.yml文件
      • 使用git commit推送变更后的代码到gitlab,触发gitlab ci/cd,执行流程如下:
      • 在本地docker环境可以查看到对应服务的docker容器已经起来了:
      • 参考资料:

准备环境

操作系统:Mac OS

Docker版本:20.10.13

使用Docker安装gitlab-runner

docker run -d --name gitlab-runner-docker \
         --restart always -v $PWD:/etc/gitlab-runner \
         -v /var/run/docker.sock:/var/run/docker.sock \
         gitlab/gitlab-runner:latest

安装成功后如下所示:

Untitled.png
$PWD:/etc/gitlab-runner 表示在执行docker run命令的当前目录下挂载gitlab-runner的配置文件,这里还需要改一下当前gitlab-runner的配置,否则后面gitlab ci运行时会有如下错误:

error during connect: Post  dial tcp: lookup docker on 8.8.8.8:53: no such host

修改后的最终具体config.toml文件如下:

concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "container-runner"
  url = ""
  id = 6249
  token = "******"
  token_obtained_at = 2022-12-15T10:49:19Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:20.10.7-dind"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    #volumes项是是必须要修改的项目,即添加上/var/run/docker.sock:/var/run/docker.sock
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
    shm_size = 0

进入gitlab-runner的容器并注册gitlab-runner

具体操作如下:

$ root@9f005be60f1a:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=68 revision=f0a95a76 version=14.5.0
Running in system-mode.

Enter the GitLab instance URL (for example, ):
我使用的是极狐的gitlab,所以这里填下图对应的url
Enter the registration token:
填下图对应的token
Enter a description for the runner:
[9f005be60f1a]: container-runner
Enter tags for the runner (comma-separated):
docker,runner #tag很重要,可以在.gitlab-ci.yml的job中增加tags字段,如果tags的内容和runner的tag相匹配,则此runner被分配来执行此次CI/CD Pipeline
Registering runner... succeeded                     runner=sFV-HEkL
Enter an executor: custom, docker, shell, ssh, virtualbox, docker-ssh, parallels, docker+machine, docker-ssh+machine, kubernetes:
docker #指定excutor为docker
Enter the default Docker image (for example, ruby:2.6):
docker:20.10.7-dind #指定docker镜像
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

GitLab CI/CD+Docker的CI/CD部署工作流_第1张图片

注册成功后:
GitLab CI/CD+Docker的CI/CD部署工作流_第2张图片

编写.gitlab-ci.yml文件

services:
  - docker:20.10.7-dind

stages:          
  - build

build:
  stage: build
  tags: 
    - docker
  services:
    - docker:20.10.7-dind
  before_script:
    # 以下环境变量都是gitlab ci专有的
    - echo "$CI_REGISTRY_USER"
    - echo "$CI_REGISTRY_PASSWORD"
    - echo "$CI_REGISTRY_IMAGE"
    - echo "$CI_REGISTRY"
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:2.0.0 .
    - docker push $CI_REGISTRY_IMAGE:2.0.0
    - docker run --name myapp -d -p 8081:8081 $CI_REGISTRY_IMAGE:2.0.0

项目的目录结构:
GitLab CI/CD+Docker的CI/CD部署工作流_第3张图片

使用git commit推送变更后的代码到gitlab,触发gitlab ci/cd,执行流程如下:

GitLab CI/CD+Docker的CI/CD部署工作流_第4张图片

GitLab CI/CD+Docker的CI/CD部署工作流_第5张图片

在本地docker环境可以查看到对应服务的docker容器已经起来了:

image.png

对应服务也可以正常访问了:

GitLab CI/CD+Docker的CI/CD部署工作流_第6张图片

参考资料:

以容器的方式运行极狐GitLab Runner_Docker_极狐GitLab_InfoQ写作社区

在 Ubuntu 上安装使用极狐GitLab Runner_CI/CD_极狐GitLab_InfoQ写作社区

以 Kubernetes 的方式来运行极狐GitLab Runner_极狐GitLab_InfoQ写作社区

你可能感兴趣的:(项目部署,docker,gitlab,ci/cd)