From b7aa94ecceb061ae1e730a61793585f4f38ba0b8 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Tue, 31 Jul 2018 08:20:52 +0200 Subject: [PATCH] Add .gitlab-ci.yml --- .gitlab-ci.yml | 80 +++++++++++++++++++++++++++++++++++++++ .gitlab-deploy.prod.sh | 15 ++++++++ .gitlab-deploy.staging.sh | 15 ++++++++ 3 files changed, 110 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 .gitlab-deploy.prod.sh create mode 100644 .gitlab-deploy.staging.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d8053ea --- /dev/null +++ b/.gitlab-ci.yml @@ -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 diff --git a/.gitlab-deploy.prod.sh b/.gitlab-deploy.prod.sh new file mode 100644 index 0000000..a9dd3f9 --- /dev/null +++ b/.gitlab-deploy.prod.sh @@ -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 diff --git a/.gitlab-deploy.staging.sh b/.gitlab-deploy.staging.sh new file mode 100644 index 0000000..1ce9baf --- /dev/null +++ b/.gitlab-deploy.staging.sh @@ -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