Yang03 / blog

0 stars 0 forks source link

gitlab CI 自动部署Node服务 #19

Open Yang03 opened 7 years ago

Yang03 commented 7 years ago

安装gitlab-ci-multi-runner

```
 //centos
 curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bas

 sudo yum install gitlab-ci-multi-runner   
 ```   

注册runner

```
sudo gitlab-runner register
//Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
 填写你项目所在gitlab的网址
//Please enter the gitlab-ci token for this runner:
  项目设置里面
 // Choose whether the Runner should pick up jobs that do not have tags
 runner是否接受没有标签的job
 Whether to lock Runner to current project
 是否将Runner锁定到当前项目 

```

为项目添加gitlab.yml

    deploy:
  # stage:deploy
  script:
    - sudo git checkout-index -a -f --prefix=/home/www/tapes
    - sudo -u root /bin/env bash -c "export NVM_DIR=/root/.nvm && source /root/.nvm/nvm.sh && nvm use default && npm install"
    - sudo -u root /bin/env bash -c "export NVM_DIR=/root/.nvm && source /root/.nvm/nvm.sh && nvm use default && pm2 restart www"

这个时候,你提交代码的后,去点一下,就会触发runner 因为,gitlab runner 会在机器上创建一个gitlab runner 用户,然后会去拉代码,你需要把代码拷贝到你希望到的目录

    sudo git checkout-index -a -f --prefix=/home/www/tapes

因为创建的gitlab runner权限问题,需要为gitlab runner 添加sudo nopasswd

因为node,nvm都是root下安装的,所以gitlab runner 执行npm 会报 'command npm not found' 需要

    - sudo -u root /bin/env bash -c "export NVM_DIR=/root/.nvm && source /root/.nvm/nvm.sh && nvm use default && npm install"
    - sudo -u root /bin/env bash -c "export NVM_DIR=/root/.nvm && source /root/.nvm/nvm.sh && nvm use default && pm2 restart www"