Skip to content

Commit

Permalink
setting: AWS S3와 CodeDeploy 설정 후 github action 작성 완료 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
mingmingmon committed Oct 7, 2024
1 parent cf71535 commit 4827dc3
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/develop-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Develop Branch CI

on:
push:
branches: [ "setting/#48" ]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
# JDK 설정
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

# 환경 변수 파일 생성
- name: Set environment values
run: |
cd ./src/main/resources # resources 폴더로 이동
touch ./env.properties # env.properties 파일 생성
echo "${{ secrets.ENV }}" > ./env.properties # github actions secret에서 가져온 값을 application.yml 파일에 씁니다
shell: bash

# Gradle에 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# Gradle test
- name: Test with Gradle
run: ./gradlew test

# Gradle build
- name: Build Gradle
run: ./gradlew build

# 압축 파일 만들기
- name: Make zip file
run: |
mkdir develop-deploy
cp ./src/main/resources/env.properties ./deploy/.env
cp ./docker-compose.blue.yml ./deploy/
cp ./docker-compose.green.yml ./deploy/
cp ./appspec.yml ./deploy/
cp ./Dockerfile ./deploy/
cp ./scripts/*.sh ./deploy/
cp ./build/libs/*.jar ./deploy/
zip -r -qq -j ./munecting.zip ./deploy
# AWS 접속
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

# S3에 저장
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./munecting.zip s3://develop-code-deploy-munecting
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HELP.md
env.properties
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:21
WORKDIR /munecting
COPY munecting-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
19 changes: 19 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/app
overwrite: yes

permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
mode: 777

hooks:
ApplicationStart:
- location: develop-deploy.sh
timeout: 60
runas: ubuntu
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ dependencies {
implementation 'org.json:json:20240303'
implementation 'org.springframework.boot:spring-boot-starter-validation:2.5.2'

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'

Expand Down
10 changes: 10 additions & 0 deletions docker-compose.blue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#blue
version: '3'
services:
mohazi-api:
build: .
ports:
- "8081:8080"
container_name: spring-blue
environment:
- SPRING_PROFILES_ACTIVE=dev
10 changes: 10 additions & 0 deletions docker-compose.green.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#green
version: '3'
services:
mohazi-api:
build: .
ports:
- "8082:8080"
container_name: spring-green
environment:
- SPRING_PROFILES_ACTIVE=dev
29 changes: 29 additions & 0 deletions scripts/develop-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

cd /home/ubuntu/app

DOCKER_APP_NAME=spring

# 실행중인 blue가 있는지
EXIST_BLUE=$(docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps | grep running)

# green이 실행중이면 blue up
if [ -z "$EXIST_BLUE" ]; then
echo "blue up"
docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml up -d --build

sleep 30

docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml down
docker image prune -af # 사용하지 않는 이미지 삭제

# blue가 실행중이면 green up
else
echo "green up"
docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml up -d --build

sleep 30

docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml down
docker image prune -af
fi

0 comments on commit 4827dc3

Please sign in to comment.