-
Notifications
You must be signed in to change notification settings - Fork 0
166 lines (144 loc) · 4.85 KB
/
Copy pathaws-cicd.yml
File metadata and controls
166 lines (144 loc) · 4.85 KB
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
name: ci/cd action
on:
push:
branches:
- develop
- main
pull_request:
branches:
- develop
- main
jobs:
setup:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
deploy_target: ${{ steps.set-env.outputs.DEPLOY_TARGET }}
steps:
- name: Setup Environment Variables
id: set-env
run: |
if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
echo "DEPLOY_TARGET=prod" >> $GITHUB_OUTPUT
else
echo "DEPLOY_TARGET=dev" >> $GITHUB_OUTPUT
fi
build:
name: build
needs: [ setup ]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
env:
APPLICATION_SECRET_PROPERTIES: ${{ secrets.AWS_APPLICATION_SECRET_PROPERTIES }}
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
- name: Setup Gradle
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25
- name: Create application-secret.properties
run: |
echo "${APPLICATION_SECRET_PROPERTIES}" > ./layer-api/src/main/resources/application-secret.properties
- name: Build layer-api module
run: ./gradlew build -x test
- name: Upload Test Report # 실패시 원인 파악을 하기 위한 단계
uses: actions/upload-artifact@v4
if: always()
with:
name: test-report
path: layer-api/build/reports/tests/test/
- name: Upload built jar
uses: actions/upload-artifact@v4
with:
name: built-jar
path: layer-api/build/libs/*.jar
deploy:
name: Deploy
needs: [ build, setup ]
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
REGISTRY: "docker.io"
NAMESPACE: "layerapp"
IMAGE_NAME: "layer-repo"
APPLICATION_SECRET_PROPERTIES: ${{ secrets.AWS_APPLICATION_SECRET_PROPERTIES }}
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Download built jar
uses: actions/download-artifact@v4
with:
name: built-jar
path: layer-api/build/libs/
- name: Docker Hub Login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Build & Push Docker Image
uses: docker/build-push-action@v4
with:
context: ./layer-api
file: ./layer-api/Dockerfile # Dockerfile 이름 지정
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}:${{ env.DEPLOY_TARGET }}
build-args: |
SPRING_PROFILE=${{ env.DEPLOY_TARGET }}
- name: application-secret.properties 생성
run: |
echo "${{ secrets.APPLICATION_SECRET_PROPERTIES }}" > ./layer-api/infra/${{ env.DEPLOY_TARGET }}/application-secret.properties
- name: 배포 파일 압축
run: |
tar -cvzf layer-api.tar.gz ./layer-api/infra/
- name: 압축된 배포 파일 송신
uses: appleboy/scp-action@master
with:
host: ${{ secrets.AWS_DEV_INSTANCE_HOST }}
username: ubuntu
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
source: "layer-api.tar.gz"
target: "/home/ubuntu"
- name: 서버에서 압축된 파일 추출 및 권한 설정
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_DEV_INSTANCE_HOST }}
username: ubuntu
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
script: |
cd /home/ubuntu
tar -xvzf layer-api.tar.gz
sudo chmod -R 755 /home/ubuntu/layer-api/infra/
sudo chown -R ubuntu:ubuntu /home/ubuntu/layer-api/infra/
- name: 도커 로그인 및 배포스크립트 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_DEV_INSTANCE_HOST }}
username: ubuntu
key: ${{ secrets.AWS_INSTANCE_PEM }}
port: 22
script: |
sudo docker login --username ${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_ACCESS_TOKEN }}
cd /home/ubuntu/layer-api/infra/${{ env.DEPLOY_TARGET }}
chmod 755 ./deploy.sh
./deploy.sh
sudo docker image prune -a -f