8

基于GitLab CI、Docker的Node.js持续集成部署方案

 3 years ago
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.
neoserver,ios ssh client

基于GitLab CI、Docker的Node.js持续集成部署方案

tds

22 Dec 2016 • 2 min read

理论部分不赘述,一张图说完整个流程,实操以Centos 7和root用户为例

ci-docker

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK