GitLab CI 自动化部署

在项目根目录下添加 .gitlab-ci.yml 文件,并将 GitLab 项目配置为使用 Runner,则每次提交或推送都会触发 CI pipeline.
点击查看官方文档


GitLab-Runner

安装

1
2
3
4
5
6
7
# Mac
brea install gitlab-runner
# 登录后启动(后台运行)
brew services start gitlab-runner

# 其它环境
https://docs.gitlab.com/runner/install/

配置

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
# 登录 GitLab,获取 URL 和 TOKEN
Shared Runner:Adimn Area -> Overview -> Runners(需要管理员权限)
Group Runner:Group -> Settings -> CI/CD -> Runner
Specific Runner:Project -> Settings -> CI/CD -> Runner

# 注册 gitlab-runner register
# sev @ sevdeiMac in ~/Desktop/update [10:14:20] C:130
$ gitlab-runner register
Runtime platform arch=amd64 os=darwin pid=4659 revision=7a6612da version=13.12.0
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.0.77:7777/
Enter the registration token:
LhV6syDworzQ_oDyezmP
Enter a description for the runner:
[sevdeiMac.local]:
Enter tags for the runner (comma-separated):

Registering runner... succeeded runner=LhV6syDw
Enter an executor: docker, shell, ssh, virtualbox, custom, docker-ssh, parallels, docker+machine, docker-ssh+machine, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

以下部署脚本,需先配置 SSH 公钥登录

配置 PHP 项目的部署脚本

.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
# 测试服务器 121.21.21.21,端口 7777,用户 www,项目目录 /data/server
# 生产服务器 121.21.21.22,端口 7777,用户 www,项目目录 /data/server

stages:
- deploy

deploy-test:
stage: deploy
variables:
SERVER_IP: "121.21.21.21"
SERVER_PORT: "7777"
SERVER_USERNAME: "www"
PROJECT_DIR: "/data/server"
script: "sh ./deploy.sh"
only:
- test

deploy-prod:
stage: deploy
variables:
SERVER_IP: "121.21.21.22"
SERVER_PORT: "7777"
SERVER_USERNAME: "www"
PROJECT_DIR: "/data/server"
script: "sh ./deploy.sh"
only:
- master

deploay.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
rsync -azv -e "ssh -p ${SERVER_PORT}" --delete \
--exclude=.git \
--exclude=.idea \
--exclude=.gitignore \
--exclude=.gitattributes \
--exclude=.gitlab-ci.yml \
--exclude=.DS_Store \
--exclude=.env \
--exclude=vendor \
./ ${SERVER_USER}@${SERVER_IP}:${PROJECT_DIR}
ssh -p ${SERVER_PORT} ${SERVER_USER}@${SERVER_IP} << EOF
cd ${PROJECT_DIR}
composer install
exit
EOF

配置前端项目的部署脚本

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
# 前端采用 npm 作为包管理工具,vue 作为框架,打包后的目录名为 dist
# 测试服务器 121.21.21.21,端口 7777,用户 www,项目目录 /data/client
# 生产服务器 121.21.21.22,端口 7777,用户 www,项目目录 /data/client
stages:
- build
- deploy

build:
stage: build
script:
- npm config set registry https://registry.npm.taobao.org
only:
- test

deploy-test:
stage: deploy
script:
- npm install
- npm run build:stage
- rsync -azv -e "ssh -p 7777" --delete dist/ www@121.21.21.21:/data/client
cache:
paths:
- node_modules/
only:
- test

deploy-prod:
stage: deploy
script:
- npm ci
- npm run build
- rsync -azv -e "ssh -p 7777" --delete dist/ www@121.21.21.22:/data/client
only:
- master

参考资料

0%