From 583a1ca01d5b66d26044abc2f80cf12751e9f9da Mon Sep 17 00:00:00 2001 From: Carina <carina.oliveira-inacio@etu.hesge.ch> Date: Mon, 18 Nov 2019 14:15:21 +0100 Subject: [PATCH] Add deployment stage in ci pipeline --- .gitlab-ci.yml | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5c88ec7..aa7e090 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,6 +3,7 @@ image: node:latest stages: - test - doc + - deploy before_script: - npm install @@ -20,3 +21,53 @@ doc: script: # Build documentation in md file that will be available from gitlab interface - npm run docs:md +deploy: + stage: deploy + only: + - master + # Disable the cache for this job (not needed) + before_script: [] + script: + # Any future command that fails will exit the script + - set -e + + # Install ssh-agent if it's not installed to use scp to copy files into our server + - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' + + # Add the secret key of our deployment server to this gitlab docker image. + # GitLab at times add spaces which will fail ssh into server + # and hence the [tr -d '\r']. + # Issue here https://gitlab.com/gitlab-examples/ssh-private-key/issues/1 + - eval $(ssh-agent -s) + - echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null + + # Below script will disable the prompt we get whenever we ssh into the box + # and get the message like this: "The authenticity of the host 'ip address'... + # To disable this, we need to create a entry in ~/.ssh/config to look something + # like this: + # Host * + # StrictHostKeyChecking no + - mkdir -p ~/.ssh + - touch ~/.ssh/config + - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config + + - echo "deploying to ${SERVER_IP}" + + # Create the .env file with database ip, port and name from gitlab environment variables + - echo "DATABASE=mongodb://${DB_IP}:${DB_PORT}/${DB_NAME}" >> .env + - cat .env + + # Copy project files to the deployment server + - ssh ubuntu@${SERVER_IP} "mkdir -p app" + - scp -r app.js compute.js models package-lock.json package.json public + routes tests views .env ubuntu@${SERVER_IP}:app/ + + # Restart the project on the deployment server (using forever) + # https://www.npmjs.com/package/forever + - ssh ubuntu@${SERVER_IP} " + forever stopall; + cd ~/app; + npm install --only=prod; + npm run docs:html; + NODE_ENV=prod forever start app.js + " -- GitLab