Skip to content

Commit

Permalink
cd: 배포 방식 변경(beanstalk > ec2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimday0326 committed Aug 30, 2024
1 parent b329901 commit 8f61643
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 143 deletions.
3 changes: 0 additions & 3 deletions .ebextensions-dev/00-set-timezone.config

This file was deleted.

106 changes: 45 additions & 61 deletions .github/workflows/cd_gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ on:
permissions:
contents: read

env:
DOCKER_REPOSITORY: sponus
APPLICATION_SECRET_PATH: ./api/src/main/resources

jobs:
cd:
deploy-with-docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,7 +25,7 @@ jobs:
java-version: '17'
distribution: 'temurin'

## gradle 캐싱
# Gradle caching
- name: Gradle Caching
uses: actions/cache@v4
with:
Expand All @@ -32,15 +36,12 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-
## create application.yml
- name: make application-secret.yml
# Create application-secret.yaml
- name: Make application-secret.yml
run: |
mkdir -p ./api/src/main/resources
touch ./api/src/main/resources/application-secret.yml
shell: bash
- name: deliver application-secret.yml
run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
shell: bash
mkdir -p $APPLICATION_SECRET_PATH
touch $APPLICATION_SECRET_PATH/application-secret.yml
echo "${{ secrets.APPLICATION_SECRET }}" > $APPLICATION_SECRET_PATH/application-secret.yml
## firebase-key 설정
- name: Set FCM
Expand All @@ -50,64 +51,47 @@ jobs:
mkdir -p ./core/core-infra-firebase/src/main/resources/firebase
echo $DATA > ./core/core-infra-firebase/src/main/resources/firebase/firebase-key.json
# 빌드 및 테스트 단계.
# Grant execute permission for gradlew
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
run: chmod +x gradlew

# gradle build
# Build with Gradle
- name: Build with Gradle
run: ./gradlew clean build -x test
run: ./gradlew build -x test

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
# Docker Hub 로그인
- name: Docker Hub 로그인
uses: docker/login-action@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACTION_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_ACTION_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: beanstalk
IMAGE_TAG: latest
# Docker build & Push
- name: Docker build & Push
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPOSITORY }} .
docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPOSITORY }}
- name: Get current time
uses: 1466587594/get-current-time@v2
id: current-time
# Send deploy.sh & docker-compose.yaml
- name: Send deploy.sh
uses: appleboy/scp-action@master
with:
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"

- name: Show Current Time
run: echo "CurrentTime=${{ steps.current-time.outputs.formattedTime }}"
shell: bash

- name: Generate deployment package
run: |
mkdir -p deploy
cp -r .ebextensions-dev deploy/.ebextensions
cp -r .platform deploy/.platform
cp Dockerrun.aws.json deploy/Dockerrun.aws.json
cd deploy && zip -r deploy.zip .
- name: Beanstalk Deploy
uses: einaregilsson/beanstalk-deploy@v20
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
port: 22
source: "./deploy/*"
target: "~"

# Deploy to Dev
- name: Deploy to Dev
uses: appleboy/ssh-action@master
with:
aws_access_key: ${{ secrets.AWS_ACTION_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_ACTION_SECRET_ACCESS_KEY }}
application_name: project-prod-study
environment_name: Project-prod-study-env
version_label: github-action-${{ steps.current-time.outputs.formattedTime }}
region: ap-northeast-2
deployment_package: deploy/deploy.zip
wait_for_deployment: false
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
cd deploy
chmod 777 ./deploy.sh
./deploy.sh
docker image prune -f
30 changes: 14 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# Eclipse Temurin OpenJDK 17 이미지를 사용
FROM eclipse-temurin:17-jdk
ARG JAR_FILE=api/build/libs/*.jar

COPY ${JAR_FILE} app.jar
FROM eclipse-temurin:17-jdk AS builder
WORKDIR application
ARG JAR_FILE=./api/build/libs/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

# Redis 및 supervisord 설치
RUN apt-get update && \
apt-get install -y redis-server supervisor && \
rm -rf /var/lib/apt/lists/*

# supervisord 설정 파일 복사
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
FROM eclipse-temurin:17-jdk
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./

# 포트 노출
EXPOSE 8080 6379
# 애플리케이션 JAR 파일이 이미 압축이 해제 되었으므로, JarLauncher를 사용하여 애플리케이션을 시작
ENV TZ=Asia/Seoul
ENTRYPOINT ["java", "-Dspring.profiles.active=prod", "-Duser.timezone=Asia/Seoul", "org.springframework.boot.loader.launch.JarLauncher"]

# supervisord를 사용하여 애플리케이션과 Redis 실행
CMD ["/usr/bin/supervisord"]
17 changes: 0 additions & 17 deletions Dockerrun.aws.json

This file was deleted.

33 changes: 0 additions & 33 deletions HELP.md

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jar { enabled = true }
allprojects {

group = 'com.sponus'
version = '0.0.1-SNAPSHOT'
version = '0.0.1'

java {
sourceCompatibility = '17'
Expand Down
10 changes: 10 additions & 0 deletions core/core-infra-redis/src/main/resources/application-redis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
spring.config.activate.on-profile: local

spring:
data:
redis:
host: localhost
port: 6379
---
spring.config.activate.on-profile: prod

spring:
data:
redis:
Expand Down
72 changes: 72 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

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

BLUE_PORT=8081
GREEN_PORT=8082

send_failure_message() {
echo "Health check failed after 10 attempts, sending failure message and exiting."
exit 1
}

if [ -z $IS_GREEN ]; then # blue인 경우

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

echo "1. get green image"
docker compose pull green # green으로 이미지를 내려받습니다.

echo "2. green container up"
docker compose up -d green # green 컨테이너 실행

for i in {1..15}; do
echo "3. green health check attempt $i..."
sleep 3

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

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

echo "5. blue container down"
docker compose stop blue
else
echo "### GREEN => BLUE ###"

echo "1. get blue image"
docker compose pull blue

echo "2. blue container up"
docker compose up -d blue

for i in {1..15}; do
echo "3. blue health check attempt $i..."
sleep 3

REQUEST=$(curl http://127.0.0.1:$BLUE_PORT) # blue로 request

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

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

echo "5. green container down"
docker compose stop green
fi
12 changes: 12 additions & 0 deletions deploy/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
blue:
image: dhxl50/sponus:latest
container_name: blue
ports:
- "8081:8080"

green:
image: dhxl50/sponus:latest
container_name: green
ports:
- "8082:8080"
12 changes: 0 additions & 12 deletions supervisord.conf

This file was deleted.

0 comments on commit 8f61643

Please sign in to comment.