-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Temporary Single EC2 deployment file | ||
|
||
name: Build test | ||
|
||
on: | ||
push: | ||
branches: | ||
github-action-test | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
|
||
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 | ||
# 4. 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_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 | ||
# 5. 기존 애플리케이션 종료 | ||
- name: Stop running application on EC2 | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ${{ secrets.EC2_USER }} | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
script: | | ||
echo "Stopping existing application..." | ||
pkill -f myapp.jar || echo "No running process found." | ||
# 6. 빌드된 JAR 파일을 EC2로 업로드 | ||
- name: Upload JAR to EC2 | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ${{ secrets.EC2_USER }} | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
source: "build/libs/*-SNAPSHOT.jar" | ||
target: "/home/${{ secrets.EC2_USER }}/app/" | ||
|
||
# 7. NGINX 리버스 프록시 설정 | ||
- name: Configure NGINX Reverse Proxy | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ${{ secrets.EC2_USER }} | ||
key: ${{ secrets.EC2_SSH_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 | ||
# 8. EC2에서 애플리케이션 실행 | ||
- name: Start application on EC2 | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ${{ secrets.EC2_USER }} | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
script: | | ||
echo "Starting new application..." | ||
nohup java -jar /home/ubuntu/app/*.jar > /home/ubuntu/app/app.log 2>&1 & |