forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 1
213 lines (198 loc) · 7.53 KB
/
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
name: ci-cd
on:
push:
branches: [infra/#8-cicd, dev, release]
permissions:
contents: read
jobs:
build-image:
runs-on: ubuntu-latest
environment: dev
defaults:
run:
shell: bash
working-directory: ./be/carrot
steps:
- uses: actions/checkout@v3
## JDK Setting
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'termurin'
# gradle caching - 빌드 시간 향상
- name: Gradle Caching
uses: actions/cache@v3
with:
# cache directory setting
path:
~/.gradle/caches
~/.gradle/wrapper
# separate cache key setting
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
# 이전에 생성된 캐시 복원에 사용할 키 지정
# 캐시가 없거나 만료되었을 때, 이 키를 기반으로 이전에 생성된 캐시를 찾아 복원
restore-keys: |
${{ runner.os }}-gradle-
# 환경별 yml 파일 생성(1) - application.yml
- name: make application.yml
if: |
contains(github.ref, 'dev') ||
contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
run: |
mkdir ./src/main/resources
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.APPLICATION_COMMON }}" > ./applicaiton.yml
shell: bash
# 환경별 yml 파일 생성(2) - dev
- name: make application-dev.yml
if: contatins(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
run: |
cd ./src/main/resources
touch ./application-dev.yml
echo "${{ secrets.APPLICATION_DEV }}" > ./application-dev.yml
shell: bash
# 환경별 yml 파일 생성(3) - release.yml
- name: make application-release.yml
if: contains(github.ref, 'release')
run:
cd ./src/main/resources
touch ./application-release.yml
echo "${{ secrets.APPLICATION_RELEASE }}" > ./application-release.yml
shell: bash
# gradlew 실행을 위한 실행 권한 부여
- name: Grant execute permission for gradlew
run: sudo chmod +x ./gradlew
# Gradle을 이용하여 빌드 수행
- name: Build with Gradle
run: sudo ./gradlew build -x test
# docker build & push to devleop
- name: Docker build & push to dev
if: contains(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-dev -t ${{ secrets.DOCKER_USERNAME }}/bee_market_app_dev .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_app_dev
# docker build & push to release
- name: Docker build & push to release
if: contains(github.ref, 'release') ||
contains(github.ref, 'release')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release
build-react-image:
runs-on: ubuntu-latest
environment: dev
defaults:
run:
shell: bash
working-directory: ./fe
strategy:
matrix:
node-version: [ 18.x ]
steps:
- uses: actions/checkout@v3
- name: Docker build to dev
if: contains(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p & {{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-dev -t ${{ secrets.DOCKER_USERNAME }}/bee_market_web_dev .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_web_dev
- name: Docker build to release
if: contains(github.ref, 'release')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release
cd-pipeline:
needs:
- build-image
- build-react-image
name: continuos deploy
runs-on: ubuntu-latest
environment: dev
steps:
- uses: actions/checkout@master
# nginx-dev-conf 파일 EC2에 업로드
- name: copy nginx-dev.conf file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "./nginx/nginx-dev.conf"
target: "/home/ec2-user/"
# nginx-release.conf 파일 EC2에 업로드
- name: copy nginx-release.conf file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "./nginx/nginx-release.conf"
target: "/home/ec2-user/"
# docker-compose-dev.yml 파일 EC2에 업로드
- name: copy docker-compose-dev file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "docker-compose-dev.yml"
target: "/home/ec2-user/"
# docker-compose-release.yml 파일 EC2에 업로드
- name: copy docker-compose-release file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "docker-compose-release.yml"
target: "/home/ec2-user/"
# docer-compose-dev 실행
- name: Deploy to dev
uses: appleboy/ssh-action@master
if: contains(github.ref, 'dev') ||
contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }} # EC2 인스턴스 퍼블릭 DNS
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }} # pem Key
port: 22
script: |
sudo chmod 666 /var/run/docker.sock
docker-compose -f docker-compose-dev.yml down -v
docker-compose -f docker-compose-dev.yml pull
docker-compose -f dokcer-compose-dev.yml up-d
docker image prune -f
# docker-compose-release 실행
- name: Deploy to release
uses: appleboy/ssh-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }} # EC2 인스턴스 퍼블릭 DNS
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }} # pem 키
port: 22
script: |
sudo chmod 666 /var/run/docker.sock
docker-compose -f docker-compose-release.yml down -v
docker-compose -f docker-compose-release.yml pull
docker-compose -f docker-compose-release.yml up -d
docker image prune -f