From 8250fa90e2fdcfa14b0425083c423f771a0d5323 Mon Sep 17 00:00:00 2001 From: anthony Date: Sat, 30 Oct 2021 22:31:38 +0200 Subject: [PATCH 1/8] :construction_worker: Add netlify deployment steps --- .circleci/config.yml | 163 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..c454242 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,163 @@ +version: 2.1 +# --- Constants --- # + +globals: + images: + node: &node cimg/node:lts + caches: + dependencies: &dependencies dependencies-{{ checksum "package.json" }} + docker_hub: + user-id: &dockerhub-user-id anthonyjhoiro + password: &dockerhub-password $DOCKERHUB_PASSWORD + image: &dockerhub-image devops-tpfinal-front + + +# --- Commands --- # + +commands: + setup_dependencies: + description: Setup the environment + steps: + - checkout + - restore_cache: + keys: + - *dependencies + +# --- Jobs definitions --- # + +jobs: + build: &shared-config + docker: + - image: *node + auth: + username: *dockerhub-user-id + password: *dockerhub-password + working_directory: ~/app + steps: + - checkout + - restore_cache: + keys: + - *dependencies + # fallback to using the latest cache if no exact match is found + - dependencies- + - run: yarn install + - save_cache: + paths: + - node_modules + key: *dependencies + test: + <<: *shared-config + steps: + - setup_dependencies + - run: yarn test:unit + + lint: + <<: *shared-config + steps: + - setup_dependencies + - run: + name: Check files format + command: yarn format:check + + - run: + name: Lint files + command: yarn lint + + docker-build: + <<: *shared-config + environment: + DOCKER_HUB_PWD: *dockerhub-password + DOCKER_HUB_USER_ID: *dockerhub-user-id + DOCKER_HUB_IMAGE: *dockerhub-image + steps: + - checkout + - setup_remote_docker: + version: 20.10.7 + - run: + name: Build docker image + command: | + echo true + #docker build -t app . + - deploy: + name: Publish image to Docker Hub + command: | + echo true + #echo $DOCKERHUB_PASSWORD | docker login -u $DOCKER_HUB_USER_ID --password-stdin + #docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM + #docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest + #docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM + #docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest + + build-netlify: + <<: *shared-config + environment: + VITE_BACKEND_URL_URL: https://tpfinal-devops-back.herokuapp.com/ + steps: + - setup_dependencies + - run: yarn build + - run: + name: Copy deployment artifacts to workspace + command: | + cp -r dist/ /tmp/build + + - store_artifacts: + path: /tmp/build + - persist_to_workspace: + root: /tmp + paths: + - build + + deploy-netlify: + <<: *shared-config + working_directory: ~/todoapp + steps: + - attach_workspace: + at: /tmp + - run: + name: Install netlify + command: yarn add -D netlify-cli + + - run: + name: Deploy app + command: yarn netlify deploy --prod --dir=/tmp/build + + +# --- Workflow definition --- # + +workflows: + version: 2 + build-test-and-lint: + jobs: + - build + - test: + requires: + - build + - lint: + requires: + - build + + - build-netlify: + requires: + - test + - lint + filters: + branches: + only: + - main + + - deploy-netlify: + requires: + - build-netlify + filters: + branches: + only: + - main + + - docker-build: + requires: + - test + - lint + filters: + branches: + only: + - main From bf6bc206e178f61eea2a57666823b4e66f121dde Mon Sep 17 00:00:00 2001 From: anthony Date: Sat, 30 Oct 2021 22:43:28 +0200 Subject: [PATCH 2/8] :green_heart: Remove branch restrictions to test Netlify deployment --- .circleci/config.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c454242..7a53a07 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -140,18 +140,18 @@ workflows: requires: - test - lint - filters: - branches: - only: - - main +# filters: +# branches: +# only: +# - main - deploy-netlify: requires: - build-netlify - filters: - branches: - only: - - main +# filters: +# branches: +# only: +# - main - docker-build: requires: From ada68bd44527d9f985bc18ed3aea1ca5dac0b8c0 Mon Sep 17 00:00:00 2001 From: anthony Date: Sat, 30 Oct 2021 22:59:54 +0200 Subject: [PATCH 3/8] :green_heart: Fix lint job commands --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7a53a07..6705d33 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -57,11 +57,11 @@ jobs: - setup_dependencies - run: name: Check files format - command: yarn format:check + command: yarn format:write - run: name: Lint files - command: yarn lint + command: yarn format:check docker-build: <<: *shared-config From 2c83b8c378039a3e7906d9a45dc27773df21c6dd Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 27 Dec 2021 19:13:33 +0100 Subject: [PATCH 4/8] :construction_worker: Add docker build --- .circleci/config.yml | 26 +++++++++++++++++--------- Dockerfile | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 Dockerfile diff --git a/.circleci/config.yml b/.circleci/config.yml index 6705d33..cee6c93 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,6 +10,8 @@ globals: user-id: &dockerhub-user-id anthonyjhoiro password: &dockerhub-password $DOCKERHUB_PASSWORD image: &dockerhub-image devops-tpfinal-front + backends: + prod: &prod-backend-url https://tpfinal-devops-back.herokuapp.com # --- Commands --- # @@ -65,6 +67,9 @@ jobs: docker-build: <<: *shared-config + parameters: + backendurl: + type: string environment: DOCKER_HUB_PWD: *dockerhub-password DOCKER_HUB_USER_ID: *dockerhub-user-id @@ -76,22 +81,23 @@ jobs: - run: name: Build docker image command: | - echo true - #docker build -t app . + docker build -t app --build-arg backend_url=<> . - deploy: name: Publish image to Docker Hub command: | - echo true - #echo $DOCKERHUB_PASSWORD | docker login -u $DOCKER_HUB_USER_ID --password-stdin - #docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM - #docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest - #docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM - #docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest + echo $DOCKERHUB_PASSWORD | docker login -u $DOCKER_HUB_USER_ID --password-stdin + docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM + docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest + docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM + docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest build-netlify: <<: *shared-config + parameters: + backendurl: + type: string environment: - VITE_BACKEND_URL_URL: https://tpfinal-devops-back.herokuapp.com/ + VITE_BACKEND_URL_URL: <> steps: - setup_dependencies - run: yarn build @@ -137,6 +143,7 @@ workflows: - build - build-netlify: + backendurl: *prod-backend-url requires: - test - lint @@ -154,6 +161,7 @@ workflows: # - main - docker-build: + backendurl: *prod-backend-url requires: - test - lint diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0ff709c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM node:lts as builder +WORKDIR /build + +ARG backend_url + +ENV VITE_BACKEND_URL_URL=$backend_url + +COPY . . +RUN yarn install +RUN yarn build + +FROM nginx:lts + +WORKDIR /usr/share/nginx/html + + +COPY --from=builder /build/dist . + +CMD ["yarn", "start:prod"] + From 20a4eb7aa90e8c79c0280a8b7ffe0d2f25048cbd Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 27 Dec 2021 19:15:05 +0100 Subject: [PATCH 5/8] :green_heart: Remove filters for docker build --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index cee6c93..9b3c0d9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -165,7 +165,7 @@ workflows: requires: - test - lint - filters: - branches: - only: - - main +# filters: +# branches: +# only: +# - main From 2734dc6b90d27bce8059140b12e4df224331d962 Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 27 Dec 2021 19:19:22 +0100 Subject: [PATCH 6/8] :green_heart: Remove CMD command from docker image --- Dockerfile | 3 --- 1 file changed, 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0ff709c..7e3536a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,3 @@ WORKDIR /usr/share/nginx/html COPY --from=builder /build/dist . - -CMD ["yarn", "start:prod"] - From 84f25f1746e138c2939891253b85d6887cc27dbe Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 27 Dec 2021 19:26:36 +0100 Subject: [PATCH 7/8] :green_heart: Fix nginx image tag --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7e3536a..5cfde9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ COPY . . RUN yarn install RUN yarn build -FROM nginx:lts +FROM nginx:stable WORKDIR /usr/share/nginx/html From 79947087a7983d97811c97a590c389caf2dbd7ef Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 3 Jan 2022 10:28:16 +0100 Subject: [PATCH 8/8] :construction_worker: Add branch restriction to circle ci deployments --- .circleci/config.yml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9b3c0d9..2f030ff 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -147,25 +147,25 @@ workflows: requires: - test - lint -# filters: -# branches: -# only: -# - main + filters: + branches: + only: + - main - deploy-netlify: requires: - build-netlify -# filters: -# branches: -# only: -# - main + filters: + branches: + only: + - main - docker-build: backendurl: *prod-backend-url requires: - test - lint -# filters: -# branches: -# only: -# - main + filters: + branches: + only: + - main