From f54310cb7f102c5d8196ad7ec04510b2c0023cf4 Mon Sep 17 00:00:00 2001
From: Quang Long <39556130+longbui98@users.noreply.github.com>
Date: Wed, 25 Oct 2023 00:34:56 +0700
Subject: [PATCH] Updated config.yml

---
 .circleci/config.yml | 116 ++++++++++++++++++++++++++-----------------
 1 file changed, 71 insertions(+), 45 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index f99fcd5..7334dea 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -4,6 +4,11 @@ commands:
   destroy-environment:
     description: Destroy back-end and front-end cloudformation stacks given a workflow ID.
     steps:
+      - run:
+          name: Install AWS CLI
+          command: |
+            apk -v --update add python3 py-pip
+            pip install awscli
       - run:
           name: Destroy environments
           when: on_fail
@@ -164,63 +169,84 @@ jobs:
       - destroy-environment
   configure-infrastructure:
     docker:
-      - image: ubuntu-2004:2023.10.1
+      - image: python:3.7-alpine3.11
     steps:
       - checkout
       - add_ssh_keys:
             fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f]
       - persist_to_workspace:
           root: ~/
-          paths: ~/
+          paths: 
+            - project/.circleci
       - run:
           name: Install dependencies
           command: |
-            sudo apt-add-repository ppa:ansible/ansible
-            sudo apt update
-            sudo apt install ansible
+            apk add --update ansible
       - run:
           name: Configure server
           command: |
             cd .circleci/ansible
             ansible-playbook -i inventory.txt configure-server.yml
       - destroy-environment    
-#   run-migrations:
-#     docker:
-#       # Docker image here that supports NodeJS
-#     steps:
-#       # Checkout code from git
-#       - run:
-#           name: Run migrations
-#           command: |
-#             # Your code here
-#             exit 1
-#       - run:
-#           name: Send migration results to memstash
-#           command: |
-#             # Your code here
-#             exit 1
-#      # Here's where you will add some code to rollback on failure      
+  run-migrations:
+    docker:
+      - image: circleci/node:13.8.0
+    steps:
+      - checkout
+      - persist_to_workspace:
+          root: ~/
+          paths: 
+            - project/frontend
+      - run:
+          name: Run migrations
+          command: |
+            npm install
+            npm run migrations > migrations_dump.txt
+      - run:
+          name: Send migration results to memstash
+          command: |
+             	if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt
+              then
+                  curl https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_${CIRCLE_WORKFLOW_ID:0:7}  -d '1'
+              fi
+      - destroy-environment 
 
-#   deploy-frontend:
-#     docker:
-#       # Docker image here that supports AWS CLI
-#     steps:
-#       # Checkout code from git
-#       - run:
-#           name: Install dependencies
-#           command: |
-#             # your code here
-#       - run:
-#           name: Get backend url
-#           command: |
-#             # your code here
-#             export API_URL="http://${BACKEND_IP}:3030"
-#             echo "${API_URL}"
-#       - run:
-#           name: Deploy frontend objects
-#           command: |
-#             # your code here
-#       # Here's where you will add some code to rollback on failure      
+  deploy-frontend:
+    docker:
+      - image: amazon/aws-cli
+    steps:
+      - checkout
+      - persist_to_workspace:
+          root: ~/
+          paths: 
+            - project/frontend
+      - run:
+          name: Install dependencies
+          command: |
+            sudo yum update -y
+            sudo apt install -y tar
+            tar --version
+            sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
+            curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
+            sudo yum install -y nodejs
+            node -v
+            npm -v
+      - run:
+          name: Get backend url
+          command: |
+            export BACKEND_IP=$(aws ec2 describe-instances --query 'Reservations[*].Instances[*].PublicIpAddress')
+            export API_URL="http://${BACKEND_IP}:3030"
+            echo "API_URL = ${API_URL}"
+            echo API_URL="http://${BACKEND_IP}:3030" >> frontend/.env
+            cat frontend/.env
+      - run:
+          name: Deploy frontend objects
+          command: |
+            npm install
+            npm run build
+            tar -czvf artifact-"${CIRCLE_WORKFLOW_ID:0:7}".tar.gz dist
+            aws s3 cp dist s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7} --recursive
+      - destroy-environment     
                     
 #   deploy-backend:
 #     docker:
@@ -317,10 +343,10 @@ workflows:
               only: [test-feature-branch]
       - configure-infrastructure:
           requires: [deploy-infrastructure]
-      # - run-migrations:
-      #     requires: [configure-infrastructure]
-      # - deploy-frontend:
-      #     requires: [run-migrations]
+      - run-migrations:
+          requires: [configure-infrastructure]
+      - deploy-frontend:
+          requires: [run-migrations]
       # - deploy-backend:
       #     requires: [run-migrations]
       # - smoke-test: