From 3dc4ce802f923ee87a78b0980b0e6bfe9b3c8b6c Mon Sep 17 00:00:00 2001 From: Faris Shajahan Date: Mon, 11 May 2020 04:58:03 +0530 Subject: [PATCH] CircleCI changes for production EKS deployment --- .circleci/config.yml | 95 ++++++++++++++++++++------------------------ .dockerignore | 5 +++ Dockerfile | 14 +++++++ nginx/nginx.conf | 17 ++++++++ 4 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 nginx/nginx.conf diff --git a/.circleci/config.yml b/.circleci/config.yml index 886a44d385a..cee197fc988 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,55 +1,46 @@ -version: 2.1 # use CircleCI 2.1 -jobs: # a collection of steps - build: # runs not using Workflows must have a `build` job as entry point - working_directory: ~/care # directory where steps will run - docker: # run the steps with Docker - - image: circleci/node:10.16.3 # ...with this image as the primary container; this is where all `steps` will run - steps: # a collection of executable commands - - add_ssh_keys: - fingerprints: - - "7b:31:e5:d0:a8:97:2a:e1:b5:09:07:6f:77:6f:e5:72" - - checkout # special step to check out source code to working directory - - run: - name: update-npm - command: 'sudo npm install -g npm@latest' - - restore_cache: # special step to restore the dependency cache - # Read about caching dependencies: https://circleci.com/docs/2.0/caching/ - key: dependency-cache-{{ checksum "package-lock.json" }} - - run: - name: install-npm-wee - command: npm install - - save_cache: # special step to save the dependency cache - key: dependency-cache-{{ checksum "package-lock.json" }} - paths: - - ./node_modules - - run: - name: build - command: npm run build - - run: - name: list - command: ls -lah - - run: - name: Deploy - command: | - - sudo apt-get update && sudo apt-get install rsync -y +version: 2.1 - echo "The branch is: $CIRCLE_BRANCH" - if [[ $CIRCLE_BRANCH = "master" ]] - then - echo "Syncing to production" - ssh-keyscan -H 3.7.18.69 >> ~/.ssh/known_hosts - rsync -avz --delete -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p deploy@3.7.18.69"' dist/ deploy@3.7.60.240:/opt/care_prod/ - elif [[ "$CIRCLE_BRANCH" == "develop" || "$CIRCLE_BRANCH" == "migrate-to-aws" ]] - then - echo "Syncing to dev" - ssh-keyscan -H 3.7.18.69 >> ~/.ssh/known_hosts - rsync -avz --delete -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p deploy@3.7.18.69"' dist/ deploy@3.7.60.169:/opt/care/ - else - echo "Not Syncing the branch $CIRCLE_BRANCH" - fi +orbs: + aws-ecr: circleci/aws-ecr@6.8.0 + aws-eks: circleci/aws-eks@0.2.6 - # tar -czf build.tar.gz dist/ - # scp build.tar.gz netlify@34.93.26.100:/home/netlify/releases/ - # ssh netlify@34.93.26.100 "cd /home/netlify/releases/; tar xf build.tar.gz; cp -rf /opt/care_fe/ /opt/care_fe.old; mv dist /opt/care_fe/" +workflows: + deploy-to-staging: + jobs: + - aws-ecr/build-and-push-image: + account-url: AWS_ECR_ACCOUNT_URL + aws-access-key-id: AWS_ACCESS_KEY_ID + aws-secret-access-key: AWS_SECRET_ACCESS_KEY + region: AWS_DEFAULT_REGION + repo: "${ECR_REPO_NAME}" + tag: "${CIRCLE_SHA1},latest" + filters: + branches: + only: develop + - aws-eks/update-container-image: + cluster-name: "${EKS_CLUSTER_NAME}" + aws-region: "${AWS_DEFAULT_REGION}" + container-image-updates: '${EKS_CONTAINER_NAME}=${AWS_ECR_ACCOUNT_URL}/${ECR_REPO_NAME}:${CIRCLE_SHA1}' + resource-name: "${EKS_STAGING_RESOURCE_NAME}" + requires: + - aws-ecr/build-and-push-image + deploy-to-production: + jobs: + - aws-ecr/build-and-push-image: + account-url: AWS_ECR_ACCOUNT_URL + aws-access-key-id: AWS_ACCESS_KEY_ID + aws-secret-access-key: AWS_SECRET_ACCESS_KEY + region: AWS_DEFAULT_REGION + repo: "${ECR_REPO_NAME}" + tag: "${CIRCLE_SHA1},production-latest" + filters: + branches: + only: master + - aws-eks/update-container-image: + cluster-name: "${EKS_CLUSTER_NAME}" + aws-region: "${AWS_DEFAULT_REGION}" + container-image-updates: '${EKS_CONTAINER_NAME}=${AWS_ECR_ACCOUNT_URL}/${ECR_REPO_NAME}:${CIRCLE_SHA1}' + resource-name: "${EKS_PRODUCTION_RESOURCE_NAME}" + requires: + - aws-ecr/build-and-push-image diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000000..407ead420d9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +dist +.dockerignore +Dockerfile +.git diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000000..ecb4d60e50f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +#build-stage +FROM node:lts-buster-slim as build-stage +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +RUN npm run build + +#production-stage +FROM nginx:stable-alpine as production-stage +COPY --from=build-stage /app/dist /usr/share/nginx/html +COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 00000000000..40a878bcee2 --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,17 @@ +server { + + listen 80; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + + location = /50x.html { + root /usr/share/nginx/html; + } + +}