From 7facb79114d25e034cbd92d42eeb70b6c2d1fe91 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:25:38 +0900 Subject: [PATCH 01/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 96 +++++++++++++++++++--------- 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index ec585408..fd859aa6 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -6,6 +6,10 @@ on: workflow_dispatch: repository_dispatch: +env: + KCR_REGISTRY: dkation.kr-central-2.kcr.dev + KCR_REPOSITORY: dkation-prod-front/dkation-prod-fe + jobs: deploy: runs-on: ubuntu-latest @@ -31,34 +35,68 @@ jobs: with: ssh-private-key: ${{ secrets.SSH_KEY }} + - name: Add known hosts + run: | + mkdir -p ~/.ssh + ssh-keyscan -H ${{ secrets.BASTION_HOST }} >> ~/.ssh/known_hosts + IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" + for host in "${HOSTS[@]}"; do + ssh-keyscan -H $host >> ~/.ssh/known_hosts + done + - name: Deploy to FE - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.BASTION_HOST }} - username: ${{ secrets.USER }} - key: ${{ secrets.SSH_KEY }} - script: | - mkdir -p ~/.ssh - echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - - IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" + env: + BASTION_HOST: ${{ secrets.BASTION_HOST }} + USER: ${{ secrets.USER }} + WEB_IPS: ${{ secrets.WEB_IPS }} + ACCESS_KEY: ${{ secrets.ACCESS_KEY }} + ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} + run: | + IFS=',' read -r -a HOSTS <<< "$WEB_IPS" + + for host in "${HOSTS[@]}" + do + echo "Deploying to $host" + ssh -J $USER@$BASTION_HOST $USER@$host << EOF + set -e + echo "Stopping and removing existing containers" + docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true + docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true + + echo "Removing old Docker images" + docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true + + echo "Login to KCR" + echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin + + echo "Pulling new Docker image" + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} + + echo "Running new Docker container" + docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} + + echo "Checking container health" + max_retries=5 + retries=0 + until docker ps | grep frontend | grep -q "Up" || [ $retries -eq $max_retries ] + do + echo "Waiting for container to be healthy..." + sleep 5 + retries=$((retries+1)) + done + + if [ $retries -eq $max_retries ]; then + echo "Container failed to start properly" + exit 1 + fi + + echo "Container is up and running" + EOF - for host in "${HOSTS[@]}" - do - echo "$host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@$host << EOF - echo "Stopping and removing existing containers" - docker ps -q --filter ancestor=dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe | xargs -r docker stop - docker ps -aq --filter ancestor=dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe | xargs -r docker rm - echo "Removing old Docker images" - docker images dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe --format '{{.ID}}' | xargs -r docker rmi - - echo "Login to KCR" - docker login dkation.kr-central-2.kcr.dev --username ${{ secrets.ACCESS_KEY }} --password ${{ secrets.ACCESS_SECRET_KEY }} - echo "Pulling new Docker image" - docker pull dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${{ steps.get_tag.outputs.tag }} - echo "Running new Docker container" - docker run -d -p 80:3000 dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${{ steps.get_tag.outputs.tag }} - EOF - done \ No newline at end of file + if [ $? -ne 0 ]; then + echo "Deployment to $host failed" + exit 1 + fi + done + + echo "Deployment completed successfully" \ No newline at end of file From 2740144b89de96ebdd6f84aee14cc43695c23bb8 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:26:31 +0900 Subject: [PATCH 02/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index fd859aa6..9b5bfb65 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -3,6 +3,9 @@ name: NextJS CD on: release: types: [created] + push: + branches: + - MF-361-Next.js-CD-Github-Action workflow_dispatch: repository_dispatch: From d73d115ac072ebea30f4acd51134c565119c066e Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:28:54 +0900 Subject: [PATCH 03/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 9b5bfb65..0705ac34 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -22,15 +22,17 @@ jobs: with: fetch-depth: 0 - - name: Get release tag - id: get_tag + - name: Get release tag or commit hash + id: get_version run: | if [[ ${{ github.event_name }} == 'release' ]]; then - echo "tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT + echo "version=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT + elif [[ ${{ github.event_name }} == 'push' ]]; then + echo "version=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT else git fetch --tags latest_tag=$(git describe --tags --abbrev=0) - echo "tag=$latest_tag" >> $GITHUB_OUTPUT + echo "version=$latest_tag" >> $GITHUB_OUTPUT fi - name: Setup SSH @@ -73,10 +75,10 @@ jobs: echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} echo "Running new Docker container" - docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} + docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} echo "Checking container health" max_retries=5 From 9b97891398f4581292aeb84080298fc5c3b6784f Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:30:50 +0900 Subject: [PATCH 04/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 0705ac34..b26a7905 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -43,9 +43,12 @@ jobs: - name: Add known hosts run: | mkdir -p ~/.ssh + echo "BASTION_HOST: ${{ secrets.BASTION_HOST }}" + echo "WEB_IPS: ${{ secrets.WEB_IPS }}" ssh-keyscan -H ${{ secrets.BASTION_HOST }} >> ~/.ssh/known_hosts IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" for host in "${HOSTS[@]}"; do + echo "Adding host $host to known_hosts" ssh-keyscan -H $host >> ~/.ssh/known_hosts done From 77eeb2c759ae4794002b051efaa724e9940eed7c Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:32:57 +0900 Subject: [PATCH 05/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index b26a7905..a9223656 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -27,8 +27,6 @@ jobs: run: | if [[ ${{ github.event_name }} == 'release' ]]; then echo "version=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT - elif [[ ${{ github.event_name }} == 'push' ]]; then - echo "version=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT else git fetch --tags latest_tag=$(git describe --tags --abbrev=0) @@ -43,9 +41,9 @@ jobs: - name: Add known hosts run: | mkdir -p ~/.ssh - echo "BASTION_HOST: ${{ secrets.BASTION_HOST }}" + echo "BASTION_IP: ${{ secrets.BASTION_IP }}" echo "WEB_IPS: ${{ secrets.WEB_IPS }}" - ssh-keyscan -H ${{ secrets.BASTION_HOST }} >> ~/.ssh/known_hosts + ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" for host in "${HOSTS[@]}"; do echo "Adding host $host to known_hosts" @@ -54,7 +52,7 @@ jobs: - name: Deploy to FE env: - BASTION_HOST: ${{ secrets.BASTION_HOST }} + BASTION_HOST: ${{ secrets.BASTION_IP }} USER: ${{ secrets.USER }} WEB_IPS: ${{ secrets.WEB_IPS }} ACCESS_KEY: ${{ secrets.ACCESS_KEY }} From 06d0e14c7e5d4e9940e25ff22fb4b70e70a03d2b Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:35:05 +0900 Subject: [PATCH 06/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index a9223656..7cdde61d 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -44,10 +44,18 @@ jobs: echo "BASTION_IP: ${{ secrets.BASTION_IP }}" echo "WEB_IPS: ${{ secrets.WEB_IPS }}" ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts + if [ $? -ne 0 ]; then + echo "Failed to add BASTION_IP to known_hosts" + exit 1 + fi IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" for host in "${HOSTS[@]}"; do echo "Adding host $host to known_hosts" ssh-keyscan -H $host >> ~/.ssh/known_hosts + if [ $? -ne 0 ]; then + echo "Failed to add $host to known_hosts" + exit 1 + fi done - name: Deploy to FE From 28a087c22aa6f8ef1cc68b7d9cdcc69116896b82 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 10:58:37 +0900 Subject: [PATCH 07/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 7cdde61d..ccc9dbb4 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -38,29 +38,19 @@ jobs: with: ssh-private-key: ${{ secrets.SSH_KEY }} - - name: Add known hosts + - name: Add known hosts for Bastion Host run: | mkdir -p ~/.ssh echo "BASTION_IP: ${{ secrets.BASTION_IP }}" - echo "WEB_IPS: ${{ secrets.WEB_IPS }}" ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts if [ $? -ne 0 ]; then echo "Failed to add BASTION_IP to known_hosts" exit 1 fi - IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" - for host in "${HOSTS[@]}"; do - echo "Adding host $host to known_hosts" - ssh-keyscan -H $host >> ~/.ssh/known_hosts - if [ $? -ne 0 ]; then - echo "Failed to add $host to known_hosts" - exit 1 - fi - done - name: Deploy to FE env: - BASTION_HOST: ${{ secrets.BASTION_IP }} + BASTION_HOST: ${{ secrets.BASTION_HOST }} USER: ${{ secrets.USER }} WEB_IPS: ${{ secrets.WEB_IPS }} ACCESS_KEY: ${{ secrets.ACCESS_KEY }} @@ -84,10 +74,10 @@ jobs: echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} echo "Running new Docker container" - docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} echo "Checking container health" max_retries=5 From 3840a208011ae6fdc439e26dec59bc14f7ed8a09 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:02:09 +0900 Subject: [PATCH 08/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index ccc9dbb4..235afb75 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -57,7 +57,7 @@ jobs: ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} run: | IFS=',' read -r -a HOSTS <<< "$WEB_IPS" - + for host in "${HOSTS[@]}" do echo "Deploying to $host" @@ -66,19 +66,19 @@ jobs: echo "Stopping and removing existing containers" docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true - + echo "Removing old Docker images" docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true - + echo "Login to KCR" echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin - + echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} - + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + echo "Running new Docker container" - docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_tag.outputs.tag }} - + docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + echo "Checking container health" max_retries=5 retries=0 @@ -88,19 +88,19 @@ jobs: sleep 5 retries=$((retries+1)) done - + if [ $retries -eq $max_retries ]; then echo "Container failed to start properly" exit 1 fi - + echo "Container is up and running" - EOF - + EOF + if [ $? -ne 0 ]; then echo "Deployment to $host failed" exit 1 fi done - + echo "Deployment completed successfully" \ No newline at end of file From 1c08a30a66bf3e2b1559d930efcc516b81170d4b Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:03:32 +0900 Subject: [PATCH 09/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 235afb75..9967ff40 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -38,20 +38,28 @@ jobs: with: ssh-private-key: ${{ secrets.SSH_KEY }} - - name: Add known hosts for Bastion Host + - name: Add known hosts run: | mkdir -p ~/.ssh - echo "BASTION_IP: ${{ secrets.BASTION_IP }}" - ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts - if [ $? -ne 0 ]; then - echo "Failed to add BASTION_IP to known_hosts" - exit 1 - fi + echo "${{ secrets.BASTION_IP }} ${{ secrets.BASTION_HOST }}" >> ~/.ssh/known_hosts + echo "${{ secrets.WEB_IPS }}" | tr ',' '\n' | while read ip; do + echo "$ip" >> ~/.ssh/known_hosts + done + + - name: Setup SSH config + run: | + echo "Host bastion" >> ~/.ssh/config + echo " HostName ${{ secrets.BASTION_HOST }}" >> ~/.ssh/config + echo " User ${{ secrets.USER }}" >> ~/.ssh/config + echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config + echo "" >> ~/.ssh/config + echo "Host webserver" >> ~/.ssh/config + echo " ProxyCommand ssh -W %h:%p bastion" >> ~/.ssh/config + echo " User ${{ secrets.USER }}" >> ~/.ssh/config + echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config - name: Deploy to FE env: - BASTION_HOST: ${{ secrets.BASTION_HOST }} - USER: ${{ secrets.USER }} WEB_IPS: ${{ secrets.WEB_IPS }} ACCESS_KEY: ${{ secrets.ACCESS_KEY }} ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} @@ -61,7 +69,7 @@ jobs: for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -J $USER@$BASTION_HOST $USER@$host << EOF + ssh -o ProxyCommand="ssh -W %h:%p bastion" ${{ secrets.USER }}@$host << EOF set -e echo "Stopping and removing existing containers" docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true From a59a350bbcc50df0c32aed8ffef300e2c7a44493 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:05:18 +0900 Subject: [PATCH 10/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 9967ff40..25622b22 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -41,22 +41,22 @@ jobs: - name: Add known hosts run: | mkdir -p ~/.ssh - echo "${{ secrets.BASTION_IP }} ${{ secrets.BASTION_HOST }}" >> ~/.ssh/known_hosts + ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts echo "${{ secrets.WEB_IPS }}" | tr ',' '\n' | while read ip; do - echo "$ip" >> ~/.ssh/known_hosts + ssh-keyscan -H $ip >> ~/.ssh/known_hosts done - name: Setup SSH config run: | - echo "Host bastion" >> ~/.ssh/config - echo " HostName ${{ secrets.BASTION_HOST }}" >> ~/.ssh/config - echo " User ${{ secrets.USER }}" >> ~/.ssh/config - echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config + echo "Host bastion" > ~/.ssh/config + echo " HostName ${{ secrets.BASTION_IP }}" >> ~/.ssh/config + echo " User ${{ secrets.USER }}" >> ~/.ssh/config + echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config echo "" >> ~/.ssh/config echo "Host webserver" >> ~/.ssh/config - echo " ProxyCommand ssh -W %h:%p bastion" >> ~/.ssh/config - echo " User ${{ secrets.USER }}" >> ~/.ssh/config - echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config + echo " ProxyCommand ssh -W %h:%p bastion" >> ~/.ssh/config + echo " User ${{ secrets.USER }}" >> ~/.ssh/config + echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config - name: Deploy to FE env: From 20af20caf59d420811ad7e193ffc47fd1165ab8d Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:09:31 +0900 Subject: [PATCH 11/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 128 +++++++++++---------------- 1 file changed, 54 insertions(+), 74 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 25622b22..385b90e1 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -33,82 +33,62 @@ jobs: echo "version=$latest_tag" >> $GITHUB_OUTPUT fi - - name: Setup SSH - uses: webfactory/ssh-agent@v0.5.4 - with: - ssh-private-key: ${{ secrets.SSH_KEY }} - - - name: Add known hosts - run: | - mkdir -p ~/.ssh - ssh-keyscan -H ${{ secrets.BASTION_IP }} >> ~/.ssh/known_hosts - echo "${{ secrets.WEB_IPS }}" | tr ',' '\n' | while read ip; do - ssh-keyscan -H $ip >> ~/.ssh/known_hosts - done - - - name: Setup SSH config - run: | - echo "Host bastion" > ~/.ssh/config - echo " HostName ${{ secrets.BASTION_IP }}" >> ~/.ssh/config - echo " User ${{ secrets.USER }}" >> ~/.ssh/config - echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config - echo "" >> ~/.ssh/config - echo "Host webserver" >> ~/.ssh/config - echo " ProxyCommand ssh -W %h:%p bastion" >> ~/.ssh/config - echo " User ${{ secrets.USER }}" >> ~/.ssh/config - echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config - - name: Deploy to FE - env: - WEB_IPS: ${{ secrets.WEB_IPS }} - ACCESS_KEY: ${{ secrets.ACCESS_KEY }} - ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} - run: | - IFS=',' read -r -a HOSTS <<< "$WEB_IPS" - - for host in "${HOSTS[@]}" - do - echo "Deploying to $host" - ssh -o ProxyCommand="ssh -W %h:%p bastion" ${{ secrets.USER }}@$host << EOF - set -e - echo "Stopping and removing existing containers" - docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true - docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true - - echo "Removing old Docker images" - docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true - - echo "Login to KCR" - echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin - - echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} - - echo "Running new Docker container" - docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} - - echo "Checking container health" - max_retries=5 - retries=0 - until docker ps | grep frontend | grep -q "Up" || [ $retries -eq $max_retries ] - do - echo "Waiting for container to be healthy..." - sleep 5 - retries=$((retries+1)) - done - - if [ $retries -eq $max_retries ]; then - echo "Container failed to start properly" + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.BASTION_HOST }} + username: ${{ secrets.USER }} + key: ${{ secrets.SSH_KEY }} + script: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + + IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" + + for host in "${HOSTS[@]}" + do + echo "Deploying to $host" + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF + set -e + echo "Stopping and removing existing containers" + docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true + docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true + + echo "Removing old Docker images" + docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true + + echo "Login to KCR" + echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin + + echo "Pulling new Docker image" + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + + echo "Running new Docker container" + docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + + echo "Checking container health" + max_retries=5 + retries=0 + until docker ps | grep frontend | grep -q "Up" || [ $retries -eq $max_retries ] + do + echo "Waiting for container to be healthy..." + sleep 5 + retries=$((retries+1)) + done + + if [ $retries -eq $max_retries ]; then + echo "Container failed to start properly" + exit 1 + fi + + echo "Container is up and running" + EOF + + if [ $? -ne 0 ]; then + echo "Deployment to $host failed" exit 1 fi - - echo "Container is up and running" - EOF - - if [ $? -ne 0 ]; then - echo "Deployment to $host failed" - exit 1 - fi - done + done echo "Deployment completed successfully" \ No newline at end of file From dc589474c768e098046da35f79e1423551639986 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:11:47 +0900 Subject: [PATCH 12/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 385b90e1..2fbd67ad 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -89,6 +89,8 @@ jobs: echo "Deployment to $host failed" exit 1 fi + + echo "Deployment completed successfully" + done - echo "Deployment completed successfully" \ No newline at end of file From 087632b26504b130a06ceca4b823e385750c804e Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:14:27 +0900 Subject: [PATCH 13/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 2fbd67ad..a235c325 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -36,7 +36,7 @@ jobs: - name: Deploy to FE uses: appleboy/ssh-action@master with: - host: ${{ secrets.BASTION_HOST }} + host: ${{ secrets.BASTION_IP }} username: ${{ secrets.USER }} key: ${{ secrets.SSH_KEY }} script: | From 97001d27b316a445cd7488442274b981c8ce5c61 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:18:12 +0900 Subject: [PATCH 14/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index a235c325..ccd1bdeb 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -1,5 +1,4 @@ name: NextJS CD - on: release: types: [created] @@ -8,11 +7,9 @@ on: - MF-361-Next.js-CD-Github-Action workflow_dispatch: repository_dispatch: - env: KCR_REGISTRY: dkation.kr-central-2.kcr.dev KCR_REPOSITORY: dkation-prod-front/dkation-prod-fe - jobs: deploy: runs-on: ubuntu-latest @@ -21,7 +18,6 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Get release tag or commit hash id: get_version run: | @@ -32,7 +28,6 @@ jobs: latest_tag=$(git describe --tags --abbrev=0) echo "version=$latest_tag" >> $GITHUB_OUTPUT fi - - name: Deploy to FE uses: appleboy/ssh-action@master with: @@ -49,7 +44,7 @@ jobs: for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << 'EOF' set -e echo "Stopping and removing existing containers" docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true @@ -83,14 +78,12 @@ jobs: fi echo "Container is up and running" - EOF - + if [ $? -ne 0 ]; then echo "Deployment to $host failed" exit 1 fi - echo "Deployment completed successfully" - + EOF done - + echo "Deployment completed successfully" From 249151c00195e5a977836db03ef0ef8c82057324 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:25:12 +0900 Subject: [PATCH 15/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index ccd1bdeb..0aa65ce0 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -7,9 +7,11 @@ on: - MF-361-Next.js-CD-Github-Action workflow_dispatch: repository_dispatch: + env: KCR_REGISTRY: dkation.kr-central-2.kcr.dev KCR_REPOSITORY: dkation-prod-front/dkation-prod-fe + jobs: deploy: runs-on: ubuntu-latest @@ -54,7 +56,7 @@ jobs: docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true echo "Login to KCR" - echo "$ACCESS_SECRET_KEY" | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin + docker login $KCR_REGISTRY --username ${{ secrets.ACCESS_KEY }} --password ${{ secrets.ACCESS_SECRET_KEY }} echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} @@ -78,12 +80,12 @@ jobs: fi echo "Container is up and running" - +EOF if [ $? -ne 0 ]; then echo "Deployment to $host failed" exit 1 fi - EOF - done - echo "Deployment completed successfully" + echo "Deployment completed successfully" + + done \ No newline at end of file From d819a4c2d6cf3cb876fc70190e4674b2762f7fce Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:27:41 +0900 Subject: [PATCH 16/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 0aa65ce0..1ad47a7d 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -80,7 +80,8 @@ jobs: fi echo "Container is up and running" -EOF + EOF + if [ $? -ne 0 ]; then echo "Deployment to $host failed" exit 1 From 700b82078398e64fd795236be3621b9ab6065aed Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 11:28:24 +0900 Subject: [PATCH 17/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 1ad47a7d..fd056f58 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -80,7 +80,7 @@ jobs: fi echo "Container is up and running" - EOF + EOF if [ $? -ne 0 ]; then echo "Deployment to $host failed" From bd40c1b08a59e3d59ea8e9c921046422ded1e52d Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:04:34 +0900 Subject: [PATCH 18/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index fd056f58..e59b72e6 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -20,6 +20,7 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 + - name: Get release tag or commit hash id: get_version run: | @@ -30,6 +31,7 @@ jobs: latest_tag=$(git describe --tags --abbrev=0) echo "version=$latest_tag" >> $GITHUB_OUTPUT fi + - name: Deploy to FE uses: appleboy/ssh-action@master with: @@ -56,8 +58,8 @@ jobs: docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true echo "Login to KCR" - docker login $KCR_REGISTRY --username ${{ secrets.ACCESS_KEY }} --password ${{ secrets.ACCESS_SECRET_KEY }} - + echo ${{ secrets.ACCESS_SECRET_KEY }} | docker login $KCR_REGISTRY --username ${{ secrets.ACCESS_KEY }} --password-stdin + echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} From 499014a9057fd2dd068340d13393cf402c318bbe Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:06:49 +0900 Subject: [PATCH 19/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 36 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index e59b72e6..4b960947 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -42,30 +42,36 @@ jobs: mkdir -p ~/.ssh echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa - + IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" - + + KCR_REGISTRY='${{ env.KCR_REGISTRY }}' + KCR_REPOSITORY='${{ env.KCR_REPOSITORY }}' + ACCESS_KEY='${{ secrets.ACCESS_KEY }}' + ACCESS_SECRET_KEY='${{ secrets.ACCESS_SECRET_KEY }}' + for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << 'EOF' + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF set -e + echo "Stopping and removing existing containers" docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true - + echo "Removing old Docker images" docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true - + echo "Login to KCR" - echo ${{ secrets.ACCESS_SECRET_KEY }} | docker login $KCR_REGISTRY --username ${{ secrets.ACCESS_KEY }} --password-stdin - + echo $ACCESS_SECRET_KEY | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin + echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} - + echo "Running new Docker container" docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} - + echo "Checking container health" max_retries=5 retries=0 @@ -75,20 +81,20 @@ jobs: sleep 5 retries=$((retries+1)) done - + if [ $retries -eq $max_retries ]; then echo "Container failed to start properly" exit 1 fi - + echo "Container is up and running" - EOF - + EOF + if [ $? -ne 0 ]; then echo "Deployment to $host failed" exit 1 fi - + echo "Deployment completed successfully" - + done \ No newline at end of file From 3dba12ee523b033d81e02eded25ed9d96e9bd39e Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:08:19 +0900 Subject: [PATCH 20/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 4b960947..3f4c4855 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -53,7 +53,7 @@ jobs: for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << 'EOF' set -e echo "Stopping and removing existing containers" From 5c520db4b770bc40195dd83d0beed463e84ab3b4 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:09:50 +0900 Subject: [PATCH 21/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 3f4c4855..3203751c 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -88,7 +88,6 @@ jobs: fi echo "Container is up and running" - EOF if [ $? -ne 0 ]; then echo "Deployment to $host failed" @@ -96,5 +95,6 @@ jobs: fi echo "Deployment completed successfully" - + + EOF done \ No newline at end of file From 5884b33be253ba1490d96341208fdc9714bf418c Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:29:48 +0900 Subject: [PATCH 22/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 3203751c..341baef9 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -11,6 +11,8 @@ on: env: KCR_REGISTRY: dkation.kr-central-2.kcr.dev KCR_REPOSITORY: dkation-prod-front/dkation-prod-fe + ACCESS_KEY: ${{ secrets.ACCESS_KEY }} + ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} jobs: deploy: @@ -45,11 +47,6 @@ jobs: IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" - KCR_REGISTRY='${{ env.KCR_REGISTRY }}' - KCR_REPOSITORY='${{ env.KCR_REPOSITORY }}' - ACCESS_KEY='${{ secrets.ACCESS_KEY }}' - ACCESS_SECRET_KEY='${{ secrets.ACCESS_SECRET_KEY }}' - for host in "${HOSTS[@]}" do echo "Deploying to $host" @@ -57,20 +54,20 @@ jobs: set -e echo "Stopping and removing existing containers" - docker ps -q --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker stop || true - docker ps -aq --filter ancestor=$KCR_REGISTRY/$KCR_REPOSITORY | xargs -r docker rm || true - + docker ps --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker stop && \ + docker ps -a --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker rm && \ + echo "Removing old Docker images" - docker images $KCR_REGISTRY/$KCR_REPOSITORY --format '{{.ID}}' | xargs -r docker rmi || true + docker images --format '{{.Repository}}:{{.Tag}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | xargs -r docker rmi echo "Login to KCR" - echo $ACCESS_SECRET_KEY | docker login $KCR_REGISTRY --username $ACCESS_KEY --password-stdin + echo ${{ env.ACCESS_SECRET_KEY }} | docker login $KCR_REGISTRY --username ${{ env.ACCESS_KEY}} --password-stdin echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} echo "Running new Docker container" - docker run -d -p 80:3000 --name frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} echo "Checking container health" max_retries=5 From ad4d62b08431e0edb7b96daffce27b2387a89ade Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:31:53 +0900 Subject: [PATCH 23/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 341baef9..9c839bef 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -60,9 +60,6 @@ jobs: echo "Removing old Docker images" docker images --format '{{.Repository}}:{{.Tag}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | xargs -r docker rmi - echo "Login to KCR" - echo ${{ env.ACCESS_SECRET_KEY }} | docker login $KCR_REGISTRY --username ${{ env.ACCESS_KEY}} --password-stdin - echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} From 34b7404a3ee8d5acee9db9b593a0e6ce85cd438f Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:34:34 +0900 Subject: [PATCH 24/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 9c839bef..89f56d3f 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -46,42 +46,46 @@ jobs: chmod 600 ~/.ssh/id_rsa IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" + KCR_REGISTRY="${{ env.KCR_REGISTRY }}" + KCR_REPOSITORY="${{ env.KCR_REPOSITORY }}" + VERSION="${{ steps.get_version.outputs.version }}" for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << 'EOF' + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF set -e echo "Stopping and removing existing containers" - docker ps --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker stop && \ - docker ps -a --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker rm && \ - + docker ps --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | xargs -r docker stop + docker ps -a --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | xargs -r docker rm + echo "Removing old Docker images" - docker images --format '{{.Repository}}:{{.Tag}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | xargs -r docker rmi + docker images --format '{{.Repository}}:{{.Tag}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | xargs -r docker rmi echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION echo "Running new Docker container" - docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION echo "Checking container health" max_retries=5 retries=0 - until docker ps | grep frontend | grep -q "Up" || [ $retries -eq $max_retries ] + until docker ps | grep dkation-frontend | grep -q "Up" || [ \$retries -eq \$max_retries ] do echo "Waiting for container to be healthy..." sleep 5 - retries=$((retries+1)) + retries=\$((retries+1)) done - if [ $retries -eq $max_retries ]; then + if [ \$retries -eq \$max_retries ]; then echo "Container failed to start properly" exit 1 fi echo "Container is up and running" + EOF if [ $? -ne 0 ]; then echo "Deployment to $host failed" @@ -89,6 +93,4 @@ jobs: fi echo "Deployment completed successfully" - - EOF done \ No newline at end of file From 9f5a988772dacdb08b027d483bb350996a84d349 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:38:08 +0900 Subject: [PATCH 25/27] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 89f56d3f..b98c5942 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -49,6 +49,8 @@ jobs: KCR_REGISTRY="${{ env.KCR_REGISTRY }}" KCR_REPOSITORY="${{ env.KCR_REPOSITORY }}" VERSION="${{ steps.get_version.outputs.version }}" + AK="${{ env.ACCESS_KEY }}" + SK="${{ env.ACCESS_SECRET_KEY }}" for host in "${HOSTS[@]}" do @@ -63,6 +65,9 @@ jobs: echo "Removing old Docker images" docker images --format '{{.Repository}}:{{.Tag}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | xargs -r docker rmi + echo "Docker login" + echo "$SK" | docker login $KCR_REGISTRY -u "$AK" --password-stdin + echo "Pulling new Docker image" docker pull $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION From 8e2b24603a0c4a28810fd5a5a16e65ea3d05dbe9 Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:54:02 +0900 Subject: [PATCH 26/27] fix: CI/CD workflow --- .github/workflows/nextjs-prod-CD.yml | 3 --- .github/workflows/nextjs-prod-CI.yml | 4 +++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index b98c5942..c7f4e821 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -2,9 +2,6 @@ name: NextJS CD on: release: types: [created] - push: - branches: - - MF-361-Next.js-CD-Github-Action workflow_dispatch: repository_dispatch: diff --git a/.github/workflows/nextjs-prod-CI.yml b/.github/workflows/nextjs-prod-CI.yml index 20061525..f9924b95 100644 --- a/.github/workflows/nextjs-prod-CI.yml +++ b/.github/workflows/nextjs-prod-CI.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - MF-361-Next.js-CD-Github-Action workflow_dispatch: permissions: @@ -108,9 +109,10 @@ jobs: - name: Create Release uses: actions/create-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.PAT }} with: tag_name: ${{ steps.create_tag.outputs.NEW_TAG }} release_name: Release ${{ steps.create_tag.outputs.NEW_TAG }} draft: false prerelease: false + token: ${{ secrets.PAT }} From b74d5641696cc84294a03cf4fdf36fc8ebf329bd Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 14:05:02 +0900 Subject: [PATCH 27/27] fix: final CI/CD workflow --- .github/workflows/nextjs-prod-CD.yml | 2 -- .github/workflows/nextjs-prod-CI.yml | 1 - 2 files changed, 3 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index c7f4e821..4b679098 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -2,8 +2,6 @@ name: NextJS CD on: release: types: [created] - workflow_dispatch: - repository_dispatch: env: KCR_REGISTRY: dkation.kr-central-2.kcr.dev diff --git a/.github/workflows/nextjs-prod-CI.yml b/.github/workflows/nextjs-prod-CI.yml index f9924b95..6eb525be 100644 --- a/.github/workflows/nextjs-prod-CI.yml +++ b/.github/workflows/nextjs-prod-CI.yml @@ -4,7 +4,6 @@ on: push: branches: - main - - MF-361-Next.js-CD-Github-Action workflow_dispatch: permissions: