Add .gitlab-ci.yml

This commit is contained in:
dbroqua 2018-07-31 08:20:52 +02:00
parent 468249992a
commit b7aa94ecce
3 changed files with 110 additions and 0 deletions

80
.gitlab-ci.yml Normal file
View file

@ -0,0 +1,80 @@
image: node:9.4.0
cache:
paths:
- node_modules/
- .yarn
before_script:
- apt-get update -qq && apt-get install
stages:
- build
- test
- staging
- production
Build:
stage: build
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install
script:
- npm run build
Test:
stage: test
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install
script:
# Installs Chrome
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
- apt-get update
- apt-get install google-chrome-stable -y
# Runs the tests.
- npm run test:karma-headless
Deploy to Staging:
stage: staging
tags:
- node
before_script:
# Generates to connect to the AWS unit the SSH key.
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
# Sets the permission to 600 to prevent a problem with AWS
# that it's too unprotected.
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- bash ./gitlab-deploy/.gitlab-deploy.staging.sh
environment:
name: staging
# Exposes a button that when clicked take you to the defined URL:
url: http://ec2-13-59-173-91.us-east-2.compute.amazonaws.com:3001
Deploy to Production:
stage: production
tags:
- node
before_script:
# Generates to connect to the AWS unit the SSH key.
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
# Sets the permission to 600 to prevent a problem with AWS
# that it's too unprotected.
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- bash ./gitlab-deploy/.gitlab-deploy.prod.sh
environment:
name: production
# Exposes a button that when clicked take you to the defined URL:
url: http://ec2-13-59-173-91.us-east-2.compute.amazonaws.com:81
when: manual

15
.gitlab-deploy.prod.sh Normal file
View file

@ -0,0 +1,15 @@
# !/bin/bash
# Get servers list:
set - f
# Variables from GitLab server:
# Note: They can't have spaces!!
string=$DEPLOY_SERVER
array=(${string//,/ })
# Iterate servers for deploy and pull last commit
# Careful with the ; https://stackoverflow.com/a/20666248/1057052
for i in "${!array[@]}"; do
echo "Deploy project on server ${array[i]}"
ssh ubuntu@${array[i]} "cd ./Pardo/vr && git stash && git checkout $CI_BUILD_REF_NAME && git stash && git pull origin master && sudo yarn install && sudo npm run production"
done

15
.gitlab-deploy.staging.sh Normal file
View file

@ -0,0 +1,15 @@
# !/bin/bash
# Get servers list:
set - f
# Variables from GitLab server:
# Note: They can't have spaces!!
string=$DEPLOY_SERVER
array=(${string//,/ })
# Iterate servers for deploy and pull last commit
# Careful with the ; https://stackoverflow.com/a/20666248/1057052
for i in "${!array[@]}"; do
echo "Deploy project on server ${array[i]}"
ssh ubuntu@${array[i]} "cd ./Staging/vr && git stash && git checkout $CI_BUILD_REF_NAME && git stash && git pull && sudo yarn install && sudo npm run staging"
done