-
Notifications
You must be signed in to change notification settings - Fork 1
171 lines (149 loc) · 5.64 KB
/
dev-aws-CI-CD.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
name: dev - CI/CD to Amazon ECS
on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]
permissions:
contents: read
checks: write
issues: write
jobs:
build:
runs-on: ubuntu-latest
environment: dev
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant Execute Permission For Gradlew
run: chmod +x gradlew
- name: Build With Gradle
run: ./gradlew build -x test --info
- name: List All Files for Debugging
run: ls -laR # 모든 디렉토리의 파일 나열
- name: Upload JAR Artifact
uses: actions/upload-artifact@v3
with:
name: jar-file
path: build/libs/clothstar-0.0.1-SNAPSHOT.jar
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: dev
if: github.event_name == 'push'
needs: build
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download JAR Artifact
uses: actions/download-artifact@v3
with:
name: jar-file
path: build/libs/
- name: List Files for Debugging
run: ls -la ./ # 현재 디렉토리의 모든 파일 나열
- name: Show docker-compose.yml Content
run: cat docker-compose.yml # docker-compose.yml 파일 내용 확인
- name: Send docker-compose.yml and nginx.conf to Home Directory
uses: appleboy/scp-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
source: "./docker-compose.yml,./nginx/conf.d/nginx.conf"
target: "/home/ubuntu/"
strip_components: 3 # nginx.conf 경로 구성 요소를 제거하여 파일만 전송
# Move nginx.conf from Home Directory to /etc/nginx/conf.d
- name: Move nginx.conf to /etc/nginx/conf.d
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
script: |
sudo mkdir -p /etc/nginx/conf.d/
if [ -f /home/ubuntu/nginx.conf ]; then
sudo mv /home/ubuntu/nginx.conf /etc/nginx/conf.d/nginx.conf
else
echo "nginx.conf not found in /home/ubuntu/"
exit 1
fi
## Docker login
- name: Docker Login
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
## Nginx 도커 이미지 빌드 후 도커허브에 push하기
- name: Docker build & Push for Nginx
run: |
docker build -f Dockerfile-nginx -t ${{ secrets.DOCKER_REPOSITORY_NGINX }} .
docker push ${{ secrets.DOCKER_REPOSITORY_NGINX }}
- name: Check JAR file existence
run: ls -la build/libs/
## Spring Boot 도커 이미지 빌드 후 도커허브에 push하기
- name: Docker build & Push for Spring Boot
run: |
docker build -t ${{ secrets.DOCKER_REPOSITORY }} .
docker push ${{ secrets.DOCKER_REPOSITORY }}
- name: Check Files on AWS Ubuntu
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
script: |
ls -la /home/ubuntu/ # AWS 서버에서 파일이 있는지 확인
cat /home/ubuntu/docker-compose.yml # AWS 서버에서 파일 내용 확인
# SSH로 서버에 연결 및 Docker compose 실행
- name: Deploy and Run Docker Compose on Server
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
script: | # SSH 연결 후 실행할 명령어들
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
export DOCKER_REPOSITORY=${{ secrets.DOCKER_REPOSITORY }}
export DOCKER_REPOSITORY_NGINX=${{ secrets.DOCKER_REPOSITORY_NGINX }}
# Pull 최신 이미지
docker-compose -f /home/ubuntu/docker-compose.yml pull
# 새 컨테이너 실행
docker-compose -f /home/ubuntu/docker-compose.yml up -d --build
discord-notify:
name: Discord Notify
runs-on: ubuntu-latest
environment: dev
needs: [ build, deploy ]
if: always()
steps:
- name: Send Discord Notification
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK_URL }}
status: ${{ job.status }}
title: "CI/CD Pipeline Status"
description: |
${{ format(
'The CI/CD pipeline has completed.\n- **Build Job Status**: {0}\n- **Deploy Job Status**: {1}\n- **Branch**: {2}\n- **Commit**: {3}\n- **Author**: {4}',
needs.build.result,
needs.deploy.result,
github.ref,
github.sha,
github.actor
) }}
url: "https://github.com/sarisia/actions-status-discord"
username: GitHub Actions Bot