Build and Deploy (3-Tier with Force Push) #104
This file contains hidden or 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: Build and Deploy | |
| on: | |
| # Trigger for the 'deploy_immediate' job | |
| push: | |
| branches: [ "main" ] | |
| # Trigger for the 'deploy_weekly' job | |
| schedule: | |
| - cron: '0 3 * * 0' # 3:00 AM UTC on Sunday | |
| # Allows manual triggering (will run the 'deploy_weekly' logic) | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| #---------------------------------------------------- | |
| # JOB 1: Deploys immediately on every push to main | |
| #---------------------------------------------------- | |
| deploy_immediate: | |
| name: 🚀 Deploy to 'deploy' (Immediate) | |
| # This 'if' condition is key: only run this job for 'push' events | |
| if: github.event_name == 'push' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| strategy: | |
| matrix: | |
| node-version: [16.x] | |
| steps: | |
| - name: Checkout main branch 🛎️ | |
| uses: actions/checkout@v4 | |
| - name: Use Node.js ${{ matrix.node-version }} ⚙️ | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ matrix.node-version }} | |
| - name: Install and Build Frontend 🏗️ | |
| run: | | |
| npm install | |
| npm run build | |
| - name: Commit and Push to 'deploy' branch 🚀 | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| git checkout -B deploy | |
| git add . | |
| git add -f public | |
| git commit -m "${{ github.sha }} [Immediate Deploy]" || echo "No changes to commit" | |
| git push -f origin deploy | |
| #---------------------------------------------------- | |
| # JOB 2: Deploys weekly, only if new commits exist | |
| #---------------------------------------------------- | |
| deploy_weekly: | |
| name: 📅 Deploy to 'deploy-weekly' (Weekly) | |
| # This 'if' condition is key: only run for 'schedule' or 'workflow_dispatch' | |
| if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| strategy: | |
| matrix: | |
| node-version: [16.x] | |
| steps: | |
| - name: Checkout main branch 🛎️ | |
| uses: actions/checkout@v4 | |
| with: | |
| # Fetch all history to compare main and deploy-weekly | |
| fetch-depth: 0 | |
| - name: Check for new commits vs. 'deploy-weekly' 🧐 | |
| id: check_commits | |
| run: | | |
| echo "Checking for new commits..." | |
| MAIN_SHA=$(git rev-parse main) | |
| # Check if origin/deploy-weekly branch exists | |
| if git rev-parse --verify origin/deploy-weekly >/dev/null 2>&1; then | |
| # Get the last commit message from the 'deploy-weekly' branch | |
| DEPLOY_MSG=$(git log origin/deploy-weekly -1 --pretty=%B) | |
| # Extract the SHA (which is the first "word" of the message) | |
| LAST_DEPLOY_SHA=$(echo "$DEPLOY_MSG" | awk '{print $1}') | |
| echo "Latest main SHA: $MAIN_SHA" | |
| echo "Last deployed SHA (from deploy-weekly): $LAST_DEPLOY_SHA" | |
| if [ "$MAIN_SHA" == "$LAST_DEPLOY_SHA" ]; then | |
| echo "No new commits on main since last weekly deploy. Skipping build." | |
| echo "needs_build=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "New commits found. Proceeding with build." | |
| echo "needs_build=true" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| echo "Deploy-weekly branch not found. Proceeding with first build." | |
| echo "needs_build=true" >> $GITHUB_OUTPUT | |
| fi | |
| shell: bash | |
| - name: Use Node.js ${{ matrix.node-version }} ⚙️ | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ matrix.node-version }} | |
| - name: Install and Build Frontend 🏗️ | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| run: | | |
| npm install | |
| npm run build | |
| - name: Commit and Push to 'deploy-weekly' branch 🚀 | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| git checkout -B deploy-weekly | |
| git add . | |
| git add -f public | |
| git commit -m "${{ github.sha }} [Weekly Deploy]" || echo "No changes to commit" | |
| git push -f origin deploy-weekly | |
| deploy_production: | |
| name: 🌍 Deploy to 'deploy-monthly' (Production) | |
| # This 'if' check runs for the monthly cron OR a manual run | |
| if: (github.event_name == 'schedule' && github.event.schedule == '0 4 1 * *') || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: { fetch-depth: 0 } | |
| - name: Check for new commits vs. 'deploy-monthly' | |
| id: check_commits | |
| run: | | |
| MAIN_SHA=$(git rev-parse main) | |
| if git rev-parse --verify origin/deploy-monthly >/dev/null 2>&1; then | |
| DEPLOY_MSG=$(git log origin/deploy-monthly -1 --pretty=%B) | |
| LAST_DEPLOY_SHA=$(echo "$DEPLOY_MSG" | awk '{print $1}') | |
| if [ "$MAIN_SHA" == "$LAST_DEPLOY_SHA" ]; then | |
| echo "No new commits for Production. Skipping." | |
| echo "needs_build=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "New commits found for Production. Building." | |
| echo "needs_build=true" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| echo "First Production build. Proceeding." | |
| echo "needs_build=true" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: actions/setup-node@v4 | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| with: { node-version: 16.x } | |
| - run: | | |
| npm install | |
| npm run build | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| - name: Commit and Push to 'deploy-monthly' | |
| if: steps.check_commits.outputs.needs_build == 'true' | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| git checkout -B deploy-monthly | |
| git add . | |
| git add -f public | |
| git commit -m "${{ github.sha }} [Monthly Production Deploy]" || echo "No changes to commit" | |
| git push -f origin deploy-monthly |