forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 1
155 lines (141 loc) · 5 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
name: ci-cd
on:
push:
branches: [infra/#8-cicd, release]
permissions:
contents: read
jobs:
build-image:
runs-on: ubuntu-latest
environment: bee-market
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: 'adopt'
# 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 파일 생성 - application.yml
- name: make application.yml
if: |
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_RELEASE }}" > ./application.yml
shell: bash
# gradlew 실행을 위한 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# Clean Gradle Build
- name: Clean with Gradle
run: ./gradlew clean
# Gradle을 이용하여 빌드 수행
- name: Build with Gradle
run: ./gradlew build -x test
# docker build & push to devleop
- name: Docker build & push to release
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
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: bee-market
defaults:
run:
shell: bash
working-directory: ./fe
strategy:
matrix:
node-version: [ 18.x ]
steps:
- uses: actions/checkout@v3
# 환경별 .env 파일 생성
- name: make ".env" file
if: |
contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
run: |
touch ./.env
echo "${{ secrets.DOT_ENV }}" > ./.env
shell: bash
- name: Docker build to release
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
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: bee-market
steps:
- uses: actions/checkout@master
# nginx-release.conf 파일 EC2에 업로드
- name: copy nginx-release.conf file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "./fe/nginx-release.conf"
target: "/home/ubuntu/"
# docker-compose-release.yml 파일 EC2에 업로드
- name: copy docker-compose-release file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "docker-compose-release.yml"
target: "/home/ubuntu/"
# docker-compose-release 실행
- name: Deploy to release
uses: appleboy/ssh-action@master
if: contains(github.ref, 'release')
|| contains(github.ref, 'infra/#8-cicd')
with:
host: ${{ secrets.HOST }} # EC2 인스턴스 퍼블릭 DNS
username: ubuntu
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