Created
August 31, 2018 10:29
-
-
Save gblok/a7a9717fbe741ca47034f7784b0275e1 to your computer and use it in GitHub Desktop.
GitLab CI example for Node.JS (pm2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: node:7.7.0 | |
cache: | |
key: "$CI_PROJECT_ID" | |
paths: | |
- node_modules/ | |
before_script: | |
- npm set progress=false | |
building: | |
stage: build | |
variables: | |
NODE_ENV: "development" | |
script: | |
- npm i --no-optional | |
- npm run build | |
type: build | |
tags: | |
- npm | |
- node | |
only: | |
- tags | |
- master | |
testing: | |
stage: test | |
# services: | |
# - mongo:3.2 | |
# - rabbitmq:3.6 | |
# - redis:3.2 | |
variables: | |
NODE_ENV: "development" | |
script: | |
- npm i --no-optional | |
- npm run test | |
type: test | |
tags: | |
- npm | |
- node | |
only: | |
- tags | |
- master | |
staging: | |
stage: deploy | |
environment: staging | |
variables: | |
NODE_ENV: "production" | |
script: | |
# Run ssh-agent (inside the build environment) | |
- eval $(ssh-agent -s) | |
# Add the SSH key stored in SSH_RUNNER_KEY variable to the agent store | |
- ssh-add <(echo "$SSH_RUNNER_KEY") | |
# For Docker builds disable host key checking. Be aware that by adding that | |
# you are suspectible to man-in-the-middle attacks. | |
- mkdir -p ~/.ssh | |
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' | |
# Install pm2 | |
- npm i pm2 -g | |
# Deploy project | |
- pm2 deploy ecosystem.json staging | |
type: deploy | |
tags: | |
- npm | |
- node | |
only: | |
- master | |
production: | |
stage: deploy | |
environment: production | |
variables: | |
NODE_ENV: "production" | |
script: | |
# Run ssh-agent (inside the build environment) | |
- eval $(ssh-agent -s) | |
# Add the SSH key stored in SSH_RUNNER_KEY variable to the agent store | |
- ssh-add <(echo "$SSH_RUNNER_KEY") | |
# For Docker builds disable host key checking. Be aware that by adding that | |
# you are suspectible to man-in-the-middle attacks. | |
- mkdir -p ~/.ssh | |
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' | |
# Install pm2 | |
- npm i pm2 -g | |
# Deploy project | |
- pm2 deploy ecosystem.json production | |
type: deploy | |
tags: | |
- npm | |
- node | |
only: | |
- tags | |
- trigger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
/** | |
* Here we declare the apps that must be managed by PM2 | |
* All options are listed here: | |
* https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#json-app-declaration | |
*/ | |
"apps": [ | |
{ | |
"name": "name", | |
"script": "./bin/index.js", | |
"exec_mode": "cluster", | |
"instances": "4", | |
"env": {}, | |
"env_production": { | |
"NODE_ENV": "production" | |
} | |
} | |
], | |
/** | |
* PM2 help you to deploy apps over your servers | |
* For more help go to : | |
* https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#deployment-pm2--090 | |
*/ | |
"deploy": { | |
"staging": { | |
"user": "USER", | |
"host": "HOST", | |
"ref": "origin/master", | |
"repo": "REPO", | |
"path": "PATH", | |
"post-deploy": "npm i --no-optional && npm run build && pm2 startOrGracefulReload ecosystem.json --env production && pm2 save" | |
}, | |
"production": { | |
"user": "USER", | |
"host": "HOST", | |
"ref": "origin/master", | |
"repo": "REPO", | |
"path": "PATH", | |
"post-deploy": "npm i --no-optional && npm run build && pm2 startOrGracefulReload ecosystem.json --env production && pm2 save" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment