From e05f324945ddb8467e8303cdbfe152b6fa596079 Mon Sep 17 00:00:00 2001 From: kanguk Date: Sat, 2 Nov 2024 03:26:15 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20CD=20=EC=8A=A4=ED=81=AC=EB=A6=BD?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit env로 관리하게 --- .github/workflows/cd.yml | 112 +++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 67622168..8b9f63bf 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -80,20 +80,23 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_SSH_KEY }} - script: | - if [ "${{ steps.current_version.outputs.version }}" == "blue" ]; then - NEW_VERSION="green" - NEW_PORT=8081 - else - NEW_VERSION="blue" - NEW_PORT=8080 - fi - sudo docker pull kimsongmok/splanet:${{ env.IMAGE_TAG }} - sudo docker network inspect splanet >/dev/null 2>&1 || sudo docker network create splanet - sudo docker run -d --name splanet_${NEW_VERSION} \ - --network splanet \ - --env-file /home/ubuntu/env.properties \ - -p ${NEW_PORT}:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }} + envs: + CURRENT_VERSION: ${{ steps.current_version.outputs.version }} + IMAGE_TAG: ${{ env.IMAGE_TAG }} + script: | + if [ "$CURRENT_VERSION" == "blue" ]; then + NEW_VERSION="green" + NEW_PORT=8081 + else + NEW_VERSION="blue" + NEW_PORT=8080 + fi + sudo docker pull kimsongmok/splanet:$IMAGE_TAG + sudo docker network inspect splanet >/dev/null 2>&1 || sudo docker network create splanet + sudo docker run -d --name splanet_${NEW_VERSION} \ + --network splanet \ + --env-file /home/ubuntu/env.properties \ + -p ${NEW_PORT}:8080 --restart unless-stopped kimsongmok/splanet:$IMAGE_TAG - name: Wait for Spring Boot Application to Start uses: appleboy/ssh-action@v0.1.6 @@ -101,30 +104,31 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_SSH_KEY }} - script: | - if [ "${{ steps.current_version.outputs.version }}" == "blue" ]; then - NEW_VERSION="green" - NEW_PORT=8081 - else - NEW_VERSION="blue" - NEW_PORT=8080 + envs: + CURRENT_VERSION: ${{ steps.current_version.outputs.version }} + NEW_VERSION: ${{ steps.current_version.outputs.version == 'blue' && 'green' || 'blue' }} + script: | + if [ "$CURRENT_VERSION" == "blue" ]; then + NEW_PORT=8081 + else + NEW_PORT=8080 + fi + + echo "Waiting for the application to be healthy on http://api.splanet.co.kr:${NEW_PORT}/actuator/health..." + for i in {1..30}; do + if curl -s http://api.splanet.co.kr:${NEW_PORT}/actuator/health | grep '"status":"UP"' > /dev/null; then + echo "Application is healthy and ready to receive traffic." + break fi + echo "Waiting for application to start... (Attempt $i)" + sleep 5 + done - echo "Waiting for the application to be healthy on http://api.splanet.co.kr:${NEW_PORT}/actuator/health..." - for i in {1..30}; do - if curl -s http://api.splanet.co.kr:${NEW_PORT}/actuator/health | grep '"status":"UP"' > /dev/null; then - echo "Application is healthy and ready to receive traffic." - break - fi - echo "Waiting for application to start... (Attempt $i)" - sleep 5 - done - - if [ "$i" -eq 30 ]; then - echo "Application did not start successfully within the expected time." - sudo docker logs splanet_${NEW_VERSION} - exit 1 - fi + if [ "$i" -eq 30 ]; then + echo "Application did not start successfully within the expected time." + sudo docker logs splanet_${NEW_VERSION} + exit 1 + fi - name: Update Load Balancer Target Group uses: appleboy/ssh-action@v0.1.6 @@ -132,14 +136,16 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_SSH_KEY }} - script: | - if [ "${{ steps.current_version.outputs.version }}" == "blue" ]; then - TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet-8081/${{ secrets.ARN_ID_8081 }}" - else - TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet/${{ secrets.ARN_ID_8080 }}" - fi - aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:listener/app/splanet/${{ secrets.ARN_ID_443 }} \ - --default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN + envs: + CURRENT_VERSION: ${{ steps.current_version.outputs.version }} + script: | + if [ "$CURRENT_VERSION" == "blue" ]; then + TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet-8081/${{ secrets.ARN_ID_8081 }}" + else + TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet/${{ secrets.ARN_ID_8080 }}" + fi + aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:listener/app/splanet/${{ secrets.ARN_ID_443 }} \ + --default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN - name: Remove Old Version uses: appleboy/ssh-action@v0.1.6 @@ -147,11 +153,13 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_SSH_KEY }} - script: | - if [ "${{ steps.current_version.outputs.version }}" != "none" ]; then - sudo docker stop splanet_${{ steps.current_version.outputs.version }} - sudo docker rm splanet_${{ steps.current_version.outputs.version }} - fi + envs: + CURRENT_VERSION: ${{ steps.current_version.outputs.version }} + script: | + if [ "$CURRENT_VERSION" != "none" ]; then + sudo docker stop splanet_$CURRENT_VERSION + sudo docker rm splanet_$CURRENT_VERSION + fi - name: Clean up old Docker images uses: appleboy/ssh-action@v0.1.6 @@ -159,6 +167,6 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_SSH_KEY }} - script: | - docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print $1}' | xargs docker rmi -f - sudo docker system prune -f + script: | + docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print $1}' | xargs docker rmi -f + sudo docker system prune -f