-
Notifications
You must be signed in to change notification settings - Fork 0
106 lines (91 loc) · 3.15 KB
/
backend-cicd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Backend CI/CD
on:
push:
branches: [ "backend_main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
env:
working-directory: ./backend
APPLICATION: ${{ secrets.APPLICATION }}
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
steps:
# 소스 코드 체크아웃
- uses: actions/checkout@v2
# JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
# Gradle 패키지 캐시
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# 설정 파일 생성
- run: |
mkdir ./backend/src/main/resources
cd ./backend/src/main/resources
touch ./application.yml
echo "${{env.APPLICATION}}" > ./application.yml
touch ./firebase-key.json
echo "${{env.GOOGLE_APPLICATION_CREDENTIALS}}" | base64 --decode > ./firebase-key.json
# 설정 파일을 작업공간에 저장
- uses: actions/upload-artifact@v2
with:
name: application.yml
path: ./backend/src/main/resources/application.yml
- uses: actions/upload-artifact@v2
with:
name: firebase-key.json
path: ./backend/src/main/resources/firebase-key.json
# gradlew 권한 설정
- name: Grant execute permission for gradlew
run: chmod +x gradlew
working-directory: ${{ env.working-directory }}
# Gradle로 빌드
- name: Build with Gradle
run: ./gradlew build
working-directory: ${{ env.working-directory }}
# Gradle 캐시 정리
- name: Cleanup Gradle Cache
if: ${{ always() }}
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
# Docker 이미지 빌드 및 푸시
- name: Docker build
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build --no-cache -t ${{ secrets.DOCKER_USERNAME }}/moing_admin:green .
docker build --no-cache -t ${{ secrets.DOCKER_USERNAME }}/moing_admin:blue .
docker push ${{ secrets.DOCKER_USERNAME }}/moing_admin:green
docker push ${{ secrets.DOCKER_USERNAME }}/moing_admin:blue
# EC2로 deploy.sh 전송
- name: Deploy deploy.sh to EC2
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_SERVER_HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
source: "./deploy.sh"
target: "/home/ec2-user/"
# 배포 스크립트 실행
- name: Deploy on EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_SERVER_HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
chmod +x /home/ec2-user/deploy.sh
/home/ec2-user/deploy.sh