From 68ad40f14bbff7c706285d8be280292baf1bd333 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigo Garcia Date: Mon, 25 Feb 2019 17:55:57 -0300 Subject: [PATCH] Issue #55: Update CircleCI config to automatically deploy application The build config now creates a new Docker image, tags it with latest, pushes it to Google Container Registry and deploys to Google Compute Engine. --- .circleci/config.yml | 28 ++++++++++++++++++++++++ scripts/deploy.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++ scripts/setup.sh | 20 +++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100755 scripts/deploy.sh create mode 100755 scripts/setup.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 52fec64..23890ac 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -39,3 +39,31 @@ jobs: # run tests! #- run: yarn test + deploy: + docker: + - image: google/cloud-sdk + working_directory: ~/sticky-sessions-web + steps: + - checkout + - run: + name: Setup Google Cloud SDK + command: scripts/setup.sh + - setup_remote_docker: + docker_layer_caching: true + - run: + name: Docker Build, Push and Deploy + command: scripts/deploy.sh + +workflows: + version: 2 + build_and_deploy: + jobs: + - build + - deploy: + requires: + - build + filters: + branches: + only: + - dev + - master \ No newline at end of file diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..1303e6e --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +if [ -z "$IMAGE_NAME" ]; then + echo "IMAGE_NAME env variable is empty. Exiting." + exit 1 +fi + +if [ -z "$GOOGLE_PROJECT_ID" ]; then + echo "GOOGLE_PROJECT_ID env variable is empty. Exiting." + exit 1 +fi + +# Authenticate to Google Container Registry +gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://gcr.io + +if [ "$CIRCLE_BRANCH" = "issue_55_cd" ]; then + echo "Deploying application to Staging environment" + + # Injects firebase DB file + echo $FIREBASE_DB_STAG | base64 -di > src/db/firebaseDB/keys/index.js + + # Builds Docker image + docker build -t ${IMAGE_NAME} . + + # Tags newly created image + docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag + + # Pushes image to Container Registry + docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag + + # Deploys application + gcloud --quiet compute ssh stick-sessions --zone us-east1-b -- "cd /home/app && ./restart_service.sh gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag" +elif [ "$CIRCLE_BRANCH" = "master" ]; then + echo "Deploying application to Production environment" + + # Injects firebase DB file + echo $FIREBASE_DB | base64 -di > src/db/firebaseDB/keys/index.js + + # Builds Docker image + docker build -t ${IMAGE_NAME} . + + # Tags newly created image + docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:${CIRCLE_SHA1} + docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest + + # Pushes image to Container Registry + docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:${CIRCLE_SHA1} + docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest + + # Deploys application + gcloud --quiet compute ssh stick-sessions --zone us-east1-b -- "cd /home/app && ./restart_service.sh gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest" +fi diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100755 index 0000000..eb05358 --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +if [ -z "$GCLOUD_SERVICE_KEY" ]; then + echo "GCLOUD_SERVICE_KEY env variable is empty. Exiting." + exit 1 +fi + +if [ -z "$GOOGLE_PROJECT_ID" ]; then + echo "GOOGLE_PROJECT_ID env variable is empty. Exiting." + exit 1 +fi + +# Set service account key +echo $GCLOUD_SERVICE_KEY | base64 -di > ${HOME}/gcloud-service-key.json + +# Authenticate gcloud +gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json + +# Set project ID +gcloud --quiet config set project ${GOOGLE_PROJECT_ID}