Skip to content

Commit

Permalink
[chore] : CI/CD 구축 (#155)
Browse files Browse the repository at this point in the history
* [chore] : application 환경별 분리 및 firebase gitignore 추가

* [chore] : firebase_account로 파일명 수정 반영

* [chore] : CD 위한 dockerfile 작성

* [chore] : CI 시 firebase_account.json secret에서 가져오도록 수정

* [chore] : CD를 위한 cd.yml 추가

* [refactor] : 배포 스크립트 내 타임존 설정에 따른 Application 내 중복 코드 제거
  • Loading branch information
hyun2371 authored Apr 10, 2024
1 parent 5bb10f7 commit 02005bf
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 16 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: CD Backend

on:
push:
branches: [ "main", "dev" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

# 자바 버전 설정
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# yml 파일 생성
- name: Generate application-core.yml
run: |
mkdir -p ./core/src/main/resources
echo "${{ secrets.APPLICATION_CORE }}" | base64 -d > ./core/src/main/resources/application-core.yml
# firebase json 파일 생성
- name : Generate firebase_account.json
run: |
mkdir -p ./core/src/main/resources/firebase
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/main/resources/firebase/firebase_account.json
mkdir -p ./core/src/test/resources/firebase
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/test/resources/firebase/firebase_account.json
# gradle 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

# 빌드 시 캐시 적용
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# 빌드
- name: Build with Gradle
run: ./gradlew build -x test


# 도커 허브 로그인
- name: Docker Hub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}


# 도커 이미지 빌드 및 푸시
- name: docker image build and push
run: |
docker build -f Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} .
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }}
# 서버 백그라운드 실행
- name: pull image and run container
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
password: ${{ secrets.EC2_PASSWORD }}
port: ${{ secrets.EC2_PORT }}
script: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }}
docker stop ${{ secrets.DOCKERHUB_APP_NAME }}
docker rm ${{ secrets.DOCKERHUB_APP_NAME }}
docker run -d \
-p 8080:8080 \
-e TZ=Asia/Seoul \
--name ${{ secrets.DOCKERHUB_APP_NAME }} \
${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }}
docker system prune -f
24 changes: 15 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: CI Backend

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "main", "dev" ]
branches: ["main", "dev"]

permissions:
contents: read
Expand All @@ -28,6 +26,19 @@ jobs:
java-version: '17'
distribution: 'temurin'

# firebase json 파일 생성
- name : Generate firebase_account.json
run: |
mkdir -p ./core/src/main/resources/firebase
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/main/resources/firebase/firebase_account.json
mkdir -p ./core/src/test/resources/firebase
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/test/resources/firebase/firebase_account.json
# gradle 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

# 빌드 시 캐시 적용
- name: Gradle Caching
uses: actions/cache@v3
Expand All @@ -39,11 +50,6 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-
# gradle 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

# 빌드
- name: Build with Gradle
run: ./gradlew build
Expand All @@ -60,4 +66,4 @@ jobs:
uses: mikepenz/action-junit-report@v3
if: always()
with:
report_paths: '**/build/test-results/test/TEST-*.html'
report_paths: '**/build/test-results/test/TEST-*.html'
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ out/
### VS Code ###
.vscode/
.DS_Store
core/src/main/resources/application.yml
core/src/main/resources/application-core-dev.yml
core/src/main/resources/application-core-prod.yml
application-core.yml
/api/src/main/resources/application.yml
/core/src/main/generated/
/core/src/main/resources/firebase/firebase_account.json
/core/src/test/resources/firebase/firebase_account.json
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:17-jdk
ARG JAR_FILE=api/build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
5 changes: 0 additions & 5 deletions api/src/main/java/dev/handsup/HandsUpApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
@SpringBootApplication
public class HandsUpApplication {

@PostConstruct
public void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}

public static void main(String[] args) {
// 타 모듈 yml 가져오기
System.setProperty("spring.config.name", "application, application-core");
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ cloud:
auto: false

fcm:
certification: firebase/firebase_service_account.json
certification: firebase/firebase_account.json

0 comments on commit 02005bf

Please sign in to comment.