gitlab runner使用
...大约 3 分钟
提示
gitlab-runner 是gitlab ci/cd中的执行工具
本教程在centos7下操作
安装gitlab-runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
export GITLAB_RUNNER_DISABLE_SKEL=true; sudo -E yum install gitlab-runner -y
启动/停止/重启gitlab-runner服务
# 启动
systemctl start gitlab-runner
# 停止
systemctl stop gitlab-runner
# 重启
systemctl restart gitlab-runner
# 查看
systemctl status gitlab-runner
# 设置为开机自启
systemctl enable gitlab-runner
# 取消开机自启
systemctl disable gitlab-runner
处理无权限拉取代码问题
重新安装将用户更改为root
先卸载gitlab-runner
gitlab-runner uninstall
然后再安装,并将用户设置为root
gitlab-runner install --user=root --working-directory=/root
重新启动
systemctl restart gitlab-runner
gitlab-runner使用
- 运行gitlab-runner register命令注册
gitlab-runner register
- 输入gitlab的url地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.xxxxx/
- 输入要注册的token, token在gitlab项目上获取
Please enter the gitlab-ci token for this runner:
asadpna_xxxxxx
- 输入对这个runner的描述,可以在gitlab网页上修改
Please enter the gitlab-ci description for this runner:
[sada1234]: test_runner
- 输入runner的tag,需与.gitlab-runner.ci文件上的一致
Please enter the gitlab-ci tags for this runner (comma separated):
tag-example-dev-prod
- 输入runner的executor
Please enter the executor: ssh, kubernetes, shell, docker, docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine, custom:
shell
- 笔者这里选择shell
到这里就注册完成了。注册完成后会生成 /etc/gitlab-runner/config.toml 文件,文件是runner的配置文件
- 查看已注册的runner
gitlab-runner list
.gitlab-ci.yaml文件参考
选择的executor是shell
stages:
- pullsrc
- gendoc
- build
- deploy
############## 开发环境 #######################
# 拉取代码
pullsrc_stage:
stage: pullsrc
only:
- dev # 部署分支
tags:
- tag-example-dev # gitlab-runner 注册的时候配置该tag
script: # 执行脚本
- cd 你的项目根目录
- git checkout -- .
- git pull
# 生成文档
gendoc_stage:
stage: gendoc
only:
- dev
tags:
- tag-example-dev
script:
- cd 你的项目根目录
- swag init --output ./docs/api
# 项目构建
build_stage:
stage: build
only:
- dev
tags:
- tag-example-dev
script:
- cd 你的项目根目录
- go build -o example main.go
# 部署到服务器
deploy_stage:
stage: deploy
only:
- dev
tags:
- tag-example-dev
script:
- cd 你的项目根目录
- /bin/bash ./deploy_dev.sh
############## 生产环境 #######################
pullsrc_prod:
stage: pullsrc
only:
- master
tags:
- tag-example-dev-prod
script:
- cd 你的项目根目录
- git checkout -- .
- git pull
deploy_prod:
stage: deploy
only:
- master
tags:
- tag-example-dev-prod
script:
- cd 你的项目根目录
- /bin/bash ./deploy.sh
deploy_dev.sh文件参考
#!/bin/bash
# 部署项目路径
project_path='你的项目根目录'
cd ${project_path}
# ssh证书路径
ssh_key_path='~/.ssh/id_rsa'
# 部署命令
deploy_shell="cd ${project_path} && mv ${project_path}/example-tmp ${project_path}/example && systemctl restart example-api"
# 部署服务器1
echo "------正在部署服务器1------"
api1_ip='192.168.1.11'
api1_port=22
## cp配置文件
scp -i ${ssh_key_path} -P ${api1_port} ./conf/in-dev.yaml root@${api1_ip}:${project_path}/conf
## cp二进制文件
scp -i ${ssh_key_path} -P ${api1_port} ./example root@${api1_ip}:${project_path}/example-tmp
## 重启服务
ssh -i ${ssh_key_path} -p ${api1_port} root@${api1_ip} "${deploy_shell}"
echo "------完成部署服务器1------"
deploy.sh文件内容参考
# 部署项目路径
project_path='你的项目根目录'
cd ${project_path}
# 构建服务
go build -o example main.go
# ssh证书路径
ssh_key_path='~/.ssh/id_rsa'
# 部署命令
deploy_shell="cd ${project_path} && mv ${project_path}/example-tmp ${project_path}/example && systemctl restart example-api"
# 部署服务器1
echo "------正在部署服务器1------"
api1_ip='192.168.1.11'
api1_port=22
## cp配置文件
scp -i ${ssh_key_path} -P ${api1_port} ./conf/in-prod.yaml root@${api1_ip}:${project_path}/conf
## cp二进制文件
scp -i ${ssh_key_path} -P ${api1_port} ./example root@${api1_ip}:${project_path}/example-tmp
## 重启服务
ssh -i ${ssh_key_path} -p ${api1_port} root@${api1_ip} "${deploy_shell}"
echo "------完成部署服务器1------"
Powered by Waline v2.15.5