安装gitlab-runner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 下载 :如果下载太慢可以在浏览器下载,然后上传到服务器中,我是这样做的
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# 分配运行权限
chmod +x /usr/local/bin/gitlab-runner
# 创建用户
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# 安装
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# 运行
gitlab-runner start
|
我采用的是二进制文件安装,也可以采用docker安装,但是使用docker会让后续打包发布变得更麻烦一些,我就放弃了docker安装的方式
注册gitlab-runner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 注册 runner
gitlab-runner register
# 输入 runner 的访问地址
http://xxx.xxx.xx.xxx:xxx
# 输入 runner token
xxxxxxxxxxxxxxxxxxxxxx
# runner 描述,随便填
XX项目部署
# runner tag,全局唯一
# 在.gitlab-ci.yaml中会需要用到,gitlab根据tag来分配对应的runner服务执行任务
server-cicd
# 输入(选择) shell
shell
|
注册完成后就可以在gitlab项目settings=>CI/CD=>Runners=>Available specific runners中看到注册成功的服务信息
编写.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
stages:
- build
- deploy
build-inv:
stage: build
tags:
- server-cicd
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
changes:
- app/service/inventory/*
before_script:
- export PATH=$PATH:/usr/local/go/bin
script:
- echo "=========Start Build(inventory)========="
- echo $CI_COMMIT_REF_NAME
- echo $CI_COMMIT_SHORT_SHA
- go env -w GO111MODULE=on
- go env -w GOPROXY=https://goproxy.cn,direct
- cd app/service/inventory/cmd/server/
- go mod tidy
- make build
- make docker
- echo "=========End Build(inventory)========="
deploy-inv:
stage: deploy
tags:
- server-cicd
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
changes:
- app/service/inventory/*
script:
- echo "=========Start Deploy(inventory)========="
- echo $CI_COMMIT_REF_NAME
- echo $CI_COMMIT_SHORT_SHA
- docker stop service-inv:latest || true
- docker rm service-inv:latest || true
- cd app/service/inventory/cmd/server && make docker-run
- echo "=========End Deploy(inventory)========="
|
GitLab-Runner的部署就完成了,根据GitLab提供的模板对.gitlab-ci.yml文件编写CI/CD配置后,提交代码Runner服务就会根据配置文件信息进行对应的操作执行达到自动构建测试部署的效果
参考文献
https://docs.gitlab.com/ee/ci/