本文将以LNMP+Docker自动化部署项目
安装原文参照:https://blog.csdn.net/qq_36892341/article/details/73918672
安装原文参照:https://blog.csdn.net/xy752068432/article/details/75975065
照着上面安装基本没问题
注意下nginx.conf配置文件支持php的完整代码
server {
listen 80; #指定端口
server_name 127.0.0.1; #指定服务名
root /var/www/html; #指定访问的路径(放代码位置)
location / {
index index.html index.htm index.php l.php;
autoindex off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass phpfpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
安装原文参照:https://blog.csdn.net/qq_21592575/article/details/88777368
gitlab runner作用:提交合并代码时,指定分支在runner安装的服务器进行某些操作(执行sh文件);比如提交更新代码时,把代码更新到服务器,并设置某些文件过滤不更新。
(1)使用命令在Docker中安装Gitlab Runner
docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/nginx/www/html:/var/www/html \
gitlab/gitlab-runner:latest
-v 添加目录映射,即主机上的/var/nginx/www/html和容器中/var/www/html目录是同步的
注意1:加上 -v /var/nginx/www/html:/var/www/html 这个目录映射,是为了同步服务器运行的代码到对应的目录,/var/nginx/www/html和上面讲安装nginx的路径是一样的。
(2)注册并设置Gitlan Runner
[1]访问Gitlab获取http://你的gitlab地址/admin/runners (为了获取下步注册用的token)
[2]运行Gitlab Runner注册设置
命令行:
docker exec -it gitlab-runner gitlab-runner register
#请输入gitlab-ci协调员URL(例如https://gitlab.com/),没有公网域名的输入ip,因为上面的gitlab教程是用端口8081的,别忘记加上。例:http://ip:8081/
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
#请输入此runner的gitlab-ci令牌,上一步获得的token
Please enter the gitlab-ci token for this runner:
33gfh5i****2R54
#请输入该runner的gitlab-ci描述,随便填
Please enter the gitlab-ci description for this runner:
[6d1bc8938869]:
#请输入这个runner的gitlab-ci标签(逗号分隔),这个标签作为标示,为了后面yml文件指定runner
Please enter the gitlab-ci tags for this runner (comma separated):
demo
#是否运行未标记的构建[true/false]:
Whether to run untagged builds [true/false]:
[false]: true
#是否将Runner锁定到当前项目[true/false]:
Whether to lock the Runner to current project [true/false]:
[true]: true
Registering runner... succeeded
runner=eF8wyziy
#请输入执行器(这里选择shell就可以了,因为我们的lnmp环境不在这个容器搭建,只需执行shell操作,把代码更新到映射的目录就可以了)
Please enter the executor: parallels, shell, ssh, virtualbox, docker+machine, docker, docker-ssh, docker-ssh+machine, kubernetes:
shell
注册好之后就能在Runners看见了
安装好Runner后要配置 .gitlab-ci.yml 文件让runner执行shell,.gitlab-ci.yml 文件放在根目录下
例子:
stages:
- deploy
variables:
PREVIEW_PROJECT_DIR: "/data/project/$CI_PROJECT_DIR" #定义更新代码到服务器的路径
RSYNC_SOURCE_DIR: "$CI_PROJECT_DIR" #项目目录
# 可自定义任务名称
job1:
stage: deploy
tags:
- news #标签,上面配置runner的标题,指定runner执行
only:
- master #指定合并更新的分支
script: #runner执行的代码
- "set -xe"
- "mkdir -p $PREVIEW_PROJECT_DIR" #创建目录文件夹
- >
- "rsync -auv $RSYNC_SOURCE_DIR/ $PREVIEW_PROJECT_DIR/" #拉取代码到指定目录下