1. 本次安装的环境
linux centos7.6
2. 安装 gitlab-runner
只需为你的系统下载二进制文件之一
# Linux x86-64
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Linux x86
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
# Linux arm
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
3. 授予权限
sudo chmod +x /usr/local/bin/gitlab-runner
4. 创建一个gitlab CI 用户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
5. 安装并作为服务运行
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
sudo gitlab-runner status
执行这条命令:sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner如果出现此报错:
FATAL: Failed to install gitlab-runner: Init already exists: /etc/systemd/system/gitlab-runner.service
我的是因为git 版本过低,需要跟新git 版本
[root@ip-UUklasdf opt]# yum remove git -y
[root@ip-UUklasdf opt]# yum install https://centos7.iuscommunity.org/ius-release.rpm
[root@ip-UUklasdf opt]# yum install git2u -y
[root@ip-UUklasdf opt]# git --version
git version 2.16.5
[root@ip-UUklasdf opt]# sudo gitlab-runner start
[root@ip-1*** /]# sudo gitlab-runner status
gitlab-runner: Service is running!
6. gitlab runner 部署 注册 runner
需要在gitlab 找到你要自动部署的项目,步骤如下
(1)
( 2)
(3) 在你的服务器进行注册
[root@ip-1*** /]# sudo gitlab-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/ (这里是你gitlab 上图中出现的地址)
Please enter the gitlab-ci token for this runner:
XhUTRTsg***2_hFAj (这里是你gitlab 上图中出现的token)
Please enter the gitlab-ci description for this runner:
[ip-172-31-4-245.ap-northeast-2.compute.internal]: web_dev (自定义tags 尽量保持跟下方一致)
Please enter the gitlab-ci tags for this runner (comma separated):
web_dev (自定义tags)
Whether to run untagged builds [true/false]:
[false]: (回车)
Whether to lock Runner to current project [true/false]:
[false]: (回车)
Registering runner... succeeded runner=XhUTRTsg
Please enter the executor: virtualbox, docker+machine, docker-ssh+machine, docker-ssh, parallels, ssh, kubernetes, docker, shell:
shell (我是选择的shell 你根据你的服务器实际情况选择 )
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@ip-1*** /]# sudo gitlab-runner restart (重启gitlab-runner)
重启之后回到gitlab 的runner 如图所示: 证明这次注册成功
7. 配置yml文件
在项目根目录下添加一个.gitlab-ci.yml 文件。内容如下
stages:
- deploy
deploy:
stage: deploy
script:
- cp -r * /opt/web
only:
- dev
tags:
- web_dev
stages下的deploy说明在代码提交后CI需要执行deploy节点的内容。
deploy的script就是一个个shell命令,这里需要注意每个命令都以杠+空格开头。
only:只有向dev分支提交代码时才生效。
tags:只有拥有该tags的Runner才需要执行。
cd /usr/local/bin/
vim deployuitp.sh
# echo "更新代码...."
cd `项目目录`
git pull `接项目的gitlab地址`
# build
echo "构建前端...."
cd frontend/
cnpm install
cnpm run build
echo "构建成功...."
# deploy
echo "发布中...."
cd .. && kill -9 `cat manage.pid`
gunicorn manage:app -p manage.pid -w 4 -b 0.0.0.0:9000 -D
echo "发布中成功!"
sudo chmod +x /usr/local/bin/deployuitp.sh
7. 测试
向dev提交一次代码后,打开项目的CI/CD –> Pipelines界面,就可以看到CI的执行记录
我是监听master ,当master 分支代码进行推送成功后,这里会出现代码提交记录,并且代码也会同步到你的服务器
先克隆项目到服务器
本文资料参考:
安装部署参考:https://docs.gitlab.com/runner/install/linux-manually.html
安装过程中不理解名词参考:https://blog.csdn.net/WMSOK/article/details/80017467
安装过程中报错参考:https://blog.csdn.net/weixin_33743880/article/details/93111056