Gitlab 自动部署

类似的安装教程网上遍地都是,安装过程就不讲了。
记录一下一点想法吧

缓存问题

gitlab-runner 使用my-php docker镜像编译和发布成功,但是缓存偶尔会出现remove fail问题
因为runner默认使用本地缓存,所以如果你又多个runner,它们之间不会共享编译缓存结果,导致缓存随机不生效,
这肯定不能接受啊,所以我在runner里面配置了使用s3缓存

用了minio作为缓存储存,解决了上诉问题
记得访问minio的web页面创建Bucket

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
# docker-compose.yml
version: "3"
services:
gitlab:
image: gitlab/gitlab-ee:13.4.3-ee.0
hostname: gitlab.my-server.com
environment:
- GENERATED_EXTERNAL_URL=http://gitlab.my-server.com:80/
volumes:
- ./data:/var/opt/gitlab
- ./log:/var/log/gitlab
- ./conf:/etc/gitlab
- /etc/localtime:/etc/localtime:ro
# 破解文件
- ./license/license.rb:/opt/gitlab/embedded/service/gitlab-rails/ee/app/models/license.rb
- ./license/.license_encryption_key.pub:/opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
ports:
- "80:80"
- "22:22"
restart: unless-stopped

runner:
image: gitlab/gitlab-runner:latest
volumes:
- ./gitlab-runner-conf:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
- /etc/localtime:/etc/localtime:ro
extra_hosts:
- "gitlab.my-server.com:192.168.1.82"
- "minio.my-server.com:192.168.1.82"
restart: unless-stopped

minio:
image: minio/minio
volumes:
- ./minio-data:/data
environment:
- MINIO_ACCESS_KEY=<ACCESSKEY>
- MINIO_SECRET_KEY=<SECRETKEY>
ports:
- "9000:9000"
command: server /data
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
# gitlab-runner-conf/config.toml

concurrent = 4
check_interval = 0

[session_server]
session_timeout = 1800

[[runners]]
name = "my-dev"
url = "http://gitlab/"
token = "your-token"
executor = "docker"
[runners.cache]
Type = "s3"
Path = "cache-path"
Shared = true
[runners.cache.s3]
ServerAddress = "minio.my-server.com"
AccessKey = "<ACCESSKEY>"
SecretKey = "<SECRETKEY>"
BucketName = "<BUCKETNAME>"
Insecure = true
[runners.docker]
tls_verify = false
image = "ubuntu:18.04"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
extra_hosts = ["minio.my-server.com:192.168.1.82", "gitlab.my-server.com:192.168.1.82"]
shm_size = 0

和webhook相比

就算要做代码部署,还是用webhook来做吧,用流水线来做部署要复杂得多,对小项目来说得不偿失

如果在线上机器使用shell exector,可以很简单的cp * /path/www 就实现了部署,但这很不优雅
需要在线上安装gitlab-runner

还是使用docker exector比较优雅,能实现跨机器一致性

基本上可以说熟悉gitlab CI/CD了

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# .gitlab-ci.yml
default:
image: shellus/my-php:latest

stages:
- install
- build
# - test
- deploy

cache:
paths:
- vendor/
- node_modules/
- oa-vue/node_modules/
- public/dist/
- public/static/

laravel-npm-install:
stage: install
only:
changes:
- package.json
script:
- npm install

oa-vue-install:
stage: install
only:
changes:
- oa-vue/package.json
script:
- cd oa-vue && npm install

composer-install:
stage: install
only:
changes:
- composer.json
script:
- composer install
- composer dump-autoload

laravel-mix-build:
stage: build
only:
changes:
- resources/assets/*
- resources/sass/*
script:
- npm install && npm run prod

oa-vue-build:
stage: build
only:
changes:
- oa-vue/src/*
- oa-vue/public/*
- oa-vue/vue.config.js
- oa-vue/babel.config.js
- oa-vue/.browserslistrc
script:
- cd oa-vue && npm run build
#
## 单元测试和功能测试(测试数据库还没搞好)
#phpunit-test:
# stage: test
# allow_failure: true
# script:
# - cp .env.example .env
# - php artisan key:generate
# - vendor/bin/phpunit

# 部署到线上环境
deploy:
stage: deploy
only:
refs:
- release
variables:
- $DEPLOY_PROD_PORT
- $DEPLOY_PROD_HOST
- $DEPLOY_PROD_USERNAME
- $DEPLOY_PATH_OA
environment:
name: my-prod
url: https://oa.my-server.com/
script:
- eval `ssh-agent` && ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- echo -e "Host prod\n\tHostname $DEPLOY_PROD_HOST\n\tPort $DEPLOY_PROD_PORT\n\tUser $DEPLOY_PROD_USERNAME\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
- rsync --rsync-path "sudo -u www-data rsync" -az --delete * --exclude=.env --exclude=storage prod:$DEPLOY_PATH_OA
- ssh prod "cd $DEPLOY_PATH_OA && php artisan migrate && php artisan queue:restart"

鄂ICP备14007840号-1