fix:backcicd.yaml json 파싱으로 변경3 #41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Backend CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- 48-Develop브랜치-푸시시-CI/CD-구현 | |
jobs: | |
build_and_deploy: | |
name: Build and Deploy Modules | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
include: | |
- module: auth | |
dockerfile_path: ./gitfolio-auth/Dockerfile | |
build_context: ./gitfolio-auth | |
image_name: aida0/gitfolio_auth:test | |
ec2_filters: | | |
Name=tag:Name,Values=Gitfolio BE1 | |
Name=tag:Environment,Values=dev | |
Name=tag:Type,Values=ec2 | |
- module: member | |
dockerfile_path: ./gitfolio-member/Dockerfile | |
build_context: ./gitfolio-member | |
image_name: aida0/gitfolio_member:test | |
ec2_filters: | | |
Name=tag:Name,Values=Gitfolio BE1 | |
Name=tag:Environment,Values=dev | |
Name=tag:Type,Values=ec2 | |
- module: payment | |
dockerfile_path: ./gitfolio-payment/Dockerfile | |
build_context: ./gitfolio-payment | |
image_name: aida0/gitfolio_payment:test | |
ec2_filters: | | |
Name=tag:Name,Values=Gitfolio BE2 | |
Name=tag:Environment,Values=dev | |
Name=tag:Type,Values=ec2 | |
- module: resume | |
dockerfile_path: ./gitfolio-resume/Dockerfile | |
build_context: ./gitfolio-resume | |
image_name: aida0/gitfolio_resume:test | |
ec2_filters: | | |
Name=tag:Name,Values=Gitfolio BE2 | |
Name=tag:Environment,Values=dev | |
Name=tag:Type,Values=ec2 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-northeast-2 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Build the Docker image | |
run: | | |
docker build \ | |
--build-arg REDIRECT_ONBOARDING_URL=${{ secrets.REDIRECT_ONBOARDING_URL }} \ | |
# ... 생략된 환경 변수들 ... | |
--build-arg KAFKA_PORT1=${{ secrets.KAFKA_PORT1 }} \ | |
-t ${{ matrix.image_name }} \ | |
${{ matrix.build_context }} | |
- name: Push the Docker image | |
run: | | |
docker push ${{ matrix.image_name }} | |
- name: Get EC2 Instance IDs | |
id: get_instances | |
run: | | |
FILTERS=$(echo "${{ matrix.ec2_filters }}" | tr '\n' ' ' | sed 's/\(Name=[^ ]*\)/"\1"/g') | |
INSTANCE_IDS=$(aws ec2 describe-instances \ | |
--region ap-northeast-2 \ | |
--filters $FILTERS \ | |
--query 'Reservations[].Instances[].InstanceId' \ | |
--output text) | |
echo "INSTANCE_IDS=$INSTANCE_IDS" | |
echo "instance_ids=$INSTANCE_IDS" >> $GITHUB_OUTPUT | |
- name: Deploy to EC2 instances | |
run: | | |
if [ -z "${{ steps.get_instances.outputs.instance_ids }}" ]; then | |
echo "No instance IDs found for ${{ matrix.module }} module. Exiting." | |
exit 1 | |
fi | |
aws ssm send-command \ | |
--instance-ids "${{ steps.get_instances.outputs.instance_ids }}" \ | |
--document-name "AWS-RunShellScript" \ | |
--comment "Deploying ${{ matrix.module }} module" \ | |
--parameters commands='cd /home/ec2-user && docker-compose down -v --rmi all && docker-compose pull && docker-compose up -d' \ | |
--timeout-seconds 600 \ | |
--region ap-northeast-2 | |
- name: Wait for command to complete | |
run: | | |
echo "Deployment command sent for ${{ matrix.module }} module." |