基于GitLab CI、Docker的Node.js持续集成部署方案
source link: https://laotan.net/node-ci-with-gitlab-and-docker/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
基于GitLab CI、Docker的Node.js持续集成部署方案
理论部分不赘述,一张图说完整个流程,实操以Centos 7和root用户为例
GitLab Runner安装和配置
1、 在部署机器安装CI Runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
2、 注册Runner并绑定到CI
gitlab-ci-multi-runner register
# url和token,在Git项目的 /runners页面可获取,step by step填写
配置信息保存在/etc/gitlab-runner/config.toml
3、Git根目录配置 .gitlab-ci.yml
stages:
- deploy
cache:
paths:
- node_modules/ #部分需要重复利用又被gitignore的文件
before_script:
- npm install
- npm run static-prod
- npm run vue
deploy_test:
stage: deploy
only:
- dev
script:
- rsync -azp --delete --exclude=.git current_dir deploy_dir
- docker exec -d nodejs-xxx-test bash -c “pm2 restart xxx”
...other task
Docker安装和配置
1、安装Docker和启动
yum update
curl -fsSL https://get.docker.com/ | sh
systemctl enable docker.service
systemctl start docker
2、用阿里云镜像服务器加速
cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service
sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=<your accelerate address>|g" /etc/systemd/system/docker.service
systemctl daemon-reload
service docker restart
加速器地址,请到容器Hub服务控制台查看
3、自定义镜像
# 拉取基础镜像
docker pull centos:latest
# 交互模式运行基础镜像
docker run -i -t centos:latest
# 安装Node环境和pm2以及app目录等
$root@xxxx: curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
$root@xxxx: yum -y install nodejs
$root@xxxx: npm install pm2 -g
$root@xxxx: mkdir /app/nodejs/
$root@xxxx: exit
4、提交自定义镜像
docker commit xxxx(上面$root@部分,容器id) centos/node
5、后台运行镜像
docker run --name="nodejs-xxx-test" --restart=always -d -p 8081:3131 -v deploy_dir:/data/nodejs/ centos/node /usr/bin/bash -c 'cd /data/nodejs/;pm2 start ua.json --env test --no-daemon'
-- restart=always
是让容器随docker开机启动
bash里启动pm2,需要带上--no-daemon
,防止docker后台运行后,pm2退出
6、把gitlab-runner
添加到docker
权限组
usermod -aG docker gitlab-runner
Nginx域名绑定到容器的端口
server {
listen 80;
server_name xxx.xxx.com;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log;
location / {
proxy_pass http://127.0.0.1:8082;
}
}
过程中可能因为内核设置,会出现502,不能反向代理,需要关闭
##如果SELinux status参数为enabled即为开启状态
/usr/sbin/sestatus -v
...
SELinux status: enabled
...
##将SELINUX=enforcing改为SELINUX=disabled
vim /etc/selinux/config
setsebool -P httpd_can_network_connect 1
## 重启机器
reboot
Recommend
-
83
-
55
-
33
应用场景:问题项目分为app和后台两种,为了保证再同一个环境下面测试,所以不可能链接开发本地服务进行测试,所以需要搭建一个测试环境,供app进行开发测试。这个时候就有一个问题,如果开发新增加功能或者app调试的时候发现问题,这个时候就需要提交新的代码或者...
-
19
Gitlab是一个利用RubyonRails开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人的项目Gitlab拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,他非常易于浏览提交过的版本并提供一个文件...
-
10
使用 GitLab Runner 为 R 包配置持续集成服务众所周知,R 是一个跨平台统计软件,支持 Debian Ubu...
-
12
Vue SSR基于Coding的持续集成部署实践(CI/CD)Apr 26, 2021 标签:Vue持续集成
-
2
Gitlab CI持续集成可以这样用每个公司内部都有自己的DevOps平台,来规范需求创建、开发编译、部署、测试、上线等等流程。 Beetle是转转内部的DevOps平台,很好的加强了产品的开发效率及发布质量。 其中为了实现通用性,Beetle对持续集成(CI)部分的构建...
-
3
利用 Gitlab CI 持续集成部署 Hexo使得命题“多终端管理 Hexo ”为真命题之前说过一下,我先后抛弃了 Wordpress 和 Typecho (即使再小,也是要 SQL 的……→ → )现在投奔到了 Hexo 。不可不说, Hexo 是个好东西,不过我是觉得突然没了后端...
-
4
gitlab和jenkins做持续集成构建教程 上一个轮回...
-
5
持续集成指南:GitLab 的 CI/CD 工具配置与使用 写代码这项工作,本质就是将工作自动化,减少手工操作提供效率,因为人的本质都是懒狗,程序员...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK