Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS CI/CD Docker로 변경 #162

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 114 additions & 106 deletions .github/workflows/blank.yml
Original file line number Diff line number Diff line change
@@ -1,107 +1,115 @@
name: Deploy to Amazon EC2

on:
pull_request:
branches:
- develop
- 'feat/**' # feat으로 시작하는 브랜치에 대해서도 동작
push:
branches:
- develop
- 'feat/**' # feat으로 시작하는 브랜치에 대해서도 동작
#name: Deploy to Amazon EC2
#
#on:
# pull_request:
# branches:
# - develop
# - 'feat/**' # feat으로 시작하는 브랜치에 대해서도 동작
# push:
# branches:
# - develop
# - 'feat/**' # feat으로 시작하는 브랜치에 대해서도 동작
####
##

# 본인이 설정한 값을 여기서 채워넣습니다.
# 리전, 버킷 이름, CodeDeploy 앱 이름, CodeDeploy 배포 그룹 이름
env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: just-s3
CODE_DEPLOY_APPLICATION_NAME: just-codedeploy-app
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: just-codedeploy-development-group

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
# (1) 기본 체크아웃
- name: Checkout
uses: actions/checkout@v3

# (2) JDK 11 세팅
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'

- name: Copy Firebase
env:
FIREBASE: ${{ secrets.FIREBASE }}
FIREBASE_DIR: src/main/resources/key
FIREBASE_DIR_FILE_NAME: just-firebase-key.json
run: |
mkdir -p $FIREBASE_DIR
echo $FIREBASE | base64 --decode > $FIREBASE_DIR/$FIREBASE_DIR_FILE_NAME

- name: Copy Secret
env:
OCCUPY_SECRET: ${{ secrets.OCCUPY_SECRET }}
OCCUPY_SECRET_DIR: src/main/resources
OCCUPY_SECRET_DIR_FILE_NAME: application.yml
run: echo $OCCUPY_SECRET | base64 --decode > $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME


- name: Copy Firebase
env:
FIREBASE: ${{ secrets.FIREBASE }}
FIREBASE_DIR: src/main/resources/key
FIREBASE_DIR_FILE_NAME: just-firebase-key.json
run: |
mkdir -p $FIREBASE_DIR
echo $FIREBASE | base64 --decode > $FIREBASE_DIR/$FIREBASE_DIR_FILE_NAME


# (3) Gradle build (Test 제외)
- name: Build with Gradle
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
with:
arguments: clean build -x test

# - name: Docker build
# run: |
# docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
# docker build -t app .
# docker tag app ${{ secrets.DOCKER_USERNAME }}/sejongmate:latest
# docker push ${{ secrets.DOCKER_USERNAME }}/sejongmate:latest

# (4) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용)
- 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: ${{ env.AWS_REGION }}

# (5) 빌드 결과물을 S3 버킷에 업로드
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--ignore-hidden-files \
--s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \
--source .

# (6) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행
- name: Deploy to AWS EC2 from S3
run: |
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip
### 본인이 설정한 값을 여기서 채워넣습니다.
### 리전, 버킷 이름, CodeDeploy 앱 이름, CodeDeploy 배포 그룹 이름
##env:
## AWS_REGION: ap-northeast-2
## S3_BUCKET_NAME: just-s3
## CODE_DEPLOY_APPLICATION_NAME: just-codedeploy-app
## CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: just-codedeploy-development-group
#
#permissions:
# contents: read
#
#jobs:
# deploy:
# name: Deploy
# runs-on: ubuntu-latest
# environment: production
#
# steps:
# # (1) 기본 체크아웃
# - name: Checkout
# uses: actions/checkout@v3
#
# # (2) JDK 11 세팅
# - name: Set up JDK 11
# uses: actions/setup-java@v3
# with:
# distribution: 'temurin'
# java-version: '11'
#
# - name: Copy Firebase
# env:
# FIREBASE: ${{ secrets.FIREBASE }}
# FIREBASE_DIR: src/main/resources/key
# FIREBASE_DIR_FILE_NAME: just-firebase-key.json
# run: |
# mkdir -p $FIREBASE_DIR
# echo $FIREBASE | base64 --decode > $FIREBASE_DIR/$FIREBASE_DIR_FILE_NAME
#
# - name: Copy Secret
# env:
# OCCUPY_SECRET: ${{ secrets.OCCUPY_SECRET }}
# OCCUPY_SECRET_DIR: src/main/resources
# OCCUPY_SECRET_DIR_FILE_NAME: application.yml
# run: echo $OCCUPY_SECRET | base64 --decode > $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME
#
#
# - name: Copy Firebase
# env:
# FIREBASE: ${{ secrets.FIREBASE }}
# FIREBASE_DIR: src/main/resources/key
# FIREBASE_DIR_FILE_NAME: just-firebase-key.json
# run: |
# mkdir -p $FIREBASE_DIR
# echo $FIREBASE | base64 --decode > $FIREBASE_DIR/$FIREBASE_DIR_FILE_NAME
#
#
# # (3) Gradle build (Test 제외)
# - name: Build with Gradle
# uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
# with:
# arguments: clean build -x test
#
# # 3. Docker 이미지 빌드
# - name: docker image build
# run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/just .
#
# # 4. DockerHub 로그인
# - name: docker login
# uses: docker/login-action@v2
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_PASSWORD }}
#
# # 5. Docker Hub 이미지 푸시
# - name: docker Hub push
# run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/just
#
## # (4) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용)
## - 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: ${{ env.AWS_REGION }}
###
## # (5) 빌드 결과물을 S3 버킷에 업로드
## - name: Upload to AWS S3
## run: |
## aws deploy push \
## --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
## --ignore-hidden-files \
## --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \
## --source .
##
## # (6) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행
## - name: Deploy to AWS EC2 from S3
## run: |
## aws deploy create-deployment \
## --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
## --deployment-config-name CodeDeployDefault.AllAtOnce \
## --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
## --s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip
36 changes: 36 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,39 @@ jobs: #작업을 정의
with:
gradle-version: 7.2 #그래들 버전 7.2 사용
arguments: clean build -x test

# 3. Docker 이미지 빌드
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/just .

# 4. DockerHub 로그인
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# 5. Docker Hub 이미지 푸시
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/just
run-docker-image-on-ec2:
# build-docker-image (위)과정이 완료되어야 실행됩니다.
needs: build-docker-image
runs-on: self-hosted

steps:
# 1. 최신 이미지를 풀받습니다
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/just

# 2. 기존의 컨테이너를 중지시킵니다
- name: docker stop container
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true

# 3. 최신 이미지를 컨테이너화하여 실행시킵니다
- name: docker run new container
run: sudo docker run --name just --rm -d -p 9000:9000 ${{ secrets.DOCKERHUB_USERNAME }}/just

# 4. 미사용 이미지를 정리합니다
- name: delete old docker image
run: sudo docker system prune -f
Binary file modified .gradle/7.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:11-jdk
ARG JAR_FILE=builds/libs/*.jar
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-jar", "app.jar"]
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-jar", "app.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-04-17T16:22:54+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 11.0.11 (AdoptOpenJDK)"
date = "2024-04-20T15:17:16+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 18.0.2.1 (Oracle Corporation)"
)
@Component
public class MemberMapperImpl implements MemberMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-04-17T16:22:54+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 11.0.11 (AdoptOpenJDK)"
date = "2024-04-20T15:17:16+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 18.0.2.1 (Oracle Corporation)"
)
@Component
public class PostMapperImpl implements PostMapper {
Expand Down
48 changes: 39 additions & 9 deletions src/main/resources/logback-console.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false">
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />

<!-- ELK-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>127.0.0.1:4560</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder"></encoder>
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %clr(%5level) %cyan(%logger) - %msg%n" />
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %5level %logger - %msg%n" />

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>./log/%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>

<root level="info">
<appender-ref ref="LOGSTASH"/>
</root>
<springProfile name="local">
<logger name="com.feelcoding.logbackdemo" level="DEBUG" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</springProfile>
<!-- <springProfile name="dev|stg">-->
<!-- <root level="INFO">-->
<!-- <appender-ref ref="CONSOLE" />-->
<!-- <appender-ref ref="FILE" />-->
<!-- </root>-->
<!-- </springProfile>-->
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</springProfile>
</configuration>