Skip to content

Commit

Permalink
feat: 알림 서버 상용 배포 CI/CD 작업 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Sep 6, 2024
1 parent b28cb22 commit 2528420
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/showpotAlarm-dev-cd.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: ShowPot-Dev-CD
name: ShowPot-Alarm-Dev-CD

on:
push:
branches:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/showpotAlarm-dev-ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ShowPotAlarm-Dev-CI
name: ShowPot-Alarm-Dev-CI

on:
pull_request:
Expand Down
105 changes: 105 additions & 0 deletions .github/workflows/showpotAlarm-prod-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: ShowPot-Alarm-Prod-CD

on:
push:
branches:
- prod

env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: showpot-alarm-application

jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'liberica'
cache: gradle

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Copy Secrets
uses: microsoft/variable-substitution@v1
with:
files: |
./app/src/main/resources/application-prod.yml,
./app/domain/common-domain/src/main/resources/application-domain-prod.yml,
./app/infrastructure/fcm/src/main/resources/application-fcm-prod.yml
env:
spring.datasource.url: ${{ secrets.APPLICATION_DATASOURCE_URL_DEV }}
spring.datasource.username: ${{ secrets.APPLICATION_DATASOURCE_USERNAME }}
spring.datasource.password: ${{ secrets.APPLICATION_DATASOURCE_PASSWORD }}
cloud.aws.credentials.accessKey: ${{ secrets.AWS_ACCESS_KEY }}
cloud.aws.credentials.secretKey: ${{ secrets.AWS_SECRET_KEY }}
fcm.project_id: ${{ secrets.FCM_PROJECT_ID }}
fcm.private_key_id: ${{ secrets.FCM_PRIVATE_KEY_ID }}
fcm.private_key: ${{ secrets.FCM_PRIVATE_KEY }}
fcm.client_email: ${{ secrets.FCM_CLIENT_EMAIL }}
fcm.client_id: ${{ secrets.FCM_CLIENT_ID }}
spring.data.redis.host: ${{ secrets.REDIS_HOST_PROD }}
spring.data.redis.port: ${{ secrets.REDIS_PORT_PROD }}

- name: Build with Gradle Wrapper
run: ./gradlew clean build -Dspring.profiles.active=prod

- name: Prepare File for Deployment
run: |
mkdir -p deployment-alarm/app/build/libs
cp dockerfile-alarm-dev docker-compose-alarm-dev.yml deployment-alarm/
cp -r ./app/build/libs deployment-alarm/app/build
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ap-northeast-2

- name: Set image tag
id: set-tag
run: |
DATE_TAG=$(TZ='Asia/Seoul' date +'%Y%m%d_%H%M')
SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-8)
echo "IMAGE_TAG=${DATE_TAG}_${SHORT_SHA}" >> $GITHUB_ENV
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build and Push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker build --no-cache -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f dockerfile-alarm-prod .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
IMAGE_URI=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV
- name: Update ECS task definition
id: update-task
run: |
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition showpot-alarm --region $AWS_REGION)
NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE_URI "$IMAGE_URI" '
.taskDefinition |
.containerDefinitions[0].image=$IMAGE_URI |
del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy)')
echo $NEW_TASK_DEFINITION > new-task-def.json
REGISTERED_TASK_DEFINITION=$(aws ecs register-task-definition --cli-input-json file://new-task-def.json)
TASK_REVISION=$(echo $REGISTERED_TASK_DEFINITION | jq -r '.taskDefinition.taskDefinitionArn')
echo "Registered new task definition revision: $TASK_REVISION"
echo "TASK_REVISION=$TASK_REVISION" >> $GITHUB_ENV
- name: Deploy to ECS service
run: |
aws ecs update-service --cluster showpot-cluster --service showpot-alarm-service --task-definition $TASK_REVISION --force-new-deployment --region $AWS_REGION
68 changes: 68 additions & 0 deletions .github/workflows/showpotAlarm-prod-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: ShowPot-Alarm-Prod-CI

on:
pull_request:
branches:
- prod
push:
branches:
- feat/prod-setting

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'liberica'
cache: gradle

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Copy Secrets
uses: microsoft/variable-substitution@v1
with:
files: |
./app/src/main/resources/application-dev.yml,
./app/domain/common-domain/src/main/resources/application-domain-dev.yml,
./app/infrastructure/fcm/src/main/resources/application-fcm-dev.yml
env:
spring.datasource.url: ${{ secrets.APPLICATION_DATASOURCE_URL_DEV }}
spring.datasource.username: ${{ secrets.APPLICATION_DATASOURCE_USERNAME }}
spring.datasource.password: ${{ secrets.APPLICATION_DATASOURCE_PASSWORD }}
fcm.project_id: ${{ secrets.FCM_PROJECT_ID }}
fcm.private_key_id: ${{ secrets.FCM_PRIVATE_KEY_ID }}
fcm.private_key: ${{ secrets.FCM_PRIVATE_KEY }}
fcm.client_email: ${{ secrets.FCM_CLIENT_EMAIL }}
fcm.client_id: ${{ secrets.FCM_CLIENT_ID }}

- name: Build with Gradle Wrapper
run: ./gradlew clean build -Dspring.profiles.active=prod

- name: Backend CI Discord Notification
uses: sarisia/actions-status-discord@v1
if: success()
with:
title: ✅ ShowPot Alarm Backend Prod CI success ✅
webhook: ${{ secrets.DISCORD_WEBHOOK_URL }}
content: "<@1084774841460215839> <@281597829636423682> ShoPot-Alarm Server 이슈 혹은 PR을 확인해주세요!"
color: 00FF00
username: showPot-Bot
avatar_url: ${{ secrets.DISCORD_NOTIFICATION_SUCCESS_AVATAR_URL }}

- name: Backend CI Discord Notification
uses: sarisia/actions-status-discord@v1
if: failure()
with:
title: ❗️ShowPot Alarm Backend CI failed ❗️
webhook: ${{ secrets.DISCORD_WEBHOOK_URL }}
color: FF0000
username: showPot-Bot
avatar_url: ${{ secrets.DISCORD_NOTIFICATION_FAILED_AVATAR_URL }}
12 changes: 11 additions & 1 deletion app/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ spring:
enabled: true
docker:
compose:
enabled: false
enabled: false
data:
redis:
host: ${REDIS_HOST_PROD}
port: ${REDIS_PORT_PROD}

cloud:
aws:
credentials:
accessKey: ${ACCESS_KEY}
secretKey: ${SECRET_KEY}
27 changes: 0 additions & 27 deletions docker-compose-alarm-prod.yml

This file was deleted.

2 changes: 1 addition & 1 deletion dockerfile-alarm-prod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ COPY --from=build ${EXTRACTED}/spring-boot-loader/ ./
COPY --from=build ${EXTRACTED}/snapshot-dependencies/ ./
COPY --from=build ${EXTRACTED}/application/ ./

ENTRYPOINT java -Dspring.profiles.active=dev org.springframework.boot.loader.launch.JarLauncher
ENTRYPOINT java -Dspring.profiles.active=prod org.springframework.boot.loader.launch.JarLauncher

0 comments on commit 2528420

Please sign in to comment.