Skip to content

Update rankit-multiaz-cicd.yml #13

Update rankit-multiaz-cicd.yml

Update rankit-multiaz-cicd.yml #13

name: rankit-multiaz-rolling-zerodowntime-cicd
on:
push:
branches: [ "develop" ]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
# 1. JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# 2. Gradle 설정
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
# 3. Jar 파일 빌드 (테스트 제외)
- name: Build with Gradle Wrapper
run: ./gradlew -x test bootJar
# 4. Docker Buildx 설정
- name: Set up Docker Build
uses: docker/setup-buildx-action@v1
# 5. Docker 로그인
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
# 6. Docker 이미지 빌드 및 푸시
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest
deploy:
runs-on: ubuntu-latest
needs: build
permissions:
contents: read
steps:
# 1. AWS CLI 설치
- name: Install AWS CLI

Check failure on line 56 in .github/workflows/rankit-multiaz-cicd.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/rankit-multiaz-cicd.yml

Invalid workflow file

You have an error in your yaml syntax on line 56
id : 1
run: |
sudo apt-get update
sudo apt-get install awscli -y
# 2. AWS 자격 증명 설정
- name: Configure AWS credentials
id : 2
needs : 1
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set region ap-northeast-2 # 서울 AWS 리전을 설정
# 3. EC2 인스턴스 대상 등록 해제 (EC2-A)
- name: Deregister EC2-A from Target Group
id : 3
needs : 2
run: |
aws elbv2 deregister-targets \
--target-group-arn ${{ secrets.TARGET_GROUP_ARN }} \
--targets Id=${{ secrets.EC2_A_ID }}
# 4. EC2-A에 Docker 컨테이너 배포
- name: Deploy to EC2-A via SSM
id : 4
needs : 3
run: |
aws ssm send-command \
--instance-ids "${{ secrets.EC2_A_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":["if [ $(docker ps -q -f \"ancestor='${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest'\" -f \"status=running\") ]; then docker stop $(docker ps -q -f \"ancestor='${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest'\"); fi && docker pull '${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest' && cd /home/ec2-user && docker-compose -f docker-compose.yml up -d"]}' \
--comment "Deploying application to EC2-A"
# 5. EC2-A에서 헬스체크 수행
- name: Perform health check on EC2-A
id : 5
needs : 4
id: healthcheck-ec2-a
run: |
RESULT=$(aws ssm send-command \
--instance-ids "${{ secrets.EC2_A_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":["curl -f http://localhost:8080/ || echo \"Health check failed on EC2-A\""]}' \
--comment "Performing health check on EC2-A" \
--query "Command.CommandId" \
--output text)
sleep 10 # 헬스 체크 결과를 기다리기 위해 대기
OUTPUT=$(aws ssm list-command-invocations --command-id $RESULT --details --output json)
# 헬스 체크 실패 여부 확인
if echo "$OUTPUT" | grep -q "Health check failed"; then
echo "Health check failed on EC2-A"
exit 1 # 파이프라인 종료
fi
# 6. EC2 인스턴스 대상 재등록 (EC2-A)
- name: Register EC2-A to Target Group
id : 6
needs : 5
run: |
aws elbv2 register-targets \
--target-group-arn ${{ secrets.TARGET_GROUP_ARN }} \
--targets Id=${{ secrets.EC2_A_ID }}
# 7. EC2 인스턴스 대상 등록 해제 (EC2-C)
- name: Deregister EC-C from Target Group
id : 7
needs : 6
run: |
aws elbv2 deregister-targets \
--target-group-arn ${{ secrets.TARGET_GROUP_ARN }} \
--targets Id=${{ secrets.EC2_C_ID }}
# 8. EC2-C에 Docker 컨테이너 배포
- name: Deploy to EC2-C via SSM
id : 8
needs : 7
run: |
aws ssm send-command \
--instance-ids "${{ secrets.EC2_C_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":["if [ $(docker ps -q -f \"ancestor='${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest'\" -f \"status=running\") ]; then docker stop $(docker ps -q -f \"ancestor='${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest'\"); fi && docker pull '${{ secrets.DOCKER_USERNAME }}/rankitrun-be:latest' && cd /home/ec2-user && docker-compose -f docker-compose.yml up -d"]}' \
--comment "Deploying application to EC2-C"
# 9. EC2-C에서 헬스체크 수행
- name: Perform health check on EC-C
id :9
needs : 8
run: |
RESULT=$(aws ssm send-command \
--instance-ids "${{ secrets.EC2_C_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":["curl -f http://localhost:8080/ || echo \"Health check failed on EC-C\""]}' \
--comment "Performing health check on EC-C" \
--query "Command.CommandId" \
--output text)
sleep 10 # 헬스 체크 결과를 기다리기 위해 대기
OUTPUT=$(aws ssm list-command-invocations --command-id $RESULT --details --output json)
# 헬스 체크 실패 여부 확인
if echo "$OUTPUT" | grep -q "Health check failed"; then
echo "Health check failed on EC-C"
exit 1 # 파이프라인 종료
fi
# 10. EC2 인스턴스 대상 재등록 (EC-C)
- name: Register EC-C to Target Group
id : 10
needs : 9
run: |
aws elbv2 register-targets \
--target-group-arn ${{ secrets.TARGET_GROUP_ARN }} \
--targets Id=${{ secrets.EC2_C_ID }}