Skip to content

Commit

Permalink
refactor: CD 스크립트 수정
Browse files Browse the repository at this point in the history
env로 관리하게
  • Loading branch information
kanguk01 committed Nov 1, 2024
1 parent c80fdda commit 63564ac
Showing 1 changed file with 60 additions and 52 deletions.
112 changes: 60 additions & 52 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,85 +80,93 @@ 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/[email protected]
with:
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/[email protected]
with:
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/[email protected]
with:
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/[email protected]
with:
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

0 comments on commit 63564ac

Please sign in to comment.