Backend None Stop CD #1
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
name: Backend CD | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
paths: | |
- backend/** | |
env: | |
ARTIFACT_NAME: develup-0.0.1-SNAPSHOT | |
ARTIFACT_DIRECTORY: ./backend/build/libs | |
jobs: | |
build: | |
name: 🏗️ Build Jar and Upload Artifact | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: 🏗️ Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'corretto' | |
java-version: 21 | |
- name: 🏗️ Set up Gradle | |
uses: gradle/actions/setup-gradle@v3 | |
- name: 🏗️ Build with Gradle | |
run: | | |
cd backend | |
./gradlew clean bootJar | |
- name: 📤 Upload Artifact File | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.ARTIFACT_NAME }} | |
path: ${{ env.ARTIFACT_DIRECTORY }}/${{ env.ARTIFACT_NAME }}.jar | |
deploy: | |
name: 🚀 Server Deployment | |
needs: build | |
runs-on: self-hosted | |
steps: | |
- name: 📥 Download Artifact File | |
uses: actions/download-artifact@v4 | |
with: | |
name: ${{ env.ARTIFACT_NAME }} | |
path: ${{ env.ARTIFACT_DIRECTORY }} | |
- name: 🔴 Stop Server | |
run: | | |
PID=$(sudo lsof -t -i:80 || true) | |
if [ -n "$PID" ]; then | |
sudo kill -15 $PID | |
tail --pid=$PID -f /dev/null | |
echo "Server has been stopped. PID: $PID" | |
else | |
echo "No server is running." | |
fi | |
- name: 🟢 Start Server | |
run: | | |
sudo nohup java -jar ${{ env.ARTIFACT_DIRECTORY }}/${{ env.ARTIFACT_NAME }}.jar --server.port=80 & | |
slack-notify_success: | |
runs-on: ubuntu-latest | |
needs: | |
- build | |
- deploy | |
if: success() | |
steps: | |
- name: Build and Deploy Success | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "PR Merge", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> \n ✨ PR Merge 완료(Main 브랜치) ✨ \n\t • 🚀 Build Success \n\t • 🟢 Deploy Success \n\t • 🏷️ PR로 이동하기: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} | |
slack-notify_build-fail: | |
runs-on: ubuntu-latest | |
needs: | |
- build | |
if: failure() | |
steps: | |
- name: Build Fail | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "PR Merge", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> \n ✨ PR Merge 완료(Main 브랜치) ✨ \n\t • 🔴 Build Fail \n\t • 🏷️ PR로 이동하기: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} | |
slack-notify_deploy-fail: | |
runs-on: ubuntu-latest | |
needs: | |
- deploy | |
if: failure() | |
steps: | |
- name: Deploy Fail | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "PR Merge", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> \n ✨PR Merge 완료(Main 브랜치)✨ \n\t • 🚀Build Success \n\t • 🔴Deploy Fail \n\t • 🏷️PR로 이동하기: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} |