Skip to content

Commit

Permalink
Merge pull request #279 from ASAP-as-soon-as-possible/feat/#277
Browse files Browse the repository at this point in the history
#277 [feat] CD 방식 도커로 수정
  • Loading branch information
KWY0218 authored Jul 12, 2024
2 parents 7954911 + 5659b1d commit 6f27ce9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 87 deletions.
147 changes: 63 additions & 84 deletions .github/workflows/prod-cd.yml
Original file line number Diff line number Diff line change
@@ -1,96 +1,75 @@
# 워크플로우의 이름 지정
name: ASAP-Server CD

# 해당 workflow가 언제 실행될 것인지에 대한 트리거를 지정
on:
push:
branches: [ "main" ] # main branch로 push 될 때 실행됩니다.

env:
S3_BUCKET_NAME: asap-prod
branches: [ "main" ]

jobs:
build:
name: Code deployment

# 실행 환경
runs-on: ubuntu-latest

steps:

# 1) 워크플로우 실행 전 기본적으로 체크아웃 필요
- name: checkout
uses: actions/checkout@v3

# 2) JDK 11버전 설치, 다른 JDK 버전을 사용하다면 수정
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# 3) 환경변수 파일 생성
- name: make application.properties 파일 생성
run: |
## create application.yml
cd ./src/main/resources
# application.yml 파일 생성
touch ./application.yml
# GitHub-Actions 에서 설정한 값을 application.yml 파일에 쓰기
echo "${{ secrets.ASAP_APPLICATION }}" >> ./application.yml
# application.yml 파일 확인
cat ./application.yml
shell: bash

# 이 워크플로우는 gradle build
- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle # 실제 application build(-x 옵션을 통해 test는 제외)
run: ./gradlew build -x test

# 디렉토리 생성
- name: Make Directory
run: mkdir -p deploy

# Jar 파일 복사
- name: Copy Jar
run: cp ./build/libs/*.jar ./deploy

# appspec.yml 파일 복사
- name: Copy appspec.yml
run: cp appspec.yml ./deploy

# script files 복사
- name: Copy script
run: cp ./scripts/*.sh ./deploy

- name: Make zip file
run: zip -r ./asap_server.zip ./deploy
shell: bash

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ap-northeast-2

- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./asap_server.zip s3://$S3_BUCKET_NAME/

# Deploy
- name: Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
run:
aws deploy create-deployment
--application-name asap-prod-code-deploy
--deployment-group-name asap-prod-code-deploy-group
--file-exists-behavior OVERWRITE
--s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=asap_server.zip
--region ap-northeast-2
- name: checkout
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: make application.yml 파일 생성
run: |
## create application.yml
cd ./src/main/resources
# application.yml 파일 생성
touch ./application.yml
# GitHub-Actions 에서 설정한 값을 application.yml 파일에 쓰기
echo "${{ secrets.DOCKER_YAML }}" >> ./application.yml
# application.yml 파일 확인
cat ./application.yml
shell: bash

- name: Build with Gradle
run: |
chmod +x gradlew
./gradlew build -x test
- name: docker build 가능하도록 환경 설정
uses: docker/[email protected]

- name: docker hub에로그인
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}
password: ${{ secrets.DOCKERHUB_LOGIN_ACCESSTOKEN }}

- name: JAR file 로 부터 서버 버전 추출
id: server_version
run: |
jar_file=$(ls build/libs/server-*.jar | head -n 1)
version=$(echo $jar_file | grep -oP 'server-\K[^\-]+(?=\.jar)')
echo "Version: $version"
echo "::set-output name=version::$version"
- name: docker image 빌드 및 푸시
run: |
docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} .
docker tag ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:${{ steps.server_version.outputs.version }}
docker push ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:${{ steps.server_version.outputs.version }}
docker push ${{ secrets.DOCKERHUB_LOGIN_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}
- name: 도커 컨테이너 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.RELEASE_SERVER_IP }}
username: ${{ secrets.RELEASE_SERVER_USER }}
key: ${{ secrets.RELEASE_SERVER_KEY }}
script: |
cd ~
sudo ./deploy.sh
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM amd64/amazoncorretto:17-alpine

COPY ./build/libs/*.jar /asap-server.jar

CMD ["java", "-Duser.timezone=Asia/Seoul", "-jar", "asap-server.jar"]
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ plugins {
}

group = 'com.asap'
version = '0.0.1-SNAPSHOT'
version = '1.1.1'

java {
sourceCompatibility = '17'
}

jar {
enabled = false;
}

bootJar {
enabled = true;
}

configurations {
compileOnly {
extendsFrom annotationProcessor
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/file-error-appender.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<included>
<appender name="FILE-ERROR" class="ch.qos.logback.core.FileAppender">
<file>/home/ubuntu/app/logs/error/error-${BY_DATE}.log</file>
<file>/logs/error/error-${BY_DATE}.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/file-info-appender.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<included>
<appender name="FILE-INFO" class="ch.qos.logback.core.FileAppender">
<file>/home/ubuntu/app/logs/info/info-${BY_DATE}.log</file>
<file>/logs/info/info-${BY_DATE}.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
Expand Down

0 comments on commit 6f27ce9

Please sign in to comment.