|
| 1 | +name: Build and Deploy (3-Tier with Force Push) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["main"] |
| 6 | + |
| 7 | + schedule: |
| 8 | + # Weekly: Sunday at 3:00 AM UTC (for Beta) |
| 9 | + - cron: "0 3 * * 0" |
| 10 | + # Monthly: 1st of the month at 4:00 AM UTC (for Production) |
| 11 | + - cron: "0 4 1 * *" |
| 12 | + |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + #---------------------------------------------------- |
| 20 | + # JOB 1: DEVELOPMENT (Always runs on push) |
| 21 | + #---------------------------------------------------- |
| 22 | + deploy_dev: |
| 23 | + name: 🚀 Deploy to 'deploy-dev' (Dev) |
| 24 | + if: github.event_name == 'push' |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - uses: actions/checkout@v4 |
| 28 | + - uses: actions/setup-node@v4 |
| 29 | + with: { node-version: 16.x } |
| 30 | + - run: | |
| 31 | + npm install |
| 32 | + npm run build |
| 33 | + - name: Commit and Push to 'deploy-dev' |
| 34 | + run: | |
| 35 | + git config --global user.name 'github-actions[bot]' |
| 36 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 37 | + git checkout -B deploy-dev |
| 38 | + git add . |
| 39 | + git add -f public |
| 40 | + git commit -m "${{ github.sha }} [Dev Deploy]" || echo "No changes to commit" |
| 41 | + git push -f origin deploy-dev |
| 42 | +
|
| 43 | + #---------------------------------------------------- |
| 44 | + # JOB 2: BETA (Runs weekly OR on "Immediate:" push) |
| 45 | + #---------------------------------------------------- |
| 46 | + deploy_beta: |
| 47 | + name: spoilers 📅 Deploy to 'deploy-beta' (Beta) |
| 48 | + # 1. CHANGED: Now also triggers on 'push' to check the commit message |
| 49 | + if: (github.event_name == 'schedule' && github.event.schedule == '0 3 * * 0') || github.event_name == 'push' |
| 50 | + runs-on: ubuntu-latest |
| 51 | + steps: |
| 52 | + - uses: actions/checkout@v4 |
| 53 | + with: { fetch-depth: 0 } |
| 54 | + - name: Check Beta deploy logic 🧐 |
| 55 | + id: check_commits |
| 56 | + run: | |
| 57 | + echo "needs_build=false" >> $GITHUB_OUTPUT # Default to false |
| 58 | + COMMIT_MSG="${{ github.event.head_commit.message }}" |
| 59 | +
|
| 60 | + # 2. NEW LOGIC: Check for force command first |
| 61 | + if echo "$COMMIT_MSG" | grep -qi "^Immediate:"; then |
| 62 | + echo "✅ Force deploy triggered by commit message." |
| 63 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + # 3. OLD LOGIC: Check for schedule |
| 66 | + elif [ "${{ github.event_name }}" == "schedule" ]; then |
| 67 | + echo "Running on schedule, checking for new commits..." |
| 68 | + MAIN_SHA=$(git rev-parse main) |
| 69 | + if git rev-parse --verify origin/deploy-beta >/dev/null 2>&1; then |
| 70 | + DEPLOY_MSG=$(git log origin/deploy-beta -1 --pretty=%B) |
| 71 | + LAST_DEPLOY_SHA=$(echo "$DEPLOY_MSG" | awk '{print $1}') |
| 72 | + if [ "$MAIN_SHA" == "$LAST_DEPLOY_SHA" ]; then |
| 73 | + echo "No new commits for Beta. Skipping." |
| 74 | + else |
| 75 | + echo "New commits found for Beta. Building." |
| 76 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 77 | + fi |
| 78 | + else |
| 79 | + echo "First Beta build. Proceeding." |
| 80 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 81 | + fi |
| 82 | + else |
| 83 | + echo "Regular push, no force command. Skipping Beta." |
| 84 | + fi |
| 85 | + shell: bash |
| 86 | + |
| 87 | + - uses: actions/setup-node@v4 |
| 88 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 89 | + with: { node-version: 16.x } |
| 90 | + - run: | |
| 91 | + npm install |
| 92 | + npm run build |
| 93 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 94 | + - name: Commit and Push to 'deploy-beta' |
| 95 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 96 | + run: | |
| 97 | + git config --global user.name 'github-actions[bot]' |
| 98 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 99 | + git checkout -B deploy-beta |
| 100 | + git add . |
| 101 | + git add -f public |
| 102 | + git commit -m "${{ github.sha }} [Weekly Beta Deploy]" || echo "No changes to commit" |
| 103 | + git push -f origin deploy-beta |
| 104 | +
|
| 105 | + #---------------------------------------------------- |
| 106 | + # JOB 3: PRODUCTION (Runs monthly, on dispatch, OR on "Immediate:" push) |
| 107 | + #---------------------------------------------------- |
| 108 | + deploy_production: |
| 109 | + name: 🌍 Deploy to 'deploy-monthly' (Production) |
| 110 | + # 4. CHANGED: Also triggers on 'push' to check commit message |
| 111 | + if: (github.event_name == 'schedule' && github.event.schedule == '0 4 1 * *') || github.event_name == 'workflow_dispatch' || github.event_name == 'push' |
| 112 | + runs-on: ubuntu-latest |
| 113 | + steps: |
| 114 | + - uses: actions/checkout@v4 |
| 115 | + with: { fetch-depth: 0 } |
| 116 | + - name: Check Production deploy logic 🧐 |
| 117 | + id: check_commits |
| 118 | + run: | |
| 119 | + echo "needs_build=false" >> $GITHUB_OUTPUT # Default to false |
| 120 | + COMMIT_MSG="${{ github.event.head_commit.message }}" |
| 121 | +
|
| 122 | + # 5. NEW LOGIC: Check for force command first |
| 123 | + if echo "$COMMIT_MSG" | grep -qi "^Immediate:"; then |
| 124 | + echo "✅ Force deploy triggered by commit message." |
| 125 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 126 | +
|
| 127 | + # 6. OLD LOGIC: Check for schedule or dispatch |
| 128 | + elif [ "${{ github.event_name }}" == "schedule" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 129 | + echo "Running on schedule/dispatch, checking for new commits..." |
| 130 | + MAIN_SHA=$(git rev-parse main) |
| 131 | + if git rev-parse --verify origin/deploy-monthly >/dev/null 2>&1; then |
| 132 | + DEPLOY_MSG=$(git log origin/deploy-monthly -1 --pretty=%B) |
| 133 | + LAST_DEPLOY_SHA=$(echo "$DEPLOY_MSG" | awk '{print $1}') |
| 134 | + if [ "$MAIN_SHA" == "$LAST_DEPLOY_SHA" ]; then |
| 135 | + echo "No new commits for Production. Skipping." |
| 136 | + else |
| 137 | + echo "New commits found for Production. Building." |
| 138 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 139 | + fi |
| 140 | + else |
| 141 | + echo "First Production build. Proceeding." |
| 142 | + echo "needs_build=true" >> $GITHUB_OUTPUT |
| 143 | + fi |
| 144 | + else |
| 145 | + echo "Regular push, no force command. Skipping Production." |
| 146 | + fi |
| 147 | + shell: bash |
| 148 | + |
| 149 | + - uses: actions/setup-node@v4 |
| 150 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 151 | + with: { node-version: 16.x } |
| 152 | + - run: | |
| 153 | + npm install |
| 154 | + npm run build |
| 155 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 156 | + - name: Commit and Push to 'deploy-monthly' |
| 157 | + if: steps.check_commits.outputs.needs_build == 'true' |
| 158 | + run: | |
| 159 | + git config --global user.name 'github-actions[bot]' |
| 160 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 161 | + git checkout -B deploy-monthly |
| 162 | + git add . |
| 163 | + git add -f public |
| 164 | + git commit -m "${{ github.sha }} [Monthly Production Deploy]" || echo "No changes to commit" |
| 165 | + git push -f origin deploy-monthly |
0 commit comments