实现目标

  • 提交代码自动构建,自动打包为docker镜像

前期准备

安装 gitlab_runner (docker)

docker pull gitlab/gitlab-runner
#/run/docker.sock 容器中可以执行宿主机的Docker命令
docker run -d --name gitlab-runner --restart always -v /srv/runner/config:/etc/gitlab-runner -v /run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner

工程目录

  • 目录
├── buildDockerImage.sh
├── Dockerfile
├── main.go
├── Makefile
  • .gitlab-ci.yml 内容
# 所有任务基于这个镜像
image: golang:1.14.0
# 在每个任务执行前运行
before_script:
  - export VERSION=`echo ${CI_COMMIT_TAG} | awk -F"_" '{print $1}'`
  # 安装 docker,由于需要在容器里面使用宿主的docker命令,这里就需要安装一个docker的可执行文件,然后在启动容器的时候,将宿主的 docker.sock 文件挂载到容器内的同样位置。
  - curl -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar zxvf docker-latest.tgz && cp docker/docker /usr/local/bin/ && rm -rf docker docker-latest.tgz
stages:
  - build
# 定义 job
build:
  stage: build
  tags:
    - test
  script:
    - echo "开始构建程序"
    - chmod +x *.sh
    - make build_image ENV="prod" VERSION=${VERSION}
  only:
    - tags
  • Makefile 内容
#环境变量
export VERSION=1.0.0
build_image:
  @echo "构建docker镜像"
  ./buildDockerImage.sh $(VERSION)
  • buildDockerImage.sh
#!/bin/bash
echo "检测GOPATH"
if [ -z "$GOPATH" ]; then
  echo "GOPATH 未设定"
  exit 1
else
  echo "GOPATH=$GOPATH"
fi
if [ ! "$1" ]; then
  echo "请输入应用版本"
  exit 1
fi

app_name="app"
new_version=$1

echo "当前路径 $(pwd)"

echo "编译 $app_name 应用"
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o $app_name .

echo "检测 $app_name 应用"
FILE="$app_name"
if [ -f "$FILE" ]; then
  echo "$FILE 已就绪"
else
  echo "$FILE 应用不存在"
  exit 1
fi

echo "开始构建Docker镜像"
echo "sudo docker build -t $app_name:$new_version ."
docker build -t $app_name:$new_version .

echo "删除老的编译文件"

rm -rf $app_name

注册 gitlab_runner

  • 在要构建的项目里面找到 token

  • 注册一个 runner

[pc gitlabRunner]$ docker exec -it gitlab-runner gitlab-ci-multi-runner register
Runtime platform                                    arch=amd64 os=linux pid=5901 revision=4c96e5ad version=12.9.0
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.2.28/   //gitlab 地址
Please enter the gitlab-ci token for this runner:
52KL2wic1AwfxffvRrRs   //项目Token
Please enter the gitlab-ci description for this runner:
ci                     //项目描述
Please enter the gitlab-ci tags for this runner (comma separated):
test                   //给当前runner 添加 tags 在 .gitlab-ci.yml中会用到
Registering runner... succeeded                     runner=52KL2wic
Please enter the executor: docker-ssh, virtualbox, kubernetes, docker, parallels, shell, ssh, docker+machine, docker-ssh+machine, custom:
docker
Please enter the default Docker image (e.g. ruby:2.6):
golang:1.14.0
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
  • gitlab runner信息

修改 /srv/runner/config 里面的 gitlab_runner 配置文件

concurrent = 1
check_interval = 0
[session_server]
  session_timeout = 1800
[[runners]]
  name = "test docker"
  url = "http://192.168.2.28/"
  token = "XsnZbWbbkwujbAiXoJ9m"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.docker]
    tls_verify = false
    image = "golang:1.14.0"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/run/docker.sock:/var/run/docker.sock", "/cache"]
    pull_policy = "if-not-present"
    shm_size = 0
  • volumes:是为了在容器中可以执行宿主机的Docker命令。
  • pull_policy:当指定的镜像不存在的话,则通过docker pull拉取。

运行代码

git commit -a -m "test"
git push
​
## 打tag
git tag -a "1.2.1" -m "test ci"
git push origin 1.2.1

执行结果

  • gitlab

  • docker

结语

  • 发布流程有很多种,这里只是提供了一个简单的发布模式,其实发布方法不一定要最厉害最好,要最适合自己的团队才是最好的
  • 欢迎添加QQ一起讨论

联系 QQ: 3355168235