From 02c1cd3a02db7ac058d1a99d0b6acccba76a6873 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 22:57:46 +0700 Subject: [PATCH 01/95] Add configure-infrastructure job --- .circleci/ansible/configure-server.yml | 18 +++++--- .../roles/configure-server/tasks/main.yml | 20 +++++++++ .circleci/config.yml | 44 ++++++++++--------- 3 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 .circleci/ansible/roles/configure-server/tasks/main.yml diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index bc38c9d..42149a7 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -14,15 +14,23 @@ pre_tasks: - name: "wait 600 seconds for target connection to become reachable/usable." - # Your code here + wait_for_connection: - name: "install python for Ansible." - # Your code here + become: true + raw: test -e ${ansible_python_interpreter} || (apt -y update && apt install -y python3) + changed_when: false # Get the environment variables from CircleCI and add to the EC2 instance environment: - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" - # Add more env vars here - + - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" + - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" + - TYPEORM_PORT: 5432 + - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" + - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" + - TYPEORM_DATABASE: "{{ lookup('env', 'TYPEORM_DATABASE')}}" + - TYPEORM_MIGRATIONS: "{{ lookup('env', 'TYPEORM_MIGRATIONS')}}" + - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env', 'TYPEORM_MIGRATIONS_DIR')}}" roles: - # Your code here \ No newline at end of file + - configure-server \ No newline at end of file diff --git a/.circleci/ansible/roles/configure-server/tasks/main.yml b/.circleci/ansible/roles/configure-server/tasks/main.yml new file mode 100644 index 0000000..b093a2f --- /dev/null +++ b/.circleci/ansible/roles/configure-server/tasks/main.yml @@ -0,0 +1,20 @@ +--- +- name: "upgrade packages." + become: true + apt: + upgrade: "yes" + +- name: "install dependencies." + become: true + apt: + name: ["nodejs", "npm"] + state: latest + update_cache: yes + +- name: "install pm2" + become: true + npm: + name: pm2 + global: yes + production: yes + state: present \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 8426b3a..6d57d34 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -162,25 +162,27 @@ jobs: paths: - project/.circleci/ansible/inventory.txt - destroy-environment -# configure-infrastructure: -# docker: -# # Docker image here that supports Ansible -# steps: -# # Checkout code from git -# # Add ssh keys with fingerprint -# # attach workspace -# - run: -# name: Install dependencies -# command: | -# # Your code here -# exit 1 -# - run: -# name: Configure server -# command: | -# # Your code here -# exit 1 -# # Here's where you will add some code to rollback on failure - + configure-infrastructure: + docker: + image: ubuntu-2004:2023.10.1 + 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: ~/ + - run: + name: Install dependencies + command: | + sudo apt-add-repository ppa:ansible/ansible + sudo apt update + sudo apt install 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 @@ -312,8 +314,8 @@ workflows: filters: branches: only: [test-feature-branch] - # - configure-infrastructure: - # requires: [deploy-infrastructure] + - configure-infrastructure: + requires: [deploy-infrastructure] # - run-migrations: # requires: [configure-infrastructure] # - deploy-frontend: From 643448be64620859d53bd57290bc79be45c1b40e Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 23:10:14 +0700 Subject: [PATCH 02/95] Update image and path in configure-infrastructure jong --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6d57d34..f99fcd5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -164,13 +164,14 @@ jobs: - destroy-environment configure-infrastructure: docker: - image: ubuntu-2004:2023.10.1 + - image: ubuntu-2004:2023.10.1 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: ~/ - run: name: Install dependencies command: | From d7dbdcaf07a25c200207a4bfe3a66fc4cd6ae93d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 23:18:37 +0700 Subject: [PATCH 03/95] Update path in configure-infrastructure job --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f99fcd5..264837d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -171,7 +171,7 @@ jobs: 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: | From 6d8ba355a1882eef915607b28d011b19d2d47024 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 23:29:23 +0700 Subject: [PATCH 04/95] Changing image in configure infrastructure --- .circleci/config.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 264837d..c4d00b7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -164,7 +164,7 @@ jobs: - destroy-environment configure-infrastructure: docker: - - image: ubuntu-2004:2023.10.1 + - image: python:3.7-alpine3.11 steps: - checkout - add_ssh_keys: @@ -175,9 +175,7 @@ jobs: - 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: | From 842de248fe925700fadf7165c92916c750fca22c Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 23:52:09 +0700 Subject: [PATCH 05/95] Adding job run-migrations --- .circleci/config.yml | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c4d00b7..104fa8f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -182,22 +182,24 @@ jobs: 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 + - 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: @@ -315,8 +317,8 @@ workflows: only: [test-feature-branch] - configure-infrastructure: requires: [deploy-infrastructure] - # - run-migrations: - # requires: [configure-infrastructure] + - run-migrations: + requires: [configure-infrastructure] # - deploy-frontend: # requires: [run-migrations] # - deploy-backend: From 8fe07e4096b4776bf2d86bcddafddaea7cc82b18 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:06:18 +0700 Subject: [PATCH 06/95] Adding deploy-frontend job --- .circleci/config.yml | 62 ++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 104fa8f..5e77aa8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -187,6 +187,9 @@ jobs: - image: circleci/node:13.8.0 steps: - checkout + - persist_to_workspace: + root: ~/ + paths: project/.circleci - run: name: Run migrations command: | @@ -201,26 +204,41 @@ jobs: 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/.circleci + - 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: @@ -319,8 +337,8 @@ workflows: requires: [deploy-infrastructure] - run-migrations: requires: [configure-infrastructure] - # - deploy-frontend: - # requires: [run-migrations] + - deploy-frontend: + requires: [run-migrations] # - deploy-backend: # requires: [run-migrations] # - smoke-test: From b9611a4e5a5712c20c7599592463560304e30815 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:15:31 +0700 Subject: [PATCH 07/95] Update path and create file for storing migration info --- .circleci/config.yml | 4 ++-- backend/migrations_dump.txt | 0 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 backend/migrations_dump.txt diff --git a/.circleci/config.yml b/.circleci/config.yml index 5e77aa8..c455b43 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -189,7 +189,7 @@ jobs: - checkout - persist_to_workspace: root: ~/ - paths: project/.circleci + paths: project/ - run: name: Run migrations command: | @@ -211,7 +211,7 @@ jobs: - checkout - persist_to_workspace: root: ~/ - paths: project/.circleci + paths: project/ - run: name: Install dependencies command: | diff --git a/backend/migrations_dump.txt b/backend/migrations_dump.txt new file mode 100644 index 0000000..e69de29 From 95a860f5c4200ec794d5dfb3d5c1a8bd0e7dff00 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:25:12 +0700 Subject: [PATCH 08/95] Update path for migration jod and move migrations_dump file to frontend folder --- .circleci/config.yml | 4 +++- {backend => frontend}/migrations_dump.txt | 0 2 files changed, 3 insertions(+), 1 deletion(-) rename {backend => frontend}/migrations_dump.txt (100%) diff --git a/.circleci/config.yml b/.circleci/config.yml index c455b43..b14ec68 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,6 +3,8 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. + docker: + - image: amazon/aws-cli steps: - run: name: Destroy environments @@ -211,7 +213,7 @@ jobs: - checkout - persist_to_workspace: root: ~/ - paths: project/ + paths: project/frontend - run: name: Install dependencies command: | diff --git a/backend/migrations_dump.txt b/frontend/migrations_dump.txt similarity index 100% rename from backend/migrations_dump.txt rename to frontend/migrations_dump.txt From 99463dc41ebd1d4ee7f25f418ead76ade67d2abe Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:36:22 +0700 Subject: [PATCH 09/95] Update step to install aws cli in commands --- .circleci/config.yml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b14ec68..e16e34a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,9 +3,12 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. - docker: - - image: amazon/aws-cli steps: + - run: + name: Insall AWS CLI + command: | + apk -v --update add python3 py-pip + pip install awscli - run: name: Destroy environments when: on_fail @@ -173,7 +176,8 @@ jobs: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - persist_to_workspace: root: ~/ - paths: project/.circleci + paths: + - project/.circleci - run: name: Install dependencies command: | @@ -191,7 +195,8 @@ jobs: - checkout - persist_to_workspace: root: ~/ - paths: project/ + paths: + - project/frontend - run: name: Run migrations command: | @@ -213,7 +218,8 @@ jobs: - checkout - persist_to_workspace: root: ~/ - paths: project/frontend + paths: + - project/frontend - run: name: Install dependencies command: | From 1490097f8401030414079189d03a1f38210559d4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:44:07 +0700 Subject: [PATCH 10/95] Update step in commands --- .circleci/config.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e16e34a..6ece300 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,7 +7,6 @@ commands: - run: name: Insall AWS CLI command: | - apk -v --update add python3 py-pip pip install awscli - run: name: Destroy environments From 95d9cc048e5057ecd757755f18c477ab36ada6e4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 25 Oct 2023 00:45:35 +0700 Subject: [PATCH 11/95] Update step in commands --- .circleci/config.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6ece300..20dea3d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,14 +4,11 @@ commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. steps: - - run: - name: Insall AWS CLI - command: | - pip install awscli - run: name: Destroy environments when: on_fail command: | + pip install awscli aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: From 77f71b36a94c54f89f1eca291175e069710c13ed Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 26 Oct 2023 00:26:11 +0700 Subject: [PATCH 12/95] Update config for jobs --- .circleci/config.yml | 11 +++-------- frontend/migrations_dump.txt | 0 2 files changed, 3 insertions(+), 8 deletions(-) delete mode 100644 frontend/migrations_dump.txt diff --git a/.circleci/config.yml b/.circleci/config.yml index 20dea3d..7be97e6 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -189,18 +189,16 @@ jobs: - image: circleci/node:13.8.0 steps: - checkout - - persist_to_workspace: - root: ~/ - paths: - - project/frontend - run: name: Run migrations command: | + cd backend npm install npm run migrations > migrations_dump.txt - run: name: Send migration results to memstash command: | + cat ~/project/backend/migrations_dump.txt 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' @@ -212,10 +210,6 @@ jobs: - image: amazon/aws-cli steps: - checkout - - persist_to_workspace: - root: ~/ - paths: - - project/frontend - run: name: Install dependencies command: | @@ -238,6 +232,7 @@ jobs: - run: name: Deploy frontend objects command: | + cd frontend npm install npm run build tar -czvf artifact-"${CIRCLE_WORKFLOW_ID:0:7}".tar.gz dist diff --git a/frontend/migrations_dump.txt b/frontend/migrations_dump.txt deleted file mode 100644 index e69de29..0000000 From 1128c7b4b4babe1805a97241811b651c4a5899b7 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 26 Oct 2023 00:40:01 +0700 Subject: [PATCH 13/95] Fixing format in run-migration jobs --- .circleci/config.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7be97e6..264dbe6 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -198,11 +198,11 @@ jobs: - run: name: Send migration results to memstash command: | - cat ~/project/backend/migrations_dump.txt - 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 + cat ~/project/backend/migrations_dump.txt + 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: From b275efb7cb33366658f149da1fc3583d034bab5e Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 00:23:30 +0700 Subject: [PATCH 14/95] Configure deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 20 ++++++ .circleci/config.yml | 68 ++++++++++++------- 2 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 .circleci/ansible/roles/deploy/tasks/main.yml diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml new file mode 100644 index 0000000..b093a2f --- /dev/null +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -0,0 +1,20 @@ +--- +- name: "upgrade packages." + become: true + apt: + upgrade: "yes" + +- name: "install dependencies." + become: true + apt: + name: ["nodejs", "npm"] + state: latest + update_cache: yes + +- name: "install pm2" + become: true + npm: + name: pm2 + global: yes + production: yes + state: present \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 264dbe6..d81be42 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -132,6 +132,10 @@ jobs: - image: amazon/aws-cli steps: - checkout + - persist_to_workspace: + root: ~/ + paths: + - project/.circleci/ansible/inventory.txt - run: name: Install tar and gzip command: | @@ -158,10 +162,6 @@ jobs: aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ --output text >> inventory - - persist_to_workspace: - root: ~/ - paths: - - project/.circleci/ansible/inventory.txt - destroy-environment configure-infrastructure: docker: @@ -238,23 +238,45 @@ jobs: 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: -# # Docker image here that supports Ansible -# steps: -# # Checkout code from git -# # Add ssh keys with fingerprint -# # attach workspace -# - run: -# name: Install dependencies -# command: | -# # your code here -# - run: -# name: Deploy backend -# command: | -# # your code here -# # Here's where you will add some code to rollback on failure + + deploy-backend: + docker: + - image: circleci/node:13.8.0 + steps: + - checkout + - persist_to_workspace: + root: ~/ + paths: + - project/.circleci/ + - add_ssh_keys: + fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] + - run: + name: Install dependencies + command: | + sudo apt -y update + sudo apt install -y tar gzip curl unzip + sudo apt install -y software-properties-common + sudo apt install -y python3 python3-pip + python3 -m pip install --user ansible + - run: + name: Install aws-cli + command: | + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + unzip awscliv2.zip + sudo ./aws/install + - run: + name: Deploy backend + command: | + cd backend + npm i + npm run build + cd .. + tar -C backend -czvf artifact.tar.gz . + cd .circleci/ansible + echo "Contents of the inventory.txt file is -------" + cat inventory.txt + ansible-playbook -i inventory.txt deploy-backend.yml + - destroy-environment # smoke-test: # docker: @@ -338,8 +360,8 @@ workflows: requires: [configure-infrastructure] - deploy-frontend: requires: [run-migrations] - # - deploy-backend: - # requires: [run-migrations] + - deploy-backend: + requires: [run-migrations] # - smoke-test: # requires: [deploy-backend, deploy-frontend] # - cloudfront-update: From 7d6e35ca2bbfd78267a49d846e1b2cea6be4531f Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 00:30:45 +0700 Subject: [PATCH 15/95] Update workspace of deploy-infrastructure job --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d81be42..27d4966 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -135,7 +135,7 @@ jobs: - persist_to_workspace: root: ~/ paths: - - project/.circleci/ansible/inventory.txt + - project/.circleci - run: name: Install tar and gzip command: | From 17f99b00b65c0df723613e75f37eb546bf621440 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 00:35:05 +0700 Subject: [PATCH 16/95] Update workspace of deploy-infrastructure job --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 27d4966..1b329a3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -132,10 +132,6 @@ jobs: - image: amazon/aws-cli steps: - checkout - - persist_to_workspace: - root: ~/ - paths: - - project/.circleci - run: name: Install tar and gzip command: | @@ -162,6 +158,10 @@ jobs: aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ --output text >> inventory + - persist_to_workspace: + root: ~/ + paths: + - project/.circleci/ansible/inventory.txt - destroy-environment configure-infrastructure: docker: From 50a443c2df7653425a90171117bc1c717b7c3f7b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 12:31:25 +0700 Subject: [PATCH 17/95] Update promotion phase --- .circleci/config.yml | 115 +++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1b329a3..063b7d0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,6 @@ commands: name: Destroy environments when: on_fail command: | - pip install awscli aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: @@ -18,16 +17,15 @@ commands: steps: - run: name: Revert migrations - # Add when this will run + when: on_fail command: | - # Curl command here to see if there was a successful migration associated with the workflow id, store result in SUCCESS variable + SUCCESS=$(curl --insecure https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_<< ${CIRCLE_WORKFLOW_ID:0:7} >>) SUCCESS = 1 if(( $SUCCESS==1 )); then - # cd ~/project/backend - # npm install - # Add revert code here. You can find this in the Getting Started section. - exit 1 + cd ~/project/backend + npm install + npm run migration:revert fi jobs: @@ -278,43 +276,66 @@ jobs: ansible-playbook -i inventory.txt deploy-backend.yml - destroy-environment -# smoke-test: -# docker: -# # Lightweight Docker image -# steps: -# # Checkout code from git -# - run: -# name: Install dependencies -# command: | -# # your code here -# - run: -# name: Get backend url -# command: | -# # your code here -# - run: -# name: Backend smoke test. -# command: | -# # your code here -# - run: -# name: Frontend smoke test. -# command: | -# # your code here -# # Here's where you will add some code to rollback on failure + smoke-test: + docker: + - image: technekes/circleci:alpine + steps: + - checkout + - run: + name: Install dependencies + command: | + apk add --no-cache curl + apk add --update nodejs npm + apk add --update py-pip + pip install awscli + - run: + name: Backend smoke test. + command: | + export API_URL="http://${BACKEND_IP}:3030" + echo "${API_URL}" + if curl "${API_URL}/api/status" | grep "ok" + then + return 0 + else + return 1 + fi + - run: + name: Frontend smoke test. + command: | + URL="http://udapeople-${CIRCLE_WORKFLOW_ID:0:7}.s3-website-us-east-1.amazonaws.com/#/employees" + echo ${URL} + if curl -s ${URL} | grep "Welcome" + then + return 1 + else + return 1 + fi + - destroy-environment + - revert-migrations -# cloudfront-update: -# docker: -# # Docker image here that supports AWS CLI -# steps: -# # Checkout code from git -# - run: -# name: Install dependencies -# command: | -# # your code here -# - run: -# name: Update cloudfront distribution -# command: | -# # your code here -# # Here's where you will add some code to rollback on failure + cloudfront-update: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Install dependencies + command: | + 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: Update cloudfront distribution + command: | + aws cloudformation deploy \ + --template-file .circleci/files/cloudfront.yml \ + --stack-name InitialStack \ + --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ + --tags project=udapeople + - destroy-environment + - revert-migrations # cleanup: # docker: @@ -362,9 +383,9 @@ workflows: requires: [run-migrations] - deploy-backend: requires: [run-migrations] - # - smoke-test: - # requires: [deploy-backend, deploy-frontend] - # - cloudfront-update: - # requires: [smoke-test] + - smoke-test: + requires: [deploy-backend, deploy-frontend] + - cloudfront-update: + requires: [smoke-test] # - cleanup: # requires: [cloudfront-update] \ No newline at end of file From d269a89684fc7032749d00dd867b20a9265cd0cc Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 12:43:48 +0700 Subject: [PATCH 18/95] Fixing commands in pinelines --- .circleci/config.yml | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 063b7d0..617fa04 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,6 +3,10 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. + parameters: + workflow_id: + type: string + default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Destroy environments @@ -13,21 +17,22 @@ commands: revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: - # Add parameter here + workflow_id: + type: string + default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Revert migrations when: on_fail command: | - SUCCESS=$(curl --insecure https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_<< ${CIRCLE_WORKFLOW_ID:0:7} >>) + SUCCESS=$(curl --insecure https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_<< parameters.workflow_id >>) SUCCESS = 1 if(( $SUCCESS==1 )); then cd ~/project/backend - npm install - npm run migration:revert - fi - + npm install + npm run migration:revert + fi jobs: build-frontend: docker: @@ -295,9 +300,9 @@ jobs: echo "${API_URL}" if curl "${API_URL}/api/status" | grep "ok" then - return 0 + return 0 else - return 1 + return 1 fi - run: name: Frontend smoke test. From ce400e6696a9b9e603bc3f3c429abe84ee2f24bd Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 17:45:56 +0700 Subject: [PATCH 19/95] Update pineline --- .../roles/configure-server/tasks/main.yml | 10 ++++++ .circleci/ansible/roles/deploy/tasks/main.yml | 35 +++++++++++-------- .circleci/config.yml | 7 ++-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.circleci/ansible/roles/configure-server/tasks/main.yml b/.circleci/ansible/roles/configure-server/tasks/main.yml index b093a2f..cfb7ff9 100644 --- a/.circleci/ansible/roles/configure-server/tasks/main.yml +++ b/.circleci/ansible/roles/configure-server/tasks/main.yml @@ -1,9 +1,19 @@ --- +- name: "update apt packages." + become: yes + apt: + update_cache: yes + - name: "upgrade packages." become: true apt: upgrade: "yes" +- name: remove dependencies that are no longer required + become: yes + apt: + autoremove: yes + - name: "install dependencies." become: true apt: diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index b093a2f..eff5601 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -1,20 +1,27 @@ --- -- name: "upgrade packages." +- name: copy dist backend folder to ec2 instance + become: yes + copy: + src: ~/project/artifact.tar.gz + dest: /home/ubuntu/artifact.tar.gz + +- name: prepare binaries become: true - apt: - upgrade: "yes" + shell: | + cd /home/ubuntu + tar xvzf artifact.tar.gz -C . -- name: "install dependencies." +- name: copy env file become: true - apt: - name: ["nodejs", "npm"] - state: latest - update_cache: yes + shell: cp /home/ubuntu/.env.sample /home/ubuntu/.env -- name: "install pm2" +- name: "start server" become: true - npm: - name: pm2 - global: yes - production: yes - state: present \ No newline at end of file + shell: + apt-get update + cd /home/ubuntu + npm install + pm2 stop default + pm2 start npm --start + pm2 startup + pm2 save \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 617fa04..a9e741d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,8 +12,7 @@ commands: name: Destroy environments when: on_fail command: | - aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} - + aws cloudformation delete-stack --stack-name udapeople-backend-parameters.workflow_id revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: @@ -166,6 +165,7 @@ jobs: paths: - project/.circleci/ansible/inventory.txt - destroy-environment + configure-infrastructure: docker: - image: python:3.7-alpine3.11 @@ -187,6 +187,7 @@ jobs: cd .circleci/ansible ansible-playbook -i inventory.txt configure-server.yml - destroy-environment + run-migrations: docker: - image: circleci/node:13.8.0 @@ -196,7 +197,7 @@ jobs: name: Run migrations command: | cd backend - npm install + npm install npm run migrations > migrations_dump.txt - run: name: Send migration results to memstash From 9343d1d2356cd27ecfd4200f5b1ac14eb685d687 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 18:03:46 +0700 Subject: [PATCH 20/95] Update host --- .circleci/ansible/configure-server.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index 42149a7..c6d760c 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -25,7 +25,7 @@ environment: - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" - - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" + - TYPEORM_HOST: "database-2.c2roen6ycxt3.us-east-1.rds.amazonaws.com" - TYPEORM_PORT: 5432 - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" From 85a2416678c54da5d6076fe1e97c9905c930b27a Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 18:31:52 +0700 Subject: [PATCH 21/95] Update host in configure-server --- .circleci/ansible/configure-server.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index c6d760c..42149a7 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -25,7 +25,7 @@ environment: - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" - - TYPEORM_HOST: "database-2.c2roen6ycxt3.us-east-1.rds.amazonaws.com" + - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" - TYPEORM_PORT: 5432 - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" From a7877d74e9ab61090ac248118097e938b7bc5667 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 23:32:44 +0700 Subject: [PATCH 22/95] Update bucket in kvdb --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a9e741d..dbe2ee7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,7 +24,7 @@ commands: name: Revert migrations when: on_fail command: | - SUCCESS=$(curl --insecure https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_<< parameters.workflow_id >>) + SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< parameters.workflow_id >>) SUCCESS = 1 if(( $SUCCESS==1 )); then @@ -205,7 +205,7 @@ jobs: cat ~/project/backend/migrations_dump.txt 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' + curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1' fi - destroy-environment From 292a25da9cd37d380c131937522a8a74a60de042 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 29 Oct 2023 23:53:15 +0700 Subject: [PATCH 23/95] Update deploy-frontend and deploy-backend jobs --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dbe2ee7..0baffa4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -217,12 +217,12 @@ jobs: - run: name: Install dependencies command: | - sudo yum update -y - sudo apt install -y tar + yum update -y + apt install -y tar tar --version - sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm + 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 + yum install -y nodejs node -v npm -v - run: From f1a05f7d01506875a525bac058aa6570c94e414b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 30 Oct 2023 00:04:37 +0700 Subject: [PATCH 24/95] Update image for deploy backend and deploy frontend jobs --- .circleci/config.yml | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0baffa4..a99bed5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -211,20 +211,18 @@ jobs: deploy-frontend: docker: - - image: amazon/aws-cli + - image: python:3.9.1-alpine3.12 steps: - checkout + - restore_cache: + keys: [frontend-build] - run: name: Install dependencies command: | - yum update -y - apt install -y tar - tar --version - 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 - - yum install -y nodejs - node -v - npm -v + apk update + apk add tar gzip + apk add npm nodejs + apk add --update ansible aws-cli - run: name: Get backend url command: | @@ -241,11 +239,12 @@ jobs: 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 + - destroy-environment + - revert-migrations deploy-backend: docker: - - image: circleci/node:13.8.0 + - image: python:3.9.1-alpine3.12 steps: - checkout - persist_to_workspace: @@ -257,17 +256,10 @@ jobs: - run: name: Install dependencies command: | - sudo apt -y update - sudo apt install -y tar gzip curl unzip - sudo apt install -y software-properties-common - sudo apt install -y python3 python3-pip - python3 -m pip install --user ansible - - run: - name: Install aws-cli - command: | - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" - unzip awscliv2.zip - sudo ./aws/install + apk update + apk add tar gzip + apk add npm nodejs + apk add --update ansible aws-cli - run: name: Deploy backend command: | @@ -281,6 +273,7 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - destroy-environment + - revert-migrations smoke-test: docker: From 3e6171cffb554239b1aa0936ebf2c207504e119b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 30 Oct 2023 15:56:58 +0700 Subject: [PATCH 25/95] Update config.yml --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a99bed5..2019244 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,7 @@ commands: name: Destroy environments when: on_fail command: | - aws cloudformation delete-stack --stack-name udapeople-backend-parameters.workflow_id + aws cloudformation delete-stack --stack-name udapeople-backend-${parameters.workflow_id} revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: @@ -24,7 +24,7 @@ commands: name: Revert migrations when: on_fail command: | - SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< parameters.workflow_id >>) + SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< ${parameters.workflow_id} >>) SUCCESS = 1 if(( $SUCCESS==1 )); then From 1d8cdf82a65433a4a1ed2c7419b297ad9256500c Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 30 Oct 2023 23:52:56 +0700 Subject: [PATCH 26/95] Update parameter in revert migration --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2019244..0ea2070 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,7 +24,7 @@ commands: name: Revert migrations when: on_fail command: | - SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< ${parameters.workflow_id} >>) + SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< parameters.workflow_id >>) SUCCESS = 1 if(( $SUCCESS==1 )); then From ef4b08ae21cd0f2d5a5f72004b2f49f0fc6341c4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 00:08:25 +0700 Subject: [PATCH 27/95] Update parameter overrides in deploy infrastructure --- .circleci/files/cloudfront.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/files/cloudfront.yml b/.circleci/files/cloudfront.yml index c51ef58..949c996 100644 --- a/.circleci/files/cloudfront.yml +++ b/.circleci/files/cloudfront.yml @@ -2,7 +2,7 @@ Description: > Cloudfront distribution for UdaPeople. Parameters: - WorkflowID: + NewWorkflowID: Description: Unique identifier. Type: String @@ -20,7 +20,7 @@ Resources: Properties: DistributionConfig: Origins: - - DomainName: !Sub "udapeople-${WorkflowID}.s3.amazonaws.com" + - DomainName: !Sub "udapeople-${NewWorkflowID}.s3.amazonaws.com" Id: webpage S3OriginConfig: OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}" @@ -33,8 +33,8 @@ Resources: ViewerProtocolPolicy: allow-all Outputs: - WorkflowID: - Value: !Sub ${WorkflowID} + NewWorkflowID: + Value: !Sub ${NewWorkflowID} Description: URL for website hosted on S3 Export: - Name: WorkflowID + Name: NewWorkflowID From eaf21913b141218856b64e22b4c9f51caed36016 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 00:22:53 +0700 Subject: [PATCH 28/95] Update parameter overrides in deploy infrastructure --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0ea2070..3332635 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -145,15 +145,15 @@ jobs: --template-file .circleci/files/backend.yml \ --tags project=cicd_project \ --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ - --parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" + --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" - run: name: Ensure front-end infrastructure exist command: | aws cloudformation deploy \ --template-file .circleci/files/frontend.yml \ --tags project=cicd_project \ - --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ - --parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" + --stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" - run: name: Add back-end ip to ansible inventory command: | From b1b8d903fd8ced9ec749c844374fa916209ecfa5 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 00:52:56 +0700 Subject: [PATCH 29/95] Update image in smokde test job --- .circleci/config.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3332635..8995b6e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -277,16 +277,16 @@ jobs: smoke-test: docker: - - image: technekes/circleci:alpine + - image: python:3.9.1-alpine3.12 steps: - checkout - run: name: Install dependencies command: | - apk add --no-cache curl - apk add --update nodejs npm - apk add --update py-pip - pip install awscli + apt-get update && sudo apt-get install -y curl + apt-get install -y nodejs npm + sudo apt-get install -y python3-pip + pip3 install awscli - run: name: Backend smoke test. command: | From 156f3450a4f065cef3f4dae78be04885f138cae6 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 01:07:53 +0700 Subject: [PATCH 30/95] Update image in smokde test job --- .circleci/config.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8995b6e..7148d5f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,7 @@ commands: name: Destroy environments when: on_fail command: | - aws cloudformation delete-stack --stack-name udapeople-backend-${parameters.workflow_id} + aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: @@ -283,9 +283,11 @@ jobs: - run: name: Install dependencies command: | - apt-get update && sudo apt-get install -y curl - apt-get install -y nodejs npm - sudo apt-get install -y python3-pip + apk update + apk add npm nodejs + apk add --no-cache curl + apk -v --update add --no-cache groff less python3 py3-pip + pip3 install --upgrade pip pip3 install awscli - run: name: Backend smoke test. From b83b020f4d19dc79529a36fb7647f69f39909c9d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 01:23:09 +0700 Subject: [PATCH 31/95] Update workspack in smoke test job --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7148d5f..a1c49f8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -25,7 +25,6 @@ commands: when: on_fail command: | SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< parameters.workflow_id >>) - SUCCESS = 1 if(( $SUCCESS==1 )); then cd ~/project/backend @@ -280,6 +279,8 @@ jobs: - image: python:3.9.1-alpine3.12 steps: - checkout + - attach_workspace: + at: ~/ - run: name: Install dependencies command: | From 3ade0c941263f39331f01c52cf2344362eef670b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 23:22:31 +0700 Subject: [PATCH 32/95] Fixing add ip into inventory file and export BACKEND_IP --- .circleci/config.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a1c49f8..fb90c70 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -158,7 +158,7 @@ jobs: command: | aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --output text >> inventory + --output text >> ~/project/.circleci/ansible/inventory.txt - persist_to_workspace: root: ~/ paths: @@ -225,7 +225,10 @@ jobs: - run: name: Get backend url command: | - export BACKEND_IP=$(aws ec2 describe-instances --query 'Reservations[*].Instances[*].PublicIpAddress') + export BACKEND_IP=$(aws ec2 describe-instances \ + --query 'Reservations[*].Instances[*].PublicIpAddress' \ + --filters "Name=tag:project,Values=udapeople" \ + --output text) export API_URL="http://${BACKEND_IP}:3030" echo "API_URL = ${API_URL}" echo API_URL="http://${BACKEND_IP}:3030" >> frontend/.env From 0d89813d5c2c33e67a48997372366d539f0d9a84 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 31 Oct 2023 23:37:02 +0700 Subject: [PATCH 33/95] Update logic to send keystore to kvdb and accessing from it --- .circleci/config.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index fb90c70..79ad3be 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -204,8 +204,12 @@ jobs: cat ~/project/backend/migrations_dump.txt if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then - curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1' + curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1' fi + - run: + name: Accessing key from kvdb.io + command: | + curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} - destroy-environment deploy-frontend: From 2e2b235f914a06ebfddf4c1f82ea4830761b915c Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 00:07:49 +0700 Subject: [PATCH 34/95] Update bucket kvdb --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 79ad3be..7548b41 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,7 +24,7 @@ commands: name: Revert migrations when: on_fail command: | - SUCCESS=$(curl --insecure https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_<< parameters.workflow_id >>) + SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>) if(( $SUCCESS==1 )); then cd ~/project/backend @@ -204,12 +204,12 @@ jobs: cat ~/project/backend/migrations_dump.txt if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then - curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1' + curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1' fi - run: name: Accessing key from kvdb.io command: | - curl https://kvdb.io/R5G4oDceoC3mRCseSvCa4S/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} + curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} - destroy-environment deploy-frontend: From 72f35472b26c4e3a767d404932e330b829614571 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 00:18:04 +0700 Subject: [PATCH 35/95] Update trust store with the latest trusted root --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7548b41..4d4f502 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,6 +24,7 @@ commands: name: Revert migrations when: on_fail command: | + echo "insecure" >> ~/.curlrc SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>) if(( $SUCCESS==1 )); then @@ -202,6 +203,7 @@ jobs: name: Send migration results to memstash command: | cat ~/project/backend/migrations_dump.txt + echo "insecure" >> ~/.curlrc if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1' From 713faf161ebb9e6a0974fbc37174172bb13b1212 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 00:35:17 +0700 Subject: [PATCH 36/95] Update to check ip backend in inventory file --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4d4f502..b459bb3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -13,6 +13,7 @@ commands: when: on_fail command: | aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} + aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: @@ -160,6 +161,7 @@ jobs: aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ --output text >> ~/project/.circleci/ansible/inventory.txt + cat ~/project/.circleci/ansible/inventory.txt - persist_to_workspace: root: ~/ paths: From 2c7e487611e74fa1c0a6612d7621761848776ab1 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 00:46:34 +0700 Subject: [PATCH 37/95] Update config.yml --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b459bb3..15b6c80 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -160,8 +160,8 @@ jobs: command: | aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ + --filters "Name=tag:project,Values=cicd_project" \ --output text >> ~/project/.circleci/ansible/inventory.txt - cat ~/project/.circleci/ansible/inventory.txt - persist_to_workspace: root: ~/ paths: @@ -235,7 +235,7 @@ jobs: command: | export BACKEND_IP=$(aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --filters "Name=tag:project,Values=udapeople" \ + --filters "Name=tag:project,Values=cicd_project" \ --output text) export API_URL="http://${BACKEND_IP}:3030" echo "API_URL = ${API_URL}" From 93d44be72f6ed69c16332f91cb679ebfc8ceef2c Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 00:58:27 +0700 Subject: [PATCH 38/95] Update config --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 15b6c80..00d1fe0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -162,6 +162,7 @@ jobs: --query 'Reservations[*].Instances[*].PublicIpAddress' \ --filters "Name=tag:project,Values=cicd_project" \ --output text >> ~/project/.circleci/ansible/inventory.txt + cat ~/project/.circleci/ansible/inventory.txt - persist_to_workspace: root: ~/ paths: From ea058fb024ace52bbbffb8f611538932becc29d4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 01:09:05 +0700 Subject: [PATCH 39/95] Update config.yml --- .circleci/config.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 00d1fe0..2518c61 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -160,9 +160,7 @@ jobs: command: | aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --filters "Name=tag:project,Values=cicd_project" \ --output text >> ~/project/.circleci/ansible/inventory.txt - cat ~/project/.circleci/ansible/inventory.txt - persist_to_workspace: root: ~/ paths: @@ -236,7 +234,6 @@ jobs: command: | export BACKEND_IP=$(aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --filters "Name=tag:project,Values=cicd_project" \ --output text) export API_URL="http://${BACKEND_IP}:3030" echo "API_URL = ${API_URL}" From 747deb09048c6df95b82a5f2fd0f23f8c29ec333 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 01:29:05 +0700 Subject: [PATCH 40/95] Update export BACKEND_IP --- .circleci/config.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2518c61..aaf80f0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -232,9 +232,7 @@ jobs: - run: name: Get backend url command: | - export BACKEND_IP=$(aws ec2 describe-instances \ - --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --output text) + export BACKEND_IP=$(cat ~/project/.circleci/ansible/inventory.txt | sed 1d) export API_URL="http://${BACKEND_IP}:3030" echo "API_URL = ${API_URL}" echo API_URL="http://${BACKEND_IP}:3030" >> frontend/.env From bcdeab7d4109aa1b0b06c98d9211a96186b66656 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 01:39:48 +0700 Subject: [PATCH 41/95] Update export BACKEND_IP --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index aaf80f0..b107064 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -232,6 +232,7 @@ jobs: - run: name: Get backend url command: | + cat ~/project/.circleci/ansible/inventory.txt export BACKEND_IP=$(cat ~/project/.circleci/ansible/inventory.txt | sed 1d) export API_URL="http://${BACKEND_IP}:3030" echo "API_URL = ${API_URL}" From 44b2cffd9a1a448ef6f84e2d543be446a8480d07 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 01:53:16 +0700 Subject: [PATCH 42/95] Update export BACKEND_IP --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index b107064..56cea65 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -186,6 +186,7 @@ jobs: name: Configure server command: | cd .circleci/ansible + cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - destroy-environment From 53a97dbf41313e1503ceb1a5c355ca6c625924ef Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 13:55:29 +0700 Subject: [PATCH 43/95] Update workspace in config.yml --- .circleci/config.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 56cea65..de97451 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -174,10 +174,8 @@ jobs: - 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: - - project/.circleci + - attach_workspace: + at: ~/ - run: name: Install dependencies command: | @@ -255,10 +253,8 @@ jobs: - image: python:3.9.1-alpine3.12 steps: - checkout - - persist_to_workspace: - root: ~/ - paths: - - project/.circleci/ + - deploy-frontend: + at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - run: From 4eaf51411a4958b095cab3a59696beb8f9d67985 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 14:06:26 +0700 Subject: [PATCH 44/95] Update workspace in config.yml --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index de97451..32548c0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -253,7 +253,7 @@ jobs: - image: python:3.9.1-alpine3.12 steps: - checkout - - deploy-frontend: + - attach_workspace: at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] From e69eb5165324d0b7893a3634d457592d286f0b1b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 14:35:59 +0700 Subject: [PATCH 45/95] Update path for configure infrastrcture and deploye backend job --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 32548c0..53552a5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -175,7 +175,7 @@ jobs: - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - attach_workspace: - at: ~/ + at: ~/project/.circleci/ansible/ - run: name: Install dependencies command: | @@ -254,7 +254,7 @@ jobs: steps: - checkout - attach_workspace: - at: ~/ + at: ~/project/.circleci/ansible/v - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - run: From 791883e9d7ac98e16f6ee66a728eceac47de5052 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 16:09:58 +0700 Subject: [PATCH 46/95] Update attach_workspace in smoke test job --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 53552a5..6952c8f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -254,7 +254,7 @@ jobs: steps: - checkout - attach_workspace: - at: ~/project/.circleci/ansible/v + at: ~/project/.circleci/ansible/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - run: @@ -285,7 +285,7 @@ jobs: steps: - checkout - attach_workspace: - at: ~/ + at: ~/project/.circleci/ansible/ - run: name: Install dependencies command: | From 8386b3af26c40d0da18e265ef3f673dfe6708f4f Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 16:38:59 +0700 Subject: [PATCH 47/95] Update config.yml --- .circleci/config.yml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6952c8f..51a1306 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -172,6 +172,8 @@ jobs: - image: python:3.7-alpine3.11 steps: - checkout + - attach_workspace: + at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - attach_workspace: @@ -254,7 +256,7 @@ jobs: steps: - checkout - attach_workspace: - at: ~/project/.circleci/ansible/ + at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - run: @@ -285,7 +287,7 @@ jobs: steps: - checkout - attach_workspace: - at: ~/project/.circleci/ansible/ + at: ~/ - run: name: Install dependencies command: | @@ -298,8 +300,11 @@ jobs: - run: name: Backend smoke test. command: | - export API_URL="http://${BACKEND_IP}:3030" - echo "${API_URL}" + export BACKEND_IP=$(aws ec2 describe-instances \ + --query 'Reservations[*].Instances[*].PublicIpAddress' \ + --output text) + echo ${BACKEND_IP} + echo ${API_URL} if curl "${API_URL}/api/status" | grep "ok" then return 0 From ca3a0cb60b91fbdcf94ea19d00a7c7d092b78297 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 16:45:13 +0700 Subject: [PATCH 48/95] Update config.yml --- .circleci/config.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 51a1306..d8c3106 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -176,8 +176,6 @@ jobs: at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] - - attach_workspace: - at: ~/project/.circleci/ansible/ - run: name: Install dependencies command: | From 5a6954364138d1ee96bf77cb35ddeb1732433cc3 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 16:59:13 +0700 Subject: [PATCH 49/95] Update config.yml --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d8c3106..24f5199 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -172,10 +172,10 @@ jobs: - image: python:3.7-alpine3.11 steps: - checkout - - attach_workspace: - at: ~/ - add_ssh_keys: fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f] + - attach_workspace: + at: ~/ - run: name: Install dependencies command: | From a4846fe916faab1e54cb5c8414e0e84332e4faec Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 17:16:46 +0700 Subject: [PATCH 50/95] Update config.yml --- .circleci/files/backend.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/files/backend.yml b/.circleci/files/backend.yml index 74047f6..f7877a5 100644 --- a/.circleci/files/backend.yml +++ b/.circleci/files/backend.yml @@ -29,7 +29,7 @@ Resources: SecurityGroups: - Ref: InstanceSecurityGroup KeyName: test-key2 - ImageId: ami-0fc5d935ebf8bc3bc + ImageId: ami-0d73480446600f555 Tags: - Key: Name Value: !Sub backend-${ID} From 149749fbcbf6c46d4292b29dbc65d8fc70a61ca5 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 17:29:26 +0700 Subject: [PATCH 51/95] Update install python in configure-server.yml --- .circleci/ansible/configure-server.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index 42149a7..75abca4 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -17,9 +17,9 @@ wait_for_connection: - name: "install python for Ansible." - become: true - raw: test -e ${ansible_python_interpreter} || (apt -y update && apt install -y python3) - changed_when: false + apt: + name: python3 + state: latest # Get the environment variables from CircleCI and add to the EC2 instance environment: From 21e2a1f080b9e25d61770df86fc265412185b29b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 17:41:20 +0700 Subject: [PATCH 52/95] Update install for main in configure-server --- .circleci/ansible/roles/configure-server/tasks/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/ansible/roles/configure-server/tasks/main.yml b/.circleci/ansible/roles/configure-server/tasks/main.yml index cfb7ff9..6a3b29e 100644 --- a/.circleci/ansible/roles/configure-server/tasks/main.yml +++ b/.circleci/ansible/roles/configure-server/tasks/main.yml @@ -5,7 +5,7 @@ update_cache: yes - name: "upgrade packages." - become: true + become: yes apt: upgrade: "yes" @@ -15,14 +15,14 @@ autoremove: yes - name: "install dependencies." - become: true + become: yes apt: name: ["nodejs", "npm"] state: latest update_cache: yes - name: "install pm2" - become: true + become: yes npm: name: pm2 global: yes From a8c7764bddf7e4af9cd9bb49e4b1e401b3e83b7d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 18:23:01 +0700 Subject: [PATCH 53/95] Update install for main in configure-server --- .circleci/ansible/roles/configure-server/tasks/main.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.circleci/ansible/roles/configure-server/tasks/main.yml b/.circleci/ansible/roles/configure-server/tasks/main.yml index 6a3b29e..bc5467a 100644 --- a/.circleci/ansible/roles/configure-server/tasks/main.yml +++ b/.circleci/ansible/roles/configure-server/tasks/main.yml @@ -14,6 +14,13 @@ apt: autoremove: yes +- name: Add Node.js repository + become: yes + apt_repository: + repo: deb https://deb.nodesource.com/node_16.x focal main + state: present + update_cache: yes + - name: "install dependencies." become: yes apt: From 1818e1c5b8002b14f5aca7598c55c316c339da27 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Wed, 1 Nov 2023 23:47:01 +0700 Subject: [PATCH 54/95] Update image for ec2 in backend.yml --- .circleci/ansible/roles/configure-server/tasks/main.yml | 7 ------- .circleci/files/backend.yml | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.circleci/ansible/roles/configure-server/tasks/main.yml b/.circleci/ansible/roles/configure-server/tasks/main.yml index bc5467a..6a3b29e 100644 --- a/.circleci/ansible/roles/configure-server/tasks/main.yml +++ b/.circleci/ansible/roles/configure-server/tasks/main.yml @@ -14,13 +14,6 @@ apt: autoremove: yes -- name: Add Node.js repository - become: yes - apt_repository: - repo: deb https://deb.nodesource.com/node_16.x focal main - state: present - update_cache: yes - - name: "install dependencies." become: yes apt: diff --git a/.circleci/files/backend.yml b/.circleci/files/backend.yml index f7877a5..d8c0e27 100644 --- a/.circleci/files/backend.yml +++ b/.circleci/files/backend.yml @@ -29,7 +29,7 @@ Resources: SecurityGroups: - Ref: InstanceSecurityGroup KeyName: test-key2 - ImageId: ami-0d73480446600f555 + ImageId: ami-068663a3c619dd892 Tags: - Key: Name Value: !Sub backend-${ID} From 0a054c7bd99ed68f936c6024a1c386ba6a82b7c4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 2 Nov 2023 00:15:47 +0700 Subject: [PATCH 55/95] Update apt update command for main in deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index eff5601..d38af59 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -18,7 +18,7 @@ - name: "start server" become: true shell: - apt-get update + apt update cd /home/ubuntu npm install pm2 stop default From 5b902e90b87808ad34abf3538d18a8187eb13521 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 2 Nov 2023 00:33:47 +0700 Subject: [PATCH 56/95] Update start for main.yml in deploy role and update package in deploy frontend job --- .circleci/ansible/roles/deploy/tasks/main.yml | 2 +- .circleci/config.yml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index d38af59..e7ea5a9 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -22,6 +22,6 @@ cd /home/ubuntu npm install pm2 stop default - pm2 start npm --start + pm2 start npm pm2 startup pm2 save \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 24f5199..9769d07 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -221,6 +221,8 @@ jobs: - checkout - restore_cache: keys: [frontend-build] + - attach_workspace: + at: ~/ - run: name: Install dependencies command: | From 1b8a7d5d2de5364a620eb76aa3ca6b13d0a54132 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 2 Nov 2023 00:55:32 +0700 Subject: [PATCH 57/95] Update script for main.yml in deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index e7ea5a9..3871f5a 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -18,10 +18,7 @@ - name: "start server" become: true shell: - apt update cd /home/ubuntu npm install pm2 stop default - pm2 start npm - pm2 startup - pm2 save \ No newline at end of file + pm2 start npm -- start \ No newline at end of file From aca293635509cf00ee192a4f06460dcf6b0485e5 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 2 Nov 2023 01:25:56 +0700 Subject: [PATCH 58/95] Update script for main.yml in smoke-test job --- .circleci/config.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9769d07..f20c009 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -193,6 +193,8 @@ jobs: - image: circleci/node:13.8.0 steps: - checkout + - restore_cache: + keys: [backend-build] - run: name: Run migrations command: | @@ -206,12 +208,12 @@ jobs: echo "insecure" >> ~/.curlrc if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then - curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1' + curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1' fi - run: name: Accessing key from kvdb.io command: | - curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} + curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - destroy-environment deploy-frontend: @@ -300,10 +302,9 @@ jobs: - run: name: Backend smoke test. command: | - export BACKEND_IP=$(aws ec2 describe-instances \ - --query 'Reservations[*].Instances[*].PublicIpAddress' \ - --output text) + export BACKEND_IP=$(cat ~/project/.circleci/ansible/inventory.txt | sed 1d) echo ${BACKEND_IP} + export API_URL="http://${BACKEND_IP}:3030" echo ${API_URL} if curl "${API_URL}/api/status" | grep "ok" then From 4d3aeb7e0279520b3b7744eef7a025926be0085c Mon Sep 17 00:00:00 2001 From: longbui98 Date: Thu, 2 Nov 2023 22:52:33 +0700 Subject: [PATCH 59/95] Testing after deploy backend --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f20c009..81168bb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -396,9 +396,9 @@ workflows: requires: [run-migrations] - deploy-backend: requires: [run-migrations] - - smoke-test: - requires: [deploy-backend, deploy-frontend] - - cloudfront-update: - requires: [smoke-test] + # - smoke-test: + # requires: [deploy-backend, deploy-frontend] + # - cloudfront-update: + # requires: [smoke-test] # - cleanup: # requires: [cloudfront-update] \ No newline at end of file From afcf30a8a9c0cc7e5976ab76e905005e46121bfe Mon Sep 17 00:00:00 2001 From: longbui98 Date: Fri, 3 Nov 2023 00:58:25 +0700 Subject: [PATCH 60/95] Update job of role deploy and preparing clean up job in config.yml --- .circleci/ansible/roles/deploy/tasks/main.yml | 4 --- .circleci/config.yml | 25 +++++++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index 3871f5a..dd589b4 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -11,10 +11,6 @@ cd /home/ubuntu tar xvzf artifact.tar.gz -C . -- name: copy env file - become: true - shell: cp /home/ubuntu/.env.sample /home/ubuntu/.env - - name: "start server" become: true shell: diff --git a/.circleci/config.yml b/.circleci/config.yml index 81168bb..43cb0c5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -352,23 +352,32 @@ jobs: # cleanup: # docker: -# # Docker image here +# - image: amazon/aws-cli # steps: -# # Checkout code from git +# - checkout # - run: # name: Get old stack workflow id -# command: | -# # your code here -# export OldWorkflowID="the id here" -# export STACKS=[] #put the list of stacks here +# command: | +# export OldWorkflowID=$(aws cloudformation \ +# list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ +# --no-paginate --output text) +# echo OldWorkflowID: "${OldWorkflowID}" +# echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" + +# export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ +# --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) +# echo Stack names: "${STACKS[@]}" # - run: # name: Remove old stacks and files # command: | # if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] # then -# # your code here +# aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive +# aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" +# aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" +# else +# echo "==========================Error while delete old stacks==========================" # fi - workflows: default: From 17949d00db854e201ec9bd0d5b39aabeefe7b803 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sat, 4 Nov 2023 00:06:44 +0700 Subject: [PATCH 61/95] Update step to start server for deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index dd589b4..7a21afc 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -15,6 +15,6 @@ become: true shell: cd /home/ubuntu - npm install - pm2 stop default - pm2 start npm -- start \ No newline at end of file + sudo npm install + sudo pm2 stop default + sudo m2 start npm -- start \ No newline at end of file From 5567e57998b011fbfb0d771b9bc4852169c8a386 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sat, 4 Nov 2023 01:24:50 +0700 Subject: [PATCH 62/95] Update step to start server in deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 4 +++- .circleci/config.yml | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index 7a21afc..70761e1 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -17,4 +17,6 @@ cd /home/ubuntu sudo npm install sudo pm2 stop default - sudo m2 start npm -- start \ No newline at end of file + sudo pm2 start npm -- start + sudo pm2 startup + sudo pm2 save \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 43cb0c5..2f9ee20 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -405,8 +405,8 @@ workflows: requires: [run-migrations] - deploy-backend: requires: [run-migrations] - # - smoke-test: - # requires: [deploy-backend, deploy-frontend] + - smoke-test: + requires: [deploy-backend, deploy-frontend] # - cloudfront-update: # requires: [smoke-test] # - cleanup: From f99c8e92bf86f147a3d9e85d0472c843af5e9be4 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sat, 4 Nov 2023 12:20:13 +0700 Subject: [PATCH 63/95] Add step to add .env in run-migration job --- .circleci/ansible/roles/deploy/tasks/main.yml | 12 +++++++----- .circleci/config.yml | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index 70761e1..9d3eeca 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -13,10 +13,12 @@ - name: "start server" become: true + become_method: sudo + become_user: root shell: cd /home/ubuntu - sudo npm install - sudo pm2 stop default - sudo pm2 start npm -- start - sudo pm2 startup - sudo pm2 save \ No newline at end of file + npm install + pm2 stop default + pm2 start npm -- start + pm2 startup + pm2 save \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 2f9ee20..8ca935a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,6 +33,14 @@ commands: npm install npm run migration:revert fi + ensure-backend-env-exists: + description: Add .env file due to connection problems + steps: + - run: + name: Create backend .env + command: | + echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" + >> backend/.env jobs: build-frontend: docker: @@ -214,6 +222,7 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} + - ensure-backend-env-exists - destroy-environment deploy-frontend: From c0a57f717fee8689bd0a7ea46614b17aaebcb41b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sat, 4 Nov 2023 12:37:00 +0700 Subject: [PATCH 64/95] Testing after add env for backend --- .circleci/config.yml | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8ca935a..3f27d25 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -359,34 +359,34 @@ jobs: - destroy-environment - revert-migrations -# cleanup: -# docker: -# - image: amazon/aws-cli -# steps: -# - checkout -# - run: -# name: Get old stack workflow id -# command: | -# export OldWorkflowID=$(aws cloudformation \ -# list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ -# --no-paginate --output text) -# echo OldWorkflowID: "${OldWorkflowID}" -# echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" +cleanup: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Get old stack workflow id + command: | + export OldWorkflowID=$(aws cloudformation \ + list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ + --no-paginate --output text) + echo OldWorkflowID: "${OldWorkflowID}" + echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" -# export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ -# --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) -# echo Stack names: "${STACKS[@]}" -# - run: -# name: Remove old stacks and files -# command: | -# if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] -# then -# aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive -# aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" -# aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" -# else -# echo "==========================Error while delete old stacks==========================" -# fi + export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ + --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) + echo Stack names: "${STACKS[@]}" + - run: + name: Remove old stacks and files + command: | + if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] + then + aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" + else + echo "==========================Error while delete old stacks==========================" + fi workflows: default: @@ -414,8 +414,8 @@ workflows: requires: [run-migrations] - deploy-backend: requires: [run-migrations] - - smoke-test: - requires: [deploy-backend, deploy-frontend] + # - smoke-test: + # requires: [deploy-backend, deploy-frontend] # - cloudfront-update: # requires: [smoke-test] # - cleanup: From 9abc5442a4b60c85d7fe041e50f212a93b06122f Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sat, 4 Nov 2023 13:06:33 +0700 Subject: [PATCH 65/95] Update add env into backend in deploy-backend --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3f27d25..c86fd41 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -222,7 +222,6 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - - ensure-backend-env-exists - destroy-environment deploy-frontend: @@ -289,6 +288,7 @@ jobs: echo "Contents of the inventory.txt file is -------" cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml + - ensure-backend-env-exists - destroy-environment - revert-migrations From e14cf17beaaabd8ff2aa09ab8d82f27d7520123e Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 01:26:40 +0700 Subject: [PATCH 66/95] Update add env to backend --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c86fd41..3c7751b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -40,7 +40,7 @@ commands: name: Create backend .env command: | echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" - >> backend/.env + >> ~/project/backend/.env jobs: build-frontend: docker: @@ -222,6 +222,7 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} + - ensure-backend-env-exists - destroy-environment deploy-frontend: @@ -288,7 +289,6 @@ jobs: echo "Contents of the inventory.txt file is -------" cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - - ensure-backend-env-exists - destroy-environment - revert-migrations From 1a50505a7416e0c6f8d4bfd40eb1d36189e3baa7 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 17:55:27 +0700 Subject: [PATCH 67/95] Update persitence workspace at run-migrations --- .circleci/config.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3c7751b..e089622 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -223,6 +223,10 @@ jobs: command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - ensure-backend-env-exists + - persist_to_workspace: + root: ~/ + paths: + - project/backend/ - destroy-environment deploy-frontend: From a6645fe83fc490786d4c7f5a5e70d574bea5b9f3 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 18:16:38 +0700 Subject: [PATCH 68/95] Update persist_to_workspace in run-mnigrations job --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e089622..dece5dc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -226,7 +226,7 @@ jobs: - persist_to_workspace: root: ~/ paths: - - project/backend/ + - project/ - destroy-environment deploy-frontend: From 7f155942fe50abdc162fb58e01d4c23ecf24cdf3 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 18:39:23 +0700 Subject: [PATCH 69/95] Update add enviroment variable step to deploy role --- .circleci/ansible/roles/deploy/tasks/main.yml | 8 +++++++- .circleci/config.yml | 13 ------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index 9d3eeca..1be15ce 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -11,11 +11,17 @@ cd /home/ubuntu tar xvzf artifact.tar.gz -C . +- name: Add environment variable + become: true + shell: | + echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" + >> /home/ubuntu/.env + - name: "start server" become: true become_method: sudo become_user: root - shell: + shell: | cd /home/ubuntu npm install pm2 stop default diff --git a/.circleci/config.yml b/.circleci/config.yml index dece5dc..5d343a5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,14 +33,6 @@ commands: npm install npm run migration:revert fi - ensure-backend-env-exists: - description: Add .env file due to connection problems - steps: - - run: - name: Create backend .env - command: | - echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" - >> ~/project/backend/.env jobs: build-frontend: docker: @@ -222,11 +214,6 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - - ensure-backend-env-exists - - persist_to_workspace: - root: ~/ - paths: - - project/ - destroy-environment deploy-frontend: From b4cf99357a08936dbc91a54f9319ef99d1832a9f Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 19:03:35 +0700 Subject: [PATCH 70/95] Update add enviroment variable step to configure-server file --- .circleci/ansible/configure-server.yml | 16 ++++++++-------- .circleci/ansible/roles/deploy/tasks/main.yml | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index 75abca4..e73459e 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -23,14 +23,14 @@ # Get the environment variables from CircleCI and add to the EC2 instance environment: - - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" - - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" - - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" + - TYPEORM_CONNECTION: "{{ lookup('env.sample', 'TYPEORM_CONNECTION')}}" + - TYPEORM_ENTITIES: "{{ lookup('env.sample', 'TYPEORM_ENTITIES')}}" + - TYPEORM_HOST: "{{ lookup('env.sample', 'TYPEORM_HOST')}}" - TYPEORM_PORT: 5432 - - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" - - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" - - TYPEORM_DATABASE: "{{ lookup('env', 'TYPEORM_DATABASE')}}" - - TYPEORM_MIGRATIONS: "{{ lookup('env', 'TYPEORM_MIGRATIONS')}}" - - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env', 'TYPEORM_MIGRATIONS_DIR')}}" + - TYPEORM_USERNAME: "{{ lookup('env.sample', 'TYPEORM_USERNAME')}}" + - TYPEORM_PASSWORD: "{{ lookup('env.sample', 'TYPEORM_PASSWORD')}}" + - TYPEORM_DATABASE: "{{ lookup('env.sample', 'TYPEORM_DATABASE')}}" + - TYPEORM_MIGRATIONS: "{{ lookup('env.sample', 'TYPEORM_MIGRATIONS')}}" + - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env.sample', 'TYPEORM_MIGRATIONS_DIR')}}" roles: - configure-server \ No newline at end of file diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index 1be15ce..cec4c8a 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -11,11 +11,11 @@ cd /home/ubuntu tar xvzf artifact.tar.gz -C . -- name: Add environment variable - become: true - shell: | - echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" - >> /home/ubuntu/.env +# - name: Add environment variable +# become: true +# shell: | +# echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" +# >> /home/ubuntu/.env - name: "start server" become: true From 7038a35976c9bb065df0bf4478ceddd5537269c1 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 19:08:53 +0700 Subject: [PATCH 71/95] Update .env file --- .circleci/ansible/roles/deploy/tasks/main.yml | 6 ------ backend/.env.sample | 13 ------------- 2 files changed, 19 deletions(-) delete mode 100644 backend/.env.sample diff --git a/.circleci/ansible/roles/deploy/tasks/main.yml b/.circleci/ansible/roles/deploy/tasks/main.yml index cec4c8a..bfb916f 100644 --- a/.circleci/ansible/roles/deploy/tasks/main.yml +++ b/.circleci/ansible/roles/deploy/tasks/main.yml @@ -11,12 +11,6 @@ cd /home/ubuntu tar xvzf artifact.tar.gz -C . -# - name: Add environment variable -# become: true -# shell: | -# echo -e "NODE_ENV=local\nVERSION=1\nTYPEORM_CONNECTION=${TYPEORM_CONNECTION}\nTYPEORM_MIGRATIONS_DIR=${TYPEORM_MIGRATIONS_DIR}\nTYPEORM_ENTITIES=${TYPEORM_ENTITIES}\nTYPEORM_MIGRATIONS=${TYPEORM_MIGRATIONS}\nTYPEORM_HOST=${TYPEORM_HOST}\nTYPEORM_PORT=${TYPEORM_PORT}\nTYPEORM_USERNAME=${TYPEORM_USERNAME}\nTYPEORM_PASSWORD=${TYPEORM_PASSWORD}\nTYPEORM_DATABASE=${TYPEORM_DATABASE}" -# >> /home/ubuntu/.env - - name: "start server" become: true become_method: sudo diff --git a/backend/.env.sample b/backend/.env.sample deleted file mode 100644 index 3b85707..0000000 --- a/backend/.env.sample +++ /dev/null @@ -1,13 +0,0 @@ -NODE_ENV=local -VERSION=1 -TYPEORM_CONNECTION=postgres -TYPEORM_MIGRATIONS_DIR=./src/migrations -TYPEORM_ENTITIES=./src/modules/domain/**/*.entity.ts -TYPEORM_MIGRATIONS=./src/migrations/*.ts - -# Use these values for the local PG database from the Docker Compose file -TYPEORM_HOST=localhost -TYPEORM_PORT=5532 -TYPEORM_USERNAME=postgres -TYPEORM_PASSWORD=password -TYPEORM_DATABASE=glee \ No newline at end of file From caeb3e22d5c4c158102bae975694543874fa071e Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 19:16:38 +0700 Subject: [PATCH 72/95] Update .env file --- .circleci/ansible/configure-server.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index e73459e..75abca4 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -23,14 +23,14 @@ # Get the environment variables from CircleCI and add to the EC2 instance environment: - - TYPEORM_CONNECTION: "{{ lookup('env.sample', 'TYPEORM_CONNECTION')}}" - - TYPEORM_ENTITIES: "{{ lookup('env.sample', 'TYPEORM_ENTITIES')}}" - - TYPEORM_HOST: "{{ lookup('env.sample', 'TYPEORM_HOST')}}" + - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" + - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" + - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" - TYPEORM_PORT: 5432 - - TYPEORM_USERNAME: "{{ lookup('env.sample', 'TYPEORM_USERNAME')}}" - - TYPEORM_PASSWORD: "{{ lookup('env.sample', 'TYPEORM_PASSWORD')}}" - - TYPEORM_DATABASE: "{{ lookup('env.sample', 'TYPEORM_DATABASE')}}" - - TYPEORM_MIGRATIONS: "{{ lookup('env.sample', 'TYPEORM_MIGRATIONS')}}" - - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env.sample', 'TYPEORM_MIGRATIONS_DIR')}}" + - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" + - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" + - TYPEORM_DATABASE: "{{ lookup('env', 'TYPEORM_DATABASE')}}" + - TYPEORM_MIGRATIONS: "{{ lookup('env', 'TYPEORM_MIGRATIONS')}}" + - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env', 'TYPEORM_MIGRATIONS_DIR')}}" roles: - configure-server \ No newline at end of file From 0c1ba3c34cc9c3ef10791557b00b8edcb5ae8c22 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 19:44:52 +0700 Subject: [PATCH 73/95] Revert .env.sample --- backend/.env.sample | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 backend/.env.sample diff --git a/backend/.env.sample b/backend/.env.sample new file mode 100644 index 0000000..a1dd31c --- /dev/null +++ b/backend/.env.sample @@ -0,0 +1,13 @@ +NODE_ENV=local +VERSION=1 +TYPEORM_CONNECTION=postgres +TYPEORM_MIGRATIONS_DIR=./src/migrations +TYPEORM_ENTITIES=./src/modules/domain/**/*.entity.ts +TYPEORM_MIGRATIONS=./src/migrations/*.ts + +# Use these values for the local PG database from the Docker Compose file +TYPEORM_HOST=localhost +TYPEORM_PORT=5432 +TYPEORM_USERNAME=postgres +TYPEORM_PASSWORD=password +TYPEORM_DATABASE=glee \ No newline at end of file From bbaa7acaef8df56596ab5f300ef0b1e4d019a21d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 22:52:41 +0700 Subject: [PATCH 74/95] Add step to add env circleci to env in deploy backend job --- .circleci/config.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5d343a5..e83ac61 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -268,6 +268,21 @@ jobs: apk add tar gzip apk add npm nodejs apk add --update ansible aws-cli + - run: + name: Configure server + no_output_timeout: 5m + command: | + cd backend + touch .env + echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env + echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env + echo TYPEORM_HOST=$TYPEORM_HOST >> .env + echo TYPEORM_PORT=$TYPEORM_PORT >> .env + echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env + echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env + echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env + echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env + echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env - run: name: Deploy backend command: | From 266daf2996d3895be8601ca770bb966d1212b787 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 22:56:21 +0700 Subject: [PATCH 75/95] Add step to add env circleci to env in deploy configure-infrastructure job --- .circleci/config.yml | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e83ac61..270ba8c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -183,6 +183,17 @@ jobs: - run: name: Configure server command: | + cd backend + echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env + echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env + echo TYPEORM_HOST=$TYPEORM_HOST >> .env + echo TYPEORM_PORT=$TYPEORM_PORT >> .env + echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env + echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env + echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env + echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env + echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env + cd .. cd .circleci/ansible cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml @@ -268,21 +279,6 @@ jobs: apk add tar gzip apk add npm nodejs apk add --update ansible aws-cli - - run: - name: Configure server - no_output_timeout: 5m - command: | - cd backend - touch .env - echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env - echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env - echo TYPEORM_HOST=$TYPEORM_HOST >> .env - echo TYPEORM_PORT=$TYPEORM_PORT >> .env - echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env - echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env - echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env - echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env - echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env - run: name: Deploy backend command: | From 488f8378292ff93568bc3eedd44a0786a8124181 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 23:15:23 +0700 Subject: [PATCH 76/95] Add step to add env circleci to env in deploy-infrastructure job --- .circleci/config.yml | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 270ba8c..9a30e96 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -155,6 +155,19 @@ jobs: --tags project=cicd_project \ --stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \ --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" + - run: + name: Add env to backend + command: | + cd ~/project/backend + echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env + echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env + echo TYPEORM_HOST=$TYPEORM_HOST >> .env + echo TYPEORM_PORT=$TYPEORM_PORT >> .env + echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env + echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env + echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env + echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env + echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env - run: name: Add back-end ip to ansible inventory command: | @@ -164,6 +177,7 @@ jobs: - persist_to_workspace: root: ~/ paths: + - project/backend/.env - project/.circleci/ansible/inventory.txt - destroy-environment @@ -183,17 +197,6 @@ jobs: - run: name: Configure server command: | - cd backend - echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env - echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env - echo TYPEORM_HOST=$TYPEORM_HOST >> .env - echo TYPEORM_PORT=$TYPEORM_PORT >> .env - echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env - echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env - echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env - echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env - echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env - cd .. cd .circleci/ansible cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml From 25c497b929a1c01ffd889fae147098b94653b78b Mon Sep 17 00:00:00 2001 From: longbui98 Date: Sun, 5 Nov 2023 23:48:34 +0700 Subject: [PATCH 77/95] Test smoke test job --- .circleci/ansible/configure-server.yml | 13 +------------ .circleci/config.yml | 4 ++-- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index 75abca4..267bcb8 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -20,17 +20,6 @@ apt: name: python3 state: latest - - # Get the environment variables from CircleCI and add to the EC2 instance - environment: - - TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}" - - TYPEORM_ENTITIES: "{{ lookup('env', 'TYPEORM_ENTITIES')}}" - - TYPEORM_HOST: "{{ lookup('env', 'TYPEORM_HOST')}}" - - TYPEORM_PORT: 5432 - - TYPEORM_USERNAME: "{{ lookup('env', 'TYPEORM_USERNAME')}}" - - TYPEORM_PASSWORD: "{{ lookup('env', 'TYPEORM_PASSWORD')}}" - - TYPEORM_DATABASE: "{{ lookup('env', 'TYPEORM_DATABASE')}}" - - TYPEORM_MIGRATIONS: "{{ lookup('env', 'TYPEORM_MIGRATIONS')}}" - - TYPEORM_MIGRATIONS_DIR: "{{ lookup('env', 'TYPEORM_MIGRATIONS_DIR')}}" + roles: - configure-server \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 9a30e96..677ae51 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -419,8 +419,8 @@ workflows: requires: [run-migrations] - deploy-backend: requires: [run-migrations] - # - smoke-test: - # requires: [deploy-backend, deploy-frontend] + - smoke-test: + requires: [deploy-backend, deploy-frontend] # - cloudfront-update: # requires: [smoke-test] # - cleanup: From 28e4537b16d1176e5b5fbb77de53fade77c211b7 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 00:08:42 +0700 Subject: [PATCH 78/95] Fix smoke test for test fe and test cloudfront-update job --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 677ae51..6d20f4d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -333,7 +333,7 @@ jobs: echo ${URL} if curl -s ${URL} | grep "Welcome" then - return 1 + return 0 else return 1 fi @@ -421,7 +421,7 @@ workflows: requires: [run-migrations] - smoke-test: requires: [deploy-backend, deploy-frontend] - # - cloudfront-update: - # requires: [smoke-test] + - cloudfront-update: + requires: [smoke-test] # - cleanup: # requires: [cloudfront-update] \ No newline at end of file From 281923544614317795bea1db8bbb23311d3c7310 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 00:32:39 +0700 Subject: [PATCH 79/95] Fix smoke test for test fe and test cloudfront-update job --- .circleci/config.yml | 85 ++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6d20f4d..0dc4957 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,8 +12,8 @@ commands: name: Destroy environments when: on_fail command: | - aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} - aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} + aws cloudformation delete-stack --stack-name udapeople-backend-<< parameters.workflow_id >> + aws cloudformation delete-stack --stack-name udapeople-frontend-<< parameters.workflow_id >> revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: @@ -179,7 +179,8 @@ jobs: paths: - project/backend/.env - project/.circleci/ansible/inventory.txt - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} configure-infrastructure: docker: @@ -200,7 +201,8 @@ jobs: cd .circleci/ansible cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} run-migrations: docker: @@ -228,7 +230,8 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} deploy-frontend: docker: @@ -263,7 +266,8 @@ jobs: 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 + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations deploy-backend: @@ -294,7 +298,8 @@ jobs: echo "Contents of the inventory.txt file is -------" cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations smoke-test: @@ -337,7 +342,8 @@ jobs: else return 1 fi - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations cloudfront-update: @@ -348,9 +354,9 @@ jobs: - run: name: Install dependencies command: | - sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm + 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 + yum install -y nodejs node -v npm -v - run: @@ -361,37 +367,38 @@ jobs: --stack-name InitialStack \ --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - - destroy-environment + - destroy-environment: + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations -cleanup: - docker: - - image: amazon/aws-cli - steps: - - checkout - - run: - name: Get old stack workflow id - command: | - export OldWorkflowID=$(aws cloudformation \ - list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ - --no-paginate --output text) - echo OldWorkflowID: "${OldWorkflowID}" - echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" - - export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ - --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) - echo Stack names: "${STACKS[@]}" - - run: - name: Remove old stacks and files - command: | - if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] - then - aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive - aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" - aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" - else - echo "==========================Error while delete old stacks==========================" - fi + cleanup: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Get old stack workflow id + command: | + export OldWorkflowID=$(aws cloudformation \ + list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ + --no-paginate --output text) + echo OldWorkflowID: "${OldWorkflowID}" + echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" + + export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ + --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) + echo Stack names: "${STACKS[@]}" + - run: + name: Remove old stacks and files + command: | + if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] + then + aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" + else + echo "==========================Error while delete old stacks==========================" + fi workflows: default: From 6ceecc2e98817389dca8beb752f50db0c215b9e3 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 00:52:25 +0700 Subject: [PATCH 80/95] Add remove s3 in destroy environment and update cloudfront update job --- .circleci/config.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0dc4957..97cb285 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,6 +12,9 @@ commands: name: Destroy environments when: on_fail command: | + if aws s3 ls | grep udapeople-<>; then + aws s3 rm s3://udapeople-<> --recursive + fi aws cloudformation delete-stack --stack-name udapeople-backend-<< parameters.workflow_id >> aws cloudformation delete-stack --stack-name udapeople-frontend-<< parameters.workflow_id >> revert-migrations: @@ -355,7 +358,7 @@ jobs: name: Install dependencies command: | 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 - + curl -sL https://rpm.nodesource.com/setup_14.x | -E bash - yum install -y nodejs node -v npm -v From fe15f8bed896c1175e22b1135eba4954e6e1e368 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:00:00 +0700 Subject: [PATCH 81/95] Update config.yml --- .circleci/config.yml | 60 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 97cb285..3134fc0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,9 +4,8 @@ commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. parameters: - workflow_id: + id: type: string - default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Destroy environments @@ -15,21 +14,20 @@ commands: if aws s3 ls | grep udapeople-<>; then aws s3 rm s3://udapeople-<> --recursive fi - aws cloudformation delete-stack --stack-name udapeople-backend-<< parameters.workflow_id >> - aws cloudformation delete-stack --stack-name udapeople-frontend-<< parameters.workflow_id >> + aws cloudformation delete-stack --stack-name udapeople-backend-<< parameters.id >> + aws cloudformation delete-stack --stack-name udapeople-frontend-<< parameters.id >> revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: - workflow_id: + id: type: string - default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Revert migrations when: on_fail command: | echo "insecure" >> ~/.curlrc - SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>) + SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.id >>) if(( $SUCCESS==1 )); then cd ~/project/backend @@ -148,16 +146,16 @@ jobs: aws cloudformation deploy \ --template-file .circleci/files/backend.yml \ --tags project=cicd_project \ - --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ - --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" + --stack-name "udapeople-backend-${CIRCLE_id:0:7}" \ + --parameter-overrides ID="${CIRCLE_id:0:7}" - run: name: Ensure front-end infrastructure exist command: | aws cloudformation deploy \ --template-file .circleci/files/frontend.yml \ --tags project=cicd_project \ - --stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \ - --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" + --stack-name "udapeople-frontend-${CIRCLE_id:0:7}" \ + --parameter-overrides ID="${CIRCLE_id:0:7}" - run: name: Add env to backend command: | @@ -183,7 +181,7 @@ jobs: - project/backend/.env - project/.circleci/ansible/inventory.txt - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + id : ${CIRCLE_id:0:7} configure-infrastructure: docker: @@ -205,7 +203,7 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + id : ${CIRCLE_id:0:7} run-migrations: docker: @@ -227,14 +225,14 @@ jobs: echo "insecure" >> ~/.curlrc if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then - curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1' + curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_id:0:7} -d '1' fi - run: name: Accessing key from kvdb.io command: | - curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} + curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_id:0:7} - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + id : ${CIRCLE_id:0:7} deploy-frontend: docker: @@ -267,11 +265,12 @@ jobs: cd frontend 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 + tar -czvf artifact-"${CIRCLE_id:0:7}".tar.gz dist + aws s3 cp dist s3://udapeople-${CIRCLE_id:0:7} --recursive - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations + id : ${CIRCLE_id:0:7} + - revert-migrations: + id : ${CIRCLE_id:0:7} deploy-backend: docker: @@ -302,8 +301,9 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations + id : ${CIRCLE_id:0:7} + - revert-migrations: + id : ${CIRCLE_id:0:7} smoke-test: docker: @@ -337,7 +337,7 @@ jobs: - run: name: Frontend smoke test. command: | - URL="http://udapeople-${CIRCLE_WORKFLOW_ID:0:7}.s3-website-us-east-1.amazonaws.com/#/employees" + URL="http://udapeople-${CIRCLE_id:0:7}.s3-website-us-east-1.amazonaws.com/#/employees" echo ${URL} if curl -s ${URL} | grep "Welcome" then @@ -346,8 +346,9 @@ jobs: return 1 fi - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations + id : ${CIRCLE_id:0:7} + - revert-migrations: + id : ${CIRCLE_id:0:7} cloudfront-update: docker: @@ -368,11 +369,12 @@ jobs: aws cloudformation deploy \ --template-file .circleci/files/cloudfront.yml \ --stack-name InitialStack \ - --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides WorkflowID="udapeople-${CIRCLE_id:0:7}" \ --tags project=udapeople - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations + id : ${CIRCLE_id:0:7} + - revert-migrations: + id : ${CIRCLE_id:0:7} cleanup: docker: @@ -386,7 +388,7 @@ jobs: list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ --no-paginate --output text) echo OldWorkflowID: "${OldWorkflowID}" - echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" + echo CIRCLE_id "${CIRCLE_id:0:7}" export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) From 331ba0305636294d8aa5d3c04e913bd6306ba821 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:09:45 +0700 Subject: [PATCH 82/95] Update config.yml --- .circleci/config.yml | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3134fc0..9e9e387 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -146,16 +146,16 @@ jobs: aws cloudformation deploy \ --template-file .circleci/files/backend.yml \ --tags project=cicd_project \ - --stack-name "udapeople-backend-${CIRCLE_id:0:7}" \ - --parameter-overrides ID="${CIRCLE_id:0:7}" + --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" - run: name: Ensure front-end infrastructure exist command: | aws cloudformation deploy \ --template-file .circleci/files/frontend.yml \ --tags project=cicd_project \ - --stack-name "udapeople-frontend-${CIRCLE_id:0:7}" \ - --parameter-overrides ID="${CIRCLE_id:0:7}" + --stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" - run: name: Add env to backend command: | @@ -181,7 +181,7 @@ jobs: - project/backend/.env - project/.circleci/ansible/inventory.txt - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} configure-infrastructure: docker: @@ -203,7 +203,7 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} run-migrations: docker: @@ -225,14 +225,14 @@ jobs: echo "insecure" >> ~/.curlrc if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt then - curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_id:0:7} -d '1' + curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1' fi - run: name: Accessing key from kvdb.io command: | - curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_id:0:7} + curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} deploy-frontend: docker: @@ -265,12 +265,12 @@ jobs: cd frontend npm install npm run build - tar -czvf artifact-"${CIRCLE_id:0:7}".tar.gz dist - aws s3 cp dist s3://udapeople-${CIRCLE_id:0:7} --recursive + 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: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} deploy-backend: docker: @@ -301,9 +301,9 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} smoke-test: docker: @@ -337,7 +337,7 @@ jobs: - run: name: Frontend smoke test. command: | - URL="http://udapeople-${CIRCLE_id:0:7}.s3-website-us-east-1.amazonaws.com/#/employees" + URL="http://udapeople-${CIRCLE_WORKFLOW_ID:0:7}.s3-website-us-east-1.amazonaws.com/#/employees" echo ${URL} if curl -s ${URL} | grep "Welcome" then @@ -346,9 +346,9 @@ jobs: return 1 fi - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} cloudfront-update: docker: @@ -369,12 +369,12 @@ jobs: aws cloudformation deploy \ --template-file .circleci/files/cloudfront.yml \ --stack-name InitialStack \ - --parameter-overrides WorkflowID="udapeople-${CIRCLE_id:0:7}" \ + --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - destroy-environment: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} - revert-migrations: - id : ${CIRCLE_id:0:7} + id : ${CIRCLE_WORKFLOW_ID:0:7} cleanup: docker: @@ -388,7 +388,7 @@ jobs: list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ --no-paginate --output text) echo OldWorkflowID: "${OldWorkflowID}" - echo CIRCLE_id "${CIRCLE_id:0:7}" + echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) From 505ae711e8d7e29b1d91884982eb6163b7b57c40 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:23:57 +0700 Subject: [PATCH 83/95] Update config.yml --- .circleci/config.yml | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9e9e387..d16c35b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,22 +4,19 @@ commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. parameters: - id: + workflow_id: type: string steps: - run: name: Destroy environments when: on_fail command: | - if aws s3 ls | grep udapeople-<>; then - aws s3 rm s3://udapeople-<> --recursive - fi - aws cloudformation delete-stack --stack-name udapeople-backend-<< parameters.id >> - aws cloudformation delete-stack --stack-name udapeople-frontend-<< parameters.id >> + aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} + aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. parameters: - id: + workflow_id: type: string steps: - run: @@ -27,7 +24,7 @@ commands: when: on_fail command: | echo "insecure" >> ~/.curlrc - SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.id >>) + SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7}) if(( $SUCCESS==1 )); then cd ~/project/backend @@ -181,7 +178,7 @@ jobs: - project/backend/.env - project/.circleci/ansible/inventory.txt - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} configure-infrastructure: docker: @@ -203,7 +200,7 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} run-migrations: docker: @@ -232,7 +229,7 @@ jobs: command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} deploy-frontend: docker: @@ -268,9 +265,8 @@ jobs: 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: - id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - revert-migrations deploy-backend: docker: @@ -301,9 +297,8 @@ jobs: cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - revert-migrations smoke-test: docker: @@ -346,9 +341,8 @@ jobs: return 1 fi - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - revert-migrations cloudfront-update: docker: @@ -372,9 +366,8 @@ jobs: --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - destroy-environment: - id : ${CIRCLE_WORKFLOW_ID:0:7} - - revert-migrations: - id : ${CIRCLE_WORKFLOW_ID:0:7} + workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - revert-migrations cleanup: docker: From f323658141faece0cf7e6d194c40cd7e86d40dec Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:34:09 +0700 Subject: [PATCH 84/95] Update config.yml --- .circleci/config.yml | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d16c35b..875cb8f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,21 +3,18 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. - parameters: - workflow_id: - type: string steps: - run: name: Destroy environments when: on_fail command: | + if aws s3 ls | grep udapeople-<>; then + aws s3 rm s3://udapeople-<> --recursive + fi aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. - parameters: - workflow_id: - type: string steps: - run: name: Revert migrations @@ -177,8 +174,7 @@ jobs: paths: - project/backend/.env - project/.circleci/ansible/inventory.txt - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment configure-infrastructure: docker: @@ -199,8 +195,7 @@ jobs: cd .circleci/ansible cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment run-migrations: docker: @@ -228,8 +223,7 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment deploy-frontend: docker: @@ -264,8 +258,7 @@ jobs: 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: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment - revert-migrations deploy-backend: @@ -296,8 +289,7 @@ jobs: echo "Contents of the inventory.txt file is -------" cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment - revert-migrations smoke-test: @@ -340,8 +332,7 @@ jobs: else return 1 fi - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment - revert-migrations cloudfront-update: @@ -365,8 +356,7 @@ jobs: --stack-name InitialStack \ --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - - destroy-environment: - workflow_id : ${CIRCLE_WORKFLOW_ID:0:7} + - destroy-environment - revert-migrations cleanup: From fe204f7c7eee62e770b985fbae0770421688875d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:45:00 +0700 Subject: [PATCH 85/95] Update config.yml --- .circleci/config.yml | 81 +++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 875cb8f..c67b92d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,25 +3,30 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. + parameters: + workflow_id: + type: string + default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Destroy environments when: on_fail command: | - if aws s3 ls | grep udapeople-<>; then - aws s3 rm s3://udapeople-<> --recursive - fi aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. + parameters: + workflow_id: + type: string + default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Revert migrations when: on_fail command: | echo "insecure" >> ~/.curlrc - SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7}) + SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>) if(( $SUCCESS==1 )); then cd ~/project/backend @@ -195,7 +200,7 @@ jobs: cd .circleci/ansible cat inventory.txt ansible-playbook -i inventory.txt configure-server.yml - - destroy-environment + - destroy-environment run-migrations: docker: @@ -223,7 +228,7 @@ jobs: name: Accessing key from kvdb.io command: | curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7} - - destroy-environment + - destroy-environment deploy-frontend: docker: @@ -258,7 +263,7 @@ jobs: 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 + - destroy-environment - revert-migrations deploy-backend: @@ -289,7 +294,7 @@ jobs: echo "Contents of the inventory.txt file is -------" cat inventory.txt ansible-playbook -i inventory.txt deploy-backend.yml - - destroy-environment + - destroy-environment - revert-migrations smoke-test: @@ -359,34 +364,34 @@ jobs: - destroy-environment - revert-migrations - cleanup: - docker: - - image: amazon/aws-cli - steps: - - checkout - - run: - name: Get old stack workflow id - command: | - export OldWorkflowID=$(aws cloudformation \ - list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ - --no-paginate --output text) - echo OldWorkflowID: "${OldWorkflowID}" - echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" - - export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ - --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) - echo Stack names: "${STACKS[@]}" - - run: - name: Remove old stacks and files - command: | - if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] - then - aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive - aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" - aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" - else - echo "==========================Error while delete old stacks==========================" - fi +cleanup: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Get old stack workflow id + command: | + export OldWorkflowID=$(aws cloudformation \ + list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ + --no-paginate --output text) + echo OldWorkflowID: "${OldWorkflowID}" + echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" + + export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ + --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) + echo Stack names: "${STACKS[@]}" + - run: + name: Remove old stacks and files + command: | + if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] + then + aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" + else + echo "==========================Error while delete old stacks==========================" + fi workflows: default: @@ -418,5 +423,5 @@ workflows: requires: [deploy-backend, deploy-frontend] - cloudfront-update: requires: [smoke-test] - # - cleanup: - # requires: [cloudfront-update] \ No newline at end of file + - cleanup: + requires: [cloudfront-update] \ No newline at end of file From 6f2764cad9b88c3860a7e7088d20eeef7da20901 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 01:47:51 +0700 Subject: [PATCH 86/95] Update config.yml --- .circleci/config.yml | 66 +++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c67b92d..8e460bc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,10 +3,6 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. - parameters: - workflow_id: - type: string - default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Destroy environments @@ -16,17 +12,13 @@ commands: aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. - parameters: - workflow_id: - type: string - default: ${CIRCLE_WORKFLOW_ID:0:7} steps: - run: name: Revert migrations when: on_fail command: | echo "insecure" >> ~/.curlrc - SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>) + SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7}) if(( $SUCCESS==1 )); then cd ~/project/backend @@ -364,34 +356,34 @@ jobs: - destroy-environment - revert-migrations -cleanup: - docker: - - image: amazon/aws-cli - steps: - - checkout - - run: - name: Get old stack workflow id - command: | - export OldWorkflowID=$(aws cloudformation \ - list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ - --no-paginate --output text) - echo OldWorkflowID: "${OldWorkflowID}" - echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" - - export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ - --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) - echo Stack names: "${STACKS[@]}" - - run: - name: Remove old stacks and files - command: | - if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] - then - aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive - aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" - aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" - else - echo "==========================Error while delete old stacks==========================" - fi + cleanup: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Get old stack workflow id + command: | + export OldWorkflowID=$(aws cloudformation \ + list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \ + --no-paginate --output text) + echo OldWorkflowID: "${OldWorkflowID}" + echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" + + export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ + --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) + echo Stack names: "${STACKS[@]}" + - run: + name: Remove old stacks and files + command: | + if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] + then + aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" + else + echo "==========================Error while delete old stacks==========================" + fi workflows: default: From 907f50dae5b413b2e3a2dbb6b4add38b445771ba Mon Sep 17 00:00:00 2001 From: longbui98 Date: Mon, 6 Nov 2023 02:04:18 +0700 Subject: [PATCH 87/95] Update config.yml --- .circleci/config.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8e460bc..5b2f57d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,6 +8,9 @@ commands: name: Destroy environments when: on_fail command: | + if aws s3 ls | grep "udapeople-${CIRCLE_WORKFLOW_ID:0:7}"; then + aws s3 rm s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7} --recursive + fi aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: From 29497adc07f3ed2e99c1064bdc8e53da96cca76a Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 00:23:16 +0700 Subject: [PATCH 88/95] Update image in cloudfront-update job --- .circleci/config.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5b2f57d..a340661 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -337,15 +337,14 @@ jobs: cloudfront-update: docker: - - image: amazon/aws-cli + - image: python:3.9.1-alpine3.12 steps: - checkout - run: name: Install dependencies command: | - yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm - curl -sL https://rpm.nodesource.com/setup_14.x | -E bash - - yum install -y nodejs + apk update + apk add npm nodejs node -v npm -v - run: From 1547472024d496072c40983d9208aa060833bc4a Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 01:00:54 +0700 Subject: [PATCH 89/95] Install aws cli in cloudfront-update job --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index a340661..ca03c43 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -347,6 +347,7 @@ jobs: apk add npm nodejs node -v npm -v + apk add --update ansible aws-cli - run: name: Update cloudfront distribution command: | From 53dcd3c148a39584b5f7de90d2be72cb62ef8fc3 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 01:25:24 +0700 Subject: [PATCH 90/95] Update overdide ID for cloudformation deplot in cleanup job --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ca03c43..c2e4bf3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -354,7 +354,7 @@ jobs: aws cloudformation deploy \ --template-file .circleci/files/cloudfront.yml \ --stack-name InitialStack \ - --parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides NewWorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - destroy-environment - revert-migrations From a9a09ed4a022f2f7ba7ff0a82503c8add822bbe0 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 01:49:27 +0700 Subject: [PATCH 91/95] Update workspacke in cloudfront-update job --- .circleci/config.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c2e4bf3..e0de0b0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -340,6 +340,8 @@ jobs: - image: python:3.9.1-alpine3.12 steps: - checkout + - attach_workspace: + at: ~/ - run: name: Install dependencies command: | @@ -352,7 +354,7 @@ jobs: name: Update cloudfront distribution command: | aws cloudformation deploy \ - --template-file .circleci/files/cloudfront.yml \ + --template-file ~/project/.circleci/files/cloudfront.yml \ --stack-name InitialStack \ --parameter-overrides NewWorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople From d5775c1ad8e3293825eb0c0be2c64d4097a7b9ca Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 02:08:13 +0700 Subject: [PATCH 92/95] Update name of cloudformation in cloudfront update --- .circleci/config.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e0de0b0..b8cf1c5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -340,8 +340,6 @@ jobs: - image: python:3.9.1-alpine3.12 steps: - checkout - - attach_workspace: - at: ~/ - run: name: Install dependencies command: | @@ -355,7 +353,7 @@ jobs: command: | aws cloudformation deploy \ --template-file ~/project/.circleci/files/cloudfront.yml \ - --stack-name InitialStack \ + --stack-name InitialStackUpdate \ --parameter-overrides NewWorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \ --tags project=udapeople - destroy-environment From adb6ca1746e010be970fd1678f928d328c857f98 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 13:09:06 +0700 Subject: [PATCH 93/95] Update cleanup job in config.yml --- .circleci/config.yml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b8cf1c5..26bf41f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -240,6 +240,7 @@ jobs: apk update apk add tar gzip apk add npm nodejs + apk add --no-cache curl apk add --update ansible aws-cli - run: name: Get backend url @@ -276,6 +277,7 @@ jobs: apk update apk add tar gzip apk add npm nodejs + apk add --no-cache curl apk add --update ansible aws-cli - run: name: Deploy backend @@ -347,6 +349,7 @@ jobs: apk add npm nodejs node -v npm -v + apk add --no-cache curl apk add --update ansible aws-cli - run: name: Update cloudfront distribution @@ -372,21 +375,12 @@ jobs: --no-paginate --output text) echo OldWorkflowID: "${OldWorkflowID}" echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}" - - export STACKS=($(aws cloudformation list-stacks --query "StackSummaries[*].StackName" \ - --stack-status-filter CREATE_COMPLETE --no-paginate --output text)) - echo Stack names: "${STACKS[@]}" - run: name: Remove old stacks and files command: | - if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]] - then - aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive - aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" - aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" - else - echo "==========================Error while delete old stacks==========================" - fi + aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" workflows: default: From c123dc5f3e19cb358cf073a33b136a2c8033e66d Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 13:33:53 +0700 Subject: [PATCH 94/95] Update cleanup job and deploy-infrastructure in only master branch --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 26bf41f..520075d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -378,9 +378,9 @@ jobs: - run: name: Remove old stacks and files command: | - aws s3 rm "s3://udapeople-${OldWorkflowID}" --recursive - aws cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}" - aws cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}" + aws s3 rm "s3://${OldWorkflowID}" --recursive + aws cloudformation delete-stack --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" + aws cloudformation delete-stack --stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" workflows: default: @@ -399,7 +399,7 @@ workflows: requires: [test-frontend, test-backend, scan-frontend, scan-backend] filters: branches: - only: [test-feature-branch] + only: [master] - configure-infrastructure: requires: [deploy-infrastructure] - run-migrations: From 6e2b4f0a8c495de9a03a5dd33412f8199e81827a Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 7 Nov 2023 15:05:49 +0700 Subject: [PATCH 95/95] Add section to install configure-prometheus-node-exporter in configure-server --- .circleci/ansible/configure-server.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/ansible/configure-server.yml b/.circleci/ansible/configure-server.yml index 267bcb8..3f4117d 100644 --- a/.circleci/ansible/configure-server.yml +++ b/.circleci/ansible/configure-server.yml @@ -22,4 +22,5 @@ state: latest roles: - - configure-server \ No newline at end of file + - configure-server + - configure-prometheus-node-exporter \ No newline at end of file