Skip to content

feat: 新增檢查後端和前端變更的工作流程,優化部署條件 #390

feat: 新增檢查後端和前端變更的工作流程,優化部署條件

feat: 新增檢查後端和前端變更的工作流程,優化部署條件 #390

Workflow file for this run

name: redive linebot CI
on:
push:
branches:
- main
jobs:
# Check for changes in backend and frontend
check-diff:
runs-on: ubuntu-latest
outputs:
backend_changed: ${{ steps.check_backend.outputs.changed }}
frontend_changed: ${{ steps.check_frontend.outputs.changed }}
steps:
- uses: actions/checkout@v4
- name: Check if backend has changes
id: check_backend
run: |
if git diff --quiet HEAD^ HEAD -- ./app; then
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "changed=true" >> $GITHUB_OUTPUT
fi
- name: Check if frontend has changes
id: check_frontend
run: |
if git diff --quiet HEAD^ HEAD -- ./frontend; then
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "changed=true" >> $GITHUB_OUTPUT
fi
# Backend build and push job
backend:
if: needs.check-diff.outputs.backend_changed == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Backend build and push Docker image
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75
with:
context: ./app
push: true
tags: hanshino/redive_backend:latest
cache-from: type=registry,ref=hanshino/redive_backend:buildcache
cache-to: type=registry,ref=hanshino/redive_backend:buildcache,mode=max
# Frontend build and push job
frontend:
if: needs.check-diff.outputs.frontend_changed == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Frontend build and push Docker image
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75
with:
context: ./frontend
push: true
tags: hanshino/redive_frontend:latest
cache-from: type=registry,ref=hanshino/redive_frontend:buildcache
cache-to: type=registry,ref=hanshino/redive_frontend:buildcache,mode=max
build-args: |
REACT_APP_GOOGLE_ANALYTICS_ID=${{ secrets.GOOGLE_ANALYTICS_ID }}
# Deployment job
deploy:
runs-on: ubuntu-latest
needs: [backend, frontend] # Ensure that both backend and frontend jobs are finished before deploying
if: needs.backend.result == 'success' || needs.frontend.result == 'success'
steps:
- name: Deploy Redive linebot
uses: appleboy/ssh-action@v1.1.0
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ${{ secrets.DEPLOY_PATH }}
git pull
bash scripts/run-tasks.sh traefik-run
- name: Discord notification
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: "{{ github.repository }} deployment complete."