Skip to content

Commit

Permalink
Merge pull request #292 from depromeet/develop
Browse files Browse the repository at this point in the history
refactor: layer-admin, layer-batch 코드 layer-api로 이관
  • Loading branch information
clean2001 authored Feb 16, 2025
2 parents 09af213 + fb5bc6f commit 66f8dd7
Show file tree
Hide file tree
Showing 42 changed files with 910 additions and 368 deletions.
75 changes: 28 additions & 47 deletions .github/workflows/aws-cicd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- develop

pull_request:
branches:
- develop
Expand All @@ -21,11 +22,16 @@ jobs:
packages: write
outputs:
deploy_target: ${{ steps.set-env.outputs.DEPLOY_TARGET }}

steps:
- name: Setup Env
id: set-env
run: |
echo "DEPLOY_TARGET=development" >> $GITHUB_OUTPUT
if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
echo "DEPLOY_TARGET=production" >> $GITHUB_OUTPUT
else
echo "DEPLOY_TARGET=development" >> $GITHUB_OUTPUT
fi
build:
name: build
Expand All @@ -36,7 +42,7 @@ jobs:
packages: write

env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }} # 이부분
REGISTRY: "docker.io"
NAMESPACE: "clean01"
APPLICATION_SECRET_PROPERTIES: ${{ secrets.AWS_APPLICATION_SECRET_PROPERTIES }}
Expand All @@ -57,27 +63,14 @@ jobs:

- name: Create application-secret.properties
run: |
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-api/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-batch/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-admin/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-api/src/main/resources/application-secret.properties
- name: Build layer-api module
run: ./gradlew :layer-api:build

- name: Test layer-api module
run: ./gradlew :layer-api:test

- name: Build layer-batch module
run: ./gradlew :layer-batch:build

- name: Test layer-batch module
run: ./gradlew :layer-batch:test

- name: Build layer-admin module
run: ./gradlew :layer-admin:build

- name: Test layer-admin module
run: ./gradlew :layer-admin:test

- name: Docker Hub Login
uses: docker/login-action@v1
Expand All @@ -90,40 +83,20 @@ jobs:
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-batch
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-admin
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api
- name: Push layer-api Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-api
file: ./layer-api/Dockerfile-dev # Dockerfile 이름 지정
file: ./layer-api/Dockerfile # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api:latest
- name: Push layer-batch Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-batch
file: ./layer-batch/Dockerfile-batch # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-batch:latest
- name: Push layer-admin Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-admin
file: ./layer-admin/Dockerfile-admin # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-admin:latest
build-args: |
SPRING_PROFILE=dev
no-cache: true

deploy:
name: Deploy
Expand All @@ -132,6 +105,7 @@ jobs:
if: github.event_name != 'pull_request'
env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
HOST_IP: ${{ needs.setup.outputs.host_ip }}

steps:
- name: Checkout sources
Expand All @@ -140,8 +114,6 @@ jobs:
- name: Create application-secret.properties file
run: |
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-api/infra/${{ env.DEPLOY_TARGET }}/application-secret.properties
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-batch/src/main/resources/application-secret.properties
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-admin/src/main/resources/application-secret.properties
- name: Archive Files
run: |
Expand Down Expand Up @@ -171,6 +143,17 @@ jobs:
sudo chown -R ubuntu:ubuntu /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
- name: Set Permissions on Transferred Files
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_DEV_INSTANCE_HOST }}
username: ubuntu
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
script: |
sudo chmod -R 755 /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
sudo chown -R ubuntu:ubuntu /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
- name: Deploy with Docker Compose
uses: appleboy/ssh-action@master
with:
Expand All @@ -179,10 +162,8 @@ jobs:
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
script: |
sudo apt update
sudo apt install docker-ce
sudo apt install docker-compose
sudo docker login --username ${{ secrets.DOCKER_EMAIL }} --password ${{ secrets.DOCKER_PASSWORD }}
cd /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
sudo docker-compose pull && sudo docker-compose up -d
chmod 777 ./deploy.sh
./deploy.sh
sudo docker image prune -a -f
57 changes: 10 additions & 47 deletions .github/workflows/aws-cicd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main

pull_request:
branches:
- main
Expand All @@ -21,6 +22,7 @@ jobs:
packages: write
outputs:
deploy_target: ${{ steps.set-env.outputs.DEPLOY_TARGET }}

steps:
- name: Setup Env
id: set-env
Expand All @@ -36,7 +38,7 @@ jobs:
packages: write

env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }} # 이부분
REGISTRY: "docker.io"
NAMESPACE: "clean01"
APPLICATION_SECRET_PROPERTIES: ${{ secrets.AWS_APPLICATION_SECRET_PROPERTIES }}
Expand All @@ -57,28 +59,14 @@ jobs:

- name: Create application-secret.properties
run: |
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-api/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-batch/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-admin/src/main/resources/application-secret.properties
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-api/src/main/resources/application-secret.properties
- name: Build layer-api module
run: ./gradlew :layer-api:build

- name: Test layer-api module
run: ./gradlew :layer-api:test

- name: Build layer-batch module
run: ./gradlew :layer-batch:build

- name: Test layer-batch module
run: ./gradlew :layer-batch:test

- name: Build layer-admin module
run: ./gradlew :layer-admin:build

- name: Test layer-admin module
run: ./gradlew :layer-admin:test

- name: Docker Hub Login
uses: docker/login-action@v1
with:
Expand All @@ -90,49 +78,29 @@ jobs:
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-batch
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-admin
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api
- name: Push layer-api Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-api
file: ./layer-api/Dockerfile-prod # Dockerfile 이름 지정
file: ./layer-api/Dockerfile # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-api:latest
build-args: |
SPRING_PROFILE=prod
no-cache: true

- name: Push layer-batch Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-batch
file: ./layer-batch/Dockerfile-batch # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-batch:latest
no-cache: true

- name: Push layer-admin Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-admin
file: ./layer-admin/Dockerfile-admin # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}_layer-admin:latest
deploy:
name: Deploy
needs: [ build, setup ]
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
HOST_IP: ${{ needs.setup.outputs.host_ip }}

steps:
- name: Checkout sources
Expand All @@ -141,8 +109,6 @@ jobs:
- name: Create application-secret.properties file
run: |
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-api/infra/${{ env.DEPLOY_TARGET }}/application-secret.properties
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-batch/src/main/resources/application-secret.properties
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-admin/src/main/resources/application-secret.properties
- name: Archive Files
run: |
Expand All @@ -151,7 +117,7 @@ jobs:
- name: Send Docker Compose
uses: appleboy/scp-action@master
with:
host: ${{ secrets.AWS_PROD_INSTANCE_HOST }}
host: ${{ secrets.AWS_PROD_INSTANCE_HOST }} # aws-cicd-dev.yml과 분리한 이유
username: ubuntu
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
Expand Down Expand Up @@ -191,9 +157,6 @@ jobs:
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
script: |
sudo apt update
sudo apt install docker-ce
sudo apt install docker-compose
sudo docker login --username ${{ secrets.DOCKER_EMAIL }} --password ${{ secrets.DOCKER_PASSWORD }}
cd /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
chmod 777 ./deploy.sh
Expand Down
2 changes: 1 addition & 1 deletion layer-admin/Dockerfile-admin → layer-admin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ COPY ${JAR_FILE} layer-admin.jar

ENV SPRING_PROFILE=${SPRING_PROFILE}

ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul" ,"-jar" ,"layer-admin.jar"]
ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_PROFILE}","-jar" ,"layer-admin.jar"]
8 changes: 7 additions & 1 deletion layer-api/Dockerfile-dev → layer-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
FROM openjdk:17

ARG JAR_FILE=./build/libs/*.jar
ARG SPRING_PROFILE

COPY ${JAR_FILE} layer-server.jar
ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul","-Dspring.profiles.active=dev" ,"-jar" ,"layer-server.jar"]

ENV SPRING_PROFILE=${SPRING_PROFILE}

ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul","-Dspring.profiles.active=${SPRING_PROFILE}" ,"-jar" ,"layer-server.jar"]
4 changes: 0 additions & 4 deletions layer-api/Dockerfile-prod

This file was deleted.

66 changes: 66 additions & 0 deletions layer-api/infra/development/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

IS_GREEN=$(sudo docker ps | grep layer-api-green) # 현재 실행중인 App이 blue인지 확인합니다.
DEFAULT_CONF="/etc/nginx/nginx.conf"


if [ -z $IS_GREEN ];then # blue라면

echo "### BLUE => GREEN ###"

echo "1. get green image"
cd ./layer-api/infra/production

echo "1.1. pull latest green image"
sudo docker-compose -f docker-compose-green.yaml pull

echo "2. green container up"
sudo docker-compose -f docker-compose-green.yaml up -d

while [ 1 = 1 ]; do
echo "3. green health check..."
sudo sleep 3

REQUEST=$(sudo curl http://127.0.0.1:8080/actuator/health) # green으로 request
if [ -n "$REQUEST" ]; then # 서비스 가능하면 health check 중지
echo "health check success"
break ;
fi
done;

echo "4. reload nginx"
sudo cp ./nginx.green.conf /etc/nginx/nginx.conf
sudo nginx -s reload

echo "5. blue container down"
sudo docker-compose -f docker-compose-blue.yaml rm -s -f layer-api-blue
else
echo "### GREEN => BLUE ###"
echo "1. get blue image"
cd ./layer-api/infra/production

echo "1.1. pull latest blue image"
sudo docker-compose -f docker-compose-blue.yaml pull

echo "2. blue container up"
sudo docker-compose -f docker-compose-blue.yaml up -d


while [ 1 = 1 ]; do
echo "3. blue health check..."
sleep 3
REQUEST=$(curl http://127.0.0.1:8081/actuator/health) # blue로 request

if [ -n "$REQUEST" ]; then # 서비스 가능하면 health check 중지
echo "health check success"
break ;
fi
done;

echo "4. reload nginx"
sudo cp ./nginx.blue.conf /etc/nginx/nginx.conf
sudo nginx -s reload

echo "5. green container down"
sudo docker-compose -f docker-compose-green.yaml rm -s -f layer-api-green
fi
Loading

0 comments on commit 66f8dd7

Please sign in to comment.