diff --git a/.github/workflows/deploy-ec2.yml b/.github/workflows/deploy-ec2.yml new file mode 100644 index 00000000..731f66bf --- /dev/null +++ b/.github/workflows/deploy-ec2.yml @@ -0,0 +1,117 @@ +name: CI/CD Workflow for Multi-Module Project + +on: + push: + branches: + - setting/change-cicd-action + +jobs: + ci: + runs-on: ubuntu-latest + + env: + AWS_REGION: "ap-northeast-2" + + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'temurin' + + - name: Build and Test Each Module + run: | + ./gradlew :ttoklip-api:clean :ttoklip-api:build :ttoklip-api:test + ./gradlew :ttoklip-batch:clean :ttoklip-batch:build :ttoklip-batch:test + ./gradlew :ttoklip-notification:clean :ttoklip-notification:build :ttoklip-notification:test + + - name: Verify Build Artifacts + run: | + echo "Verifying build artifacts..." + ls -la ttoklip-api/build/libs/ + ls -la ttoklip-batch/build/libs/ + ls -la ttoklip-notification/build/libs/ + + - name: Save Build Artifacts + run: | + mkdir -p deploy + cp ttoklip-api/build/libs/ttoklip-api-SNAPSHOT.jar deploy/ + cp ttoklip-batch/build/libs/ttoklip-batch-SNAPSHOT.jar deploy/ + cp ttoklip-notification/build/libs/ttoklip-notification-SNAPSHOT.jar deploy/ + + cp ttoklip-api/Dockerfile-prod deploy/Dockerfile-api-prod + cp ttoklip-batch/Dockerfile-prod deploy/Dockerfile-batch-prod + cp ttoklip-notification/Dockerfile-prod deploy/Dockerfile-notification-prod + cp -r nginx deploy/ + cp docker-compose.prod.yml deploy/ + zip -r deploy-package.zip deploy + + - name: Configure AWS Credentials + run: | + aws configure set aws_access_key_id ${{ secrets.SSM_PUBLIC }} + aws configure set aws_secret_access_key ${{ secrets.SSM_PRIVATE }} + aws configure set region $AWS_REGION + + - name: Upload to S3 + run: | + aws s3 cp deploy-package.zip s3://ttoklip-deploy/zips/deploy-package.zip --region ap-northeast-2 + + cd: + needs: ci + runs-on: ubuntu-latest + + env: + AWS_REGION: "ap-northeast-2" + + steps: + - name: Configure AWS Credentials + run: | + aws configure set aws_access_key_id ${{ secrets.SSM_PUBLIC }} + aws configure set aws_secret_access_key ${{ secrets.SSM_PRIVATE }} + aws configure set region $AWS_REGION + + - name: Upload Deploy Script to S3 + run: | + cat << 'EOF' > deploy_script.sh + #!/bin/bash + + mkdir -p /home/ec2-user/ttoklip/application + sudo chown -R ec2-user:ec2-user /home/ec2-user/ttoklip + sudo chmod -R 755 /home/ec2-user/ttoklip + + if [ -d "/home/ec2-user/ttoklip/application/deploy" ]; then + rm -rf /home/ec2-user/ttoklip/application/deploy + fi + + aws s3 cp s3://ttoklip-deploy/zips/deploy-package.zip /home/ec2-user/ttoklip/application/deploy-package.zip --region ap-northeast-2 + + unzip -o /home/ec2-user/ttoklip/application/deploy-package.zip -d /home/ec2-user/ttoklip/application/ + + docker-compose -f /home/ec2-user/ttoklip/application/deploy/docker-compose.prod.yml --env-file /home/ec2-user/ttoklip/application/.env up -d --build + EOF + + zip deploy_script.zip deploy_script.sh + + aws s3 cp deploy_script.zip s3://ttoklip-deploy/zips/deploy-script.zip --region ap-northeast-2 + + - name: Deploy to EC2 via SSM + run: | + aws ssm send-command \ + --instance-ids "${{ secrets.EC2_INSTANCE_ID }}" \ + --document-name "AWS-RunShellScript" \ + --comment "Running deployment script" \ + --parameters commands=$'\ + aws s3 cp s3://ttoklip-deploy/zips/deploy-script.zip /home/ec2-user/ttoklip/application/ --region ap-northeast-2\n\ + cd /home/ec2-user/ttoklip/application/\n\ + unzip -o deploy-script.zip\n\ + chmod +x deploy_script.sh\n\ + ./deploy_script.sh' \ + --output-s3-bucket-name "ttoklip-deploy" \ + --output-s3-key-prefix "ssm-output" \ + --region $AWS_REGION + + + diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy-ecs-previous.txt similarity index 97% rename from .github/workflows/deploy.yml rename to .github/workflows/deploy-ecs-previous.txt index de748cbe..1d136451 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy-ecs-previous.txt @@ -1,8 +1,9 @@ -name: Deploy to Amazon ECS - Develop +name: Deploy to Amazon ECS on: - push: - branches: [ "main" ] + push: + branches-ignore: + - "**" # 모든 브랜치를 무시 env: AWS_REGION: ap-northeast-2 diff --git a/build.gradle b/build.gradle index c6f05b33..d05619fa 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ repositories { subprojects { group = 'com' - version = '0.0.1-SNAPSHOT' + version = 'SNAPSHOT' sourceCompatibility = '17' apply plugin: 'java' diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 526c5b43..8d16a230 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -3,8 +3,8 @@ version: '3.8' services: api: build: - context: ./ttoklip-api - dockerfile: Dockerfile-prod + context: . + dockerfile: Dockerfile-api-prod args: JASYPT_ENCRYPTOR_PASSWORD: ${JASYPT_ENCRYPTOR_PASSWORD} container_name: api @@ -28,8 +28,8 @@ services: batch: build: - context: ./ttoklip-batch - dockerfile: Dockerfile-prod + context: . + dockerfile: Dockerfile-batch-prod args: JASYPT_ENCRYPTOR_PASSWORD: ${JASYPT_ENCRYPTOR_PASSWORD} container_name: batch @@ -53,8 +53,8 @@ services: notification: build: - context: ./ttoklip-notification - dockerfile: Dockerfile-prod + context: . + dockerfile: Dockerfile-notification-prod args: JASYPT_ENCRYPTOR_PASSWORD: ${JASYPT_ENCRYPTOR_PASSWORD} container_name: notification diff --git a/nginx/nginx.conf b/nginx/nginx.conf index f5fbe433..76d8f039 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -11,6 +11,9 @@ http { sendfile on; keepalive_timeout 65; + # 로그 설정 + error_log /var/log/nginx/error.log debug; + # API 컨테이너에 대한 upstream 설정 upstream api_upstream { server api:8080; # API 컨테이너 이름과 포트 @@ -19,7 +22,11 @@ http { server { listen 80; - # 일반 API 요청을 처리 + location /health { + proxy_pass http://api_upstream/health; + proxy_set_header Host $host; + } + location / { proxy_pass http://api_upstream; proxy_set_header Host $host; diff --git a/ttoklip-api/Dockerfile-prod b/ttoklip-api/Dockerfile-prod index 79fdc5f3..dd3518f0 100644 --- a/ttoklip-api/Dockerfile-prod +++ b/ttoklip-api/Dockerfile-prod @@ -1,6 +1,6 @@ FROM openjdk:17 -ARG JAR_FILE=build/libs/ttoklip-api-0.0.1-SNAPSHOT.jar +ARG JAR_FILE=ttoklip-api-SNAPSHOT.jar ARG JASYPT_ENCRYPTOR_PASSWORD ENV SPRING_PROFILES_ACTIVE=prod diff --git a/ttoklip-batch/Dockerfile-prod b/ttoklip-batch/Dockerfile-prod index 3c041b6e..23ed2919 100644 --- a/ttoklip-batch/Dockerfile-prod +++ b/ttoklip-batch/Dockerfile-prod @@ -1,6 +1,6 @@ FROM openjdk:17 -ARG JAR_FILE=build/libs/ttoklip-batch-0.0.1-SNAPSHOT.jar +ARG JAR_FILE=ttoklip-batch-SNAPSHOT.jar ARG JASYPT_ENCRYPTOR_PASSWORD ENV SPRING_PROFILES_ACTIVE=prod diff --git a/ttoklip-notification/Dockerfile-prod b/ttoklip-notification/Dockerfile-prod index a5351f38..67365cdf 100644 --- a/ttoklip-notification/Dockerfile-prod +++ b/ttoklip-notification/Dockerfile-prod @@ -1,6 +1,6 @@ FROM openjdk:17 -ARG JAR_FILE=build/libs/ttoklip-notification-0.0.1-SNAPSHOT.jar +ARG JAR_FILE=ttoklip-notification-SNAPSHOT.jar ARG JASYPT_ENCRYPTOR_PASSWORD ENV SPRING_PROFILES_ACTIVE=prod