Skip to content

Instantly share code, notes, and snippets.

@gblok
Created August 31, 2018 10:29
Show Gist options
  • Save gblok/a7a9717fbe741ca47034f7784b0275e1 to your computer and use it in GitHub Desktop.
Save gblok/a7a9717fbe741ca47034f7784b0275e1 to your computer and use it in GitHub Desktop.
GitLab CI example for Node.JS (pm2)
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
{
/**
* 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