Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] infra: CD 스크립트 작성 #76

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .github/workflows/backend-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: CD using Github self-hosted runner

on:
workflow_dispatch:
push:
branches:
- main
- develop
paths:
- 'backend/**'

env:
ARTIFACT_NAME: review-me-dev
ARTIFACT_DIRECTORY: ./backend/build/libs

jobs:
build:
name: Build Jar file and upload artifact
runs-on: ubuntu-latest

steps:
- name: Checkout to current repository
uses: actions/checkout@v4

- name: Setup JDK Corretto using cached gradle dependencies
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: 17
cache: 'gradle'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: 8.8

- name: Build and test with gradle
run: ./gradlew clean bootJar

- name: Rename artifact file
run: |
mv ${{ env.ARTIFACT_DIRECTORY }}/*.jar ${{ env.ARTIFACT_DIRECTORY }}/${{ env.ARTIFACT_NAME }}.jar

- name: Upload created artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: ${{ env.ARTIFACT_DIRECTORY }}/${{ env.ARTIFACT_NAME }}.jar

deploy:
name: Deploy via self-hosted runner
needs: build
runs-on: self-hosted

steps:
- name: Download uploaded artifact
uses: actions/download-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: ${{ env.ARTIFACT_DIRECTORY }}

- name: Find ${{ env.ARTIFACT_NAME }} process
run: |
echo "Checking processes..."
PID=$(pgrep -f ${{ env.ARTIFACT_NAME }}.jar -d " " || true)
if [ -n "$PID" ]; then
echo "Found processes: $PID"
echo "server_running=true" >> "$GITHUB_ENV"
echo "PID=$PID" >> "$GITHUB_ENV"
else
echo "Process not found!"
echo "server_running=false" >> "$GITHUB_ENV"
fi

- name: Stop server if available (gracefully)
if: env.server_running == 'true'
run: |
echo "Gracefully shutting down process ${{ env.PID }}"
for PID in ${{ env.PID }}; do
sudo kill -15 $PID | true
tail --pid=$PID -f /dev/null | true
done

- name: Start server
run: |
sudo nohup java -jar ${{ env.ARTIFACT_DIRECTORY }}/${{ env.ARTIFACT_NAME }}.jar --server.port=80 &