-
Notifications
You must be signed in to change notification settings - Fork 3
175 lines (151 loc) · 5.59 KB
/
gradle-build.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
name: CI with Gradle
on:
pull_request:
branches: [ dev ]
push:
branches: [ dev ]
permissions:
contents: read
env:
ECR_NAMESPACE: sasaping
STACK_VERSION: 8.6.0
CLUSTER_NAME: docker-cluster
LICENSE: basic
ES_PORT: 9200
KIBANA_PORT: 5601
MEM_LIMIT: 1073741824
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions: write-all
strategy:
matrix:
service: [ eureka, user, auth, gateway, notification, order, payment, product, slack, promotion, search ]
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build with Gradle
run: ./gradlew clean build -p ./service/${{ matrix.service }}/server -D spring.profiles.active=prod -x test
- name: Check if JAR file exists
run: |
if [ ! -f ./service/${{ matrix.service }}/server/build/libs/*SNAPSHOT.jar ]; then
echo "JAR file not found!"
exit 1
fi
- name: Upload build artifact
uses: actions/upload-artifact@v3
with:
name: '${{ matrix.service }}-artifact'
path: './service/${{ matrix.service }}/server/build/libs/*SNAPSHOT.jar'
Docker:
name: Build docker image and Push to registry
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' # Push 이벤트일 때만 실행
steps:
- name: Checkout
uses: actions/checkout@v4
# 모든 아티팩트를 다운로드합니다.
- name: Download all artifacts
uses: actions/download-artifact@v3
with:
path: './service'
# AWS ECR 에 이미지 업로드 권한을 얻기 위해 인증을 진행합니다.
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Grant execute permission for dockerTagAndPush
run: chmod +x ./dockerTagAndPush.sh
# docker compose 를 이용해서 여러 이미지를 모두 빌드하고, 별도의 script를 사용해서 이미지를 push 합니다.
- name: Build, Tag and Push docker image to AWS ECR
run: |
docker compose build
./dockerTagAndPush.sh
env:
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
Deploy:
name: Deploy
needs: Docker
runs-on: ubuntu-latest
if: github.event_name == 'push' # Push 이벤트일 때만 실행
steps:
- uses: actions/checkout@v4
# docker compose로 container를 실행하기 위해 docker-compose.yml 을 EC2로 복사합니다.
- name: Copy Docker compose file to EC2
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
source: "docker-compose.yml"
target: "/home/ubuntu" # target 은 디렉토리임. target directory 아래에 같은 이름의 파일로 옮겨진다.
# 사용 중인 포트를 점검하고 종료하는 script를 실행합니다.
- name: Upload stop-used-port script to EC2
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
source: "stop-used-port.sh"
target: "/home/ubuntu"
# ssh를 통해 EC2에 접속하고 docker container를 재시작합니다.
- name: Deploy to EC2
uses: appleboy/ssh-action@v1.0.3
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ELASTIC_PASSWORD: ${{secrets.ELASTIC_PASSWORD}}
KIBANA_PASSWORD: ${{secrets.KIBANA_PASSWORD}}
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
port: 22
envs: |
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_REGION,
ECR_REGISTRY,
ECR_NAMESPACE,
ELASTIC_PASSWORD,
KIBANA_PASSWORD,
STACK_VERSION,
CLUSTER_NAME,
LICENSE,
ES_PORT,
KIBANA_PORT,
MEM_LIMIT
script: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}
docker ps --format "{{.ID}} {{.Image}}" | grep -vE "docker.elastic.co/elasticsearch/elasticsearch:8.6.0|wurstmeister/zookeeper:latest" | awk '{print $1}' | xargs -r docker stop
docker container prune -f
docker rmi $(docker images "${ECR_REGISTRY}/${ECR_NAMESPACE}/*" -q)
sudo chmod +x /home/ubuntu/stop-used-port.sh
/home/ubuntu/stop-used-port.sh
docker compose pull
docker compose up -d --no-build