前面我们使用docker 部署了一个私有的 gitlab, 我们需要配置一条流水线,在用户上传代码时,对代码进行校验,自动化编译和部署;GitLab Runner 已提供此功能
1. 安装 GitLab runner
1.2 Docker安装 编写 docker-compose.yml
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-runner:latest
container_name: gitlab-runner
restart: always
volumes:
- './config:/etc/gitlab-runner'
- './docker.sock:/var/run/docker.sock'
- 执行
docker-compose up -d安装GitLab Runner
1.2 windows 安装
- 直接下载安装
1.3 linux 安装
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt-get install gitlab-runner
2. 新建项目 runner

企业微信截图_17289901852161.png
- 创建一个标签为
build的 runner
3. 注册 runners
3.1 docker 环境注册
docker exec -it gitlab-runner gitlab-runner register

企业微信截图_17289897511816.png
- 根据创建的
runner参数来注册 -
Enter an executor, 当前测试我们选择shell即可,其他可按照提示依次填写
3.2 linux 环境注册
gitlab-runner register --url http://gitlab.example.com:8929 --token glrt-GfpyZbFG43Y_v5B6yhyv
4.在项目中新建 .gitlab-ci.yml
build-job:
stage: build
tags:
- build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
test-job1:
stage: test
tags:
- build
script:
- echo "This job tests something"
test-job2:
stage: test
tags:
- build
script:
- echo "This job tests something, but takes more time than test-job1."
- echo "After the echo commands complete, it runs the sleep command for 20 seconds"
- echo "which simulates a test that runs 20 seconds longer than test-job1"
- sleep 20
deploy-prod:
stage: deploy
tags:
- build
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
environment: production
-
ci分为三个阶段build->test->deploy, 编译,测试,部署 - 其中
test阶段有两个任务 - 因为上面我们只注册了一个tags =
build的runner, 所以这里所有阶段都使用此runner;实际项目中,不同阶段的runner应该不一样。
5. 提交代码,查看 runner 执行状态

企业微信截图_17289910684794.png
- 可以看到
CI正在按照预想配置在执行