-
Notifications
You must be signed in to change notification settings - Fork 1
163 lines (148 loc) · 5.4 KB
/
build-test.yaml
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
# Temporary Single EC2 deployment file
name: Build test
on:
push:
branches:
github-action-test
jobs:
deploy:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb
MYSQL_USER: user
MYSQL_PASSWORD: somepassword
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h localhost"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
# 1. GitHub 저장소에서 코드 가져오기
- name: Checkout Repository
uses: actions/checkout@v4
# 2. Java 및 Gradle 설정
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
# 3. Gradle 빌드 수행
- name: Build with Gradle
run: |
chmod +x gradlew
./gradlew build
# Just for build test, not for prod!
env:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/testdb
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: somepassword
SPRING_JPA_HIBERNATE_DDL_AUTO: create-drop
SPRING_PROFILES_ACTIVE: "prod"
# AWS EC2에 SSH 접속하여 NGINX 설치 및 실행
# - name: Install & Start NGINX
# uses: appleboy/[email protected]
# with:
# host: ${{ secrets.EC2_HOST }}
# username: ${{ secrets.EC2_USER }}
# key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
# script: |
# echo "Checking if NGINX is installed..."
# if ! command -v nginx &> /dev/null
# then
# echo "NGINX not found. Installing..."
# sudo apt update && sudo apt install -y nginx
# else
# echo "NGINX is already installed."
# fi
# echo "Starting NGINX..."
# sudo systemctl enable nginx
# sudo systemctl start nginx
# sudo systemctl status nginx
# NGINX 리버스 프록시 설정
# - name: Configure NGINX Reverse Proxy
# uses: appleboy/[email protected]
# with:
# host: ${{ secrets.EC2_HOST }}
# username: ${{ secrets.EC2_USER }}
# key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
# script: |
# echo "Setting up NGINX reverse proxy..."
# sudo bash -c 'cat > /etc/nginx/sites-available/default <<EOF
# server {
# listen 80;
# server_name _;
# location / {
# proxy_pass http://localhost:8080/;
# proxy_set_header Host \$host;
# proxy_set_header X-Real-IP \$remote_addr;
# proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto \$scheme;
# }
# }
# EOF'
# sudo nginx -t && sudo systemctl restart nginx
- name: Create .env file on EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
mkdir ~/app
echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" > ~/app/.env
echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" >> ~/app/.env
echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" >> ~/app/.env
echo "SPRING_PROFILES_ACTIVE=prod" >> ~/app/.env
chmod 600 ~/app/.env
# 빌드된 JAR 파일을 EC2로 업로드
- name: Upload JAR to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "build/libs/*-SNAPSHOT.jar"
target: "~/app/"
- name: Delete latest JAR file
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
cd ~/app
LATEST_JAR=$(ls -t *.jar | head -n 1) || true
echo "Latest JAR file: $LATEST_JAR"
for file in *.jar; do
if [[ "$file" != "$LATEST_JAR" ]]; then
echo "Deleting $file..."
rm -f "$file"
fi
done
continue-on-error: true
- name: Upload Dockerfile and Compose file
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "deploy-Dockerfile, docker-compose.yaml"
target: "~/deploy"
- name: Deploy via SSH
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
cd ~/deploy
echo "Building Docker image..."
docker build -t myapp:1.0 -f deploy-dockerfile .
echo "Starting containers..."
docker-compose up -d myapp