Backend CD #17
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
# This workflow uses actions that are not certified by GitHub. | |
# They are provided by a third-party and are governed by | |
# separate terms of service, privacy policy, and support | |
# documentation. | |
# This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created | |
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle | |
name: CD | |
on: | |
workflow_dispatch: | |
pull_request: | |
types: [ closed ] | |
branches: [ 'main' ] | |
paths: | |
- 'backend/**' | |
jobs: | |
BE_CD: | |
if: ${{ github.event.pull_request.merged == true }} | |
runs-on: self-hosted | |
permissions: | |
contents: read | |
packages: write | |
actions: write | |
outputs: | |
build: ${{ steps.build_result.outputs.result }} | |
deploy: ${{ steps.deploy_result.outputs.result }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-java@v4 | |
with: | |
distribution: 'corretto' | |
java-version: '21' | |
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml | |
settings-path: ${{ github.workspace }} # location for the settings.xml file | |
- name: Setup Gradle | |
run: chmod +x ./backend/gradlew | |
- name: Build with Gradle | |
continue-on-error: true | |
id: build | |
run: | | |
cd backend | |
./gradlew build | |
- name: Save Build Result | |
continue-on-error: true | |
id: build_result | |
run: | | |
echo "result=${{steps.build.outcome}}" >> $GITHUB_OUTPUT | |
- name: Stop server | |
continue-on-error: true | |
id: stop_server | |
run: | | |
PID=$(sudo lsof -t -i:80 || true) | |
if [ -n "$PID" ]; then | |
sudo kill -15 "$PID" | |
echo "Server has been stopped. PID: $PID" | |
else | |
echo "No server is running." | |
fi | |
- name: Deploy | |
continue-on-error: true | |
id: deploy | |
run: | | |
JAR_NAME="develup-0.0.1-SNAPSHOT" | |
JAR_FILE=$(sudo find -name "$JAR_NAME.jar" -print -quit) | |
if [ -n "$JAR_FILE" ]; then | |
sudo nohup java -jar /home/ubuntu/"$JAR_NAME".jar --server.port=80 & | |
echo "Deployed $JAR_NAME" | |
else | |
echo "No JAR file found to deploy." | |
fi | |
- name: Save Deploy Result | |
continue-on-error: true | |
id: deploy_result | |
run: | | |
echo "result=${{steps.deploy.outcome}}" >> $GITHUB_OUTPUT | |
BE_SLACK_MESSAGE: | |
runs-on: ubuntu-latest | |
needs: [ BE_CD ] | |
steps: | |
- name: Slack mention when build success | |
if: ${{needs.BE_CD.outputs.build == 'success' && needs.BE_CD.outputs.deploy == 'success'}} | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "pr 머지", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> pr이 main에 머지되었습니다. 배포가 성공했습니다! \n • PR 링크: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} | |
- name: Slack mention when build fail | |
if: ${{needs.BE_CD.outputs.build == 'failure' && needs.BE_CD.outputs.deploy == 'failure'}} | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "pr 머지", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> pr이 main에 머지되었습니다. 🚨그러나 빌드가 실패합니다!!!🚨 \n • PR 링크: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} | |
- name: Slack mention when build fail | |
if: ${{needs.BE_CD.outputs.build == 'success' && needs.BE_CD.outputs.deploy == 'failure'}} | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ secrets.ISSUE_CHANNEL }} | |
payload: | | |
{ | |
"text": "pr 머지", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<!channel> pr이 main에 머지되었습니다. ⚠️그러나 배포에 실패했습니다!⚠️ \n • PR 링크: <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }} |