|
| 1 | +name: Release Calendar |
| 2 | + |
| 3 | +# Goal: automatically create release pull requests when the clock hits 17:00 UTC (5:00 PM). |
| 4 | +# For reference, 17:00 UTC corresponds to: |
| 5 | +# • 10:30 PM IST (India) |
| 6 | +# • 10:00 AM PT (San Francisco) |
| 7 | +# • 6:00 PM CET / 7:00 PM CEST (Paris) |
| 8 | +# • 12:00 PM EST / 1:00 PM EDT (New York) |
| 9 | +# • 6:00 PM CET / 7:00 PM CEST (Warsaw) |
| 10 | +# Adjust locally if daylight saving time is in effect. |
| 11 | +# |
| 12 | +# Testing: This workflow automatically runs when merged to dev (when the workflow file itself |
| 13 | +# is modified), allowing you to test changes immediately. You can also manually trigger it via |
| 14 | +# workflow_dispatch and choose which job(s) to run (staging or production). |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_dispatch: |
| 18 | + inputs: |
| 19 | + job_to_run: |
| 20 | + description: "Which job to run (staging or production)" |
| 21 | + required: false |
| 22 | + type: choice |
| 23 | + options: |
| 24 | + - staging |
| 25 | + - production |
| 26 | + default: staging |
| 27 | + push: |
| 28 | + branches: |
| 29 | + - dev |
| 30 | + paths: |
| 31 | + - ".github/workflows/release-calendar.yml" |
| 32 | + schedule: |
| 33 | + # Friday 17:00 UTC (see timezone conversions above) to prepare the weekend staging PR. |
| 34 | + - cron: "0 17 * * 5" |
| 35 | + # Sunday 17:00 UTC (same times as above) to prepare the production release PR. |
| 36 | + - cron: "0 17 * * 0" |
| 37 | + |
| 38 | +permissions: |
| 39 | + contents: read |
| 40 | + pull-requests: write |
| 41 | + issues: write # Required for creating labels |
| 42 | + |
| 43 | +jobs: |
| 44 | + release_to_staging: |
| 45 | + name: Create dev to staging release PR |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - name: Guard Friday schedule |
| 49 | + id: guard_schedule |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + set -euo pipefail |
| 53 | +
|
| 54 | + # Allow push events (when workflow file is modified) to run |
| 55 | + if [ "${{ github.event_name }}" == "push" ]; then |
| 56 | + if [ "${{ github.ref_name }}" == "dev" ]; then |
| 57 | + echo "Triggered by push event on dev. Running staging job." |
| 58 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 59 | + else |
| 60 | + echo "Triggered by push event on non-dev. Skipping staging job." |
| 61 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 62 | + fi |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | +
|
| 66 | + # Allow workflow_dispatch based on input |
| 67 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 68 | + JOB_TO_RUN="${{ inputs.job_to_run }}" |
| 69 | + if [ "$JOB_TO_RUN" == "staging" ]; then |
| 70 | + echo "Manual trigger: running staging job (job_to_run=${JOB_TO_RUN})" |
| 71 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 72 | + else |
| 73 | + echo "Manual trigger: skipping staging job (job_to_run=${JOB_TO_RUN})" |
| 74 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 75 | + fi |
| 76 | + exit 0 |
| 77 | + fi |
| 78 | +
|
| 79 | + # For schedule events, check day of week |
| 80 | + DOW=$(date -u +%u) |
| 81 | + if [ "$DOW" != "5" ]; then |
| 82 | + echo "Not Friday in UTC (current day-of-week: $DOW). Exiting job early." |
| 83 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 84 | + exit 0 |
| 85 | + fi |
| 86 | +
|
| 87 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 88 | +
|
| 89 | + - name: Check out repository |
| 90 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' }} |
| 91 | + uses: actions/checkout@v4 |
| 92 | + with: |
| 93 | + fetch-depth: 0 |
| 94 | + |
| 95 | + - name: Check for existing dev to staging PR |
| 96 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' }} |
| 97 | + id: check_dev_staging |
| 98 | + env: |
| 99 | + GH_TOKEN: ${{ github.token }} |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + PR_DATE=$(date +%Y-%m-%d) |
| 104 | + echo "date=${PR_DATE}" >> "$GITHUB_OUTPUT" |
| 105 | +
|
| 106 | + echo "Checking for existing pull requests from dev to staging..." |
| 107 | + EXISTING_PR=$(gh pr list --base staging --head dev --state open --limit 1 --json number --jq '.[0].number // ""') |
| 108 | + echo "existing_pr=${EXISTING_PR}" >> "$GITHUB_OUTPUT" |
| 109 | +
|
| 110 | + if [ -n "$EXISTING_PR" ]; then |
| 111 | + echo "Found existing release PR: #${EXISTING_PR}. Skipping creation." |
| 112 | + else |
| 113 | + echo "No existing release PR found. Proceeding to create a new one." |
| 114 | + fi |
| 115 | +
|
| 116 | + - name: Log existing PR |
| 117 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.check_dev_staging.outputs.existing_pr != '' }} |
| 118 | + run: | |
| 119 | + echo "Release PR already exists: #${{ steps.check_dev_staging.outputs.existing_pr }}" |
| 120 | +
|
| 121 | + - name: Ensure release labels exist |
| 122 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.check_dev_staging.outputs.existing_pr == '' }} |
| 123 | + env: |
| 124 | + GH_TOKEN: ${{ github.token }} |
| 125 | + shell: bash |
| 126 | + run: | |
| 127 | + set -euo pipefail |
| 128 | +
|
| 129 | + for LABEL in release automated staging; do |
| 130 | + if ! gh label list --json name --jq '.[].name' | grep -q "^${LABEL}$"; then |
| 131 | + echo "Creating missing label: ${LABEL}" |
| 132 | + gh label create "${LABEL}" --color BFD4F2 |
| 133 | + else |
| 134 | + echo "Label ${LABEL} already exists." |
| 135 | + fi |
| 136 | + done |
| 137 | +
|
| 138 | + - name: Create dev to staging release PR |
| 139 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.check_dev_staging.outputs.existing_pr == '' }} |
| 140 | + env: |
| 141 | + GH_TOKEN: ${{ github.token }} |
| 142 | + PR_DATE: ${{ steps.check_dev_staging.outputs.date }} |
| 143 | + shell: bash |
| 144 | + run: | |
| 145 | + set -euo pipefail |
| 146 | +
|
| 147 | + python <<'PY' |
| 148 | + import pathlib |
| 149 | + import textwrap |
| 150 | +
|
| 151 | + pathlib.Path("pr_body.md").write_text(textwrap.dedent("""\ |
| 152 | + ## 🚀 Weekly Release to Staging |
| 153 | +
|
| 154 | + This automated PR promotes all changes from `dev` to `staging` for testing. |
| 155 | +
|
| 156 | + ### What's Included |
| 157 | + All commits merged to `dev` since the last staging release. |
| 158 | +
|
| 159 | + ### Review Checklist |
| 160 | + - [ ] All CI checks pass |
| 161 | + - [ ] Code review completed |
| 162 | + - [ ] QA team notified |
| 163 | + - [ ] Ready to merge to staging environment |
| 164 | +
|
| 165 | + ### Next Steps |
| 166 | + After merging, the staging environment will be updated. A production release PR will be created on Sunday. |
| 167 | +
|
| 168 | + --- |
| 169 | + *This PR was automatically created by the Release Calendar workflow* |
| 170 | + """)) |
| 171 | + PY |
| 172 | +
|
| 173 | + TITLE="Release to Staging - ${PR_DATE}" |
| 174 | + echo "Creating PR with title: ${TITLE}" |
| 175 | +
|
| 176 | + gh pr create \ |
| 177 | + --base staging \ |
| 178 | + --head dev \ |
| 179 | + --title "${TITLE}" \ |
| 180 | + --label release \ |
| 181 | + --label automated \ |
| 182 | + --label staging \ |
| 183 | + --body-file pr_body.md |
| 184 | +
|
| 185 | + release_to_production: |
| 186 | + name: Create staging to main release PR |
| 187 | + runs-on: ubuntu-latest |
| 188 | + steps: |
| 189 | + - name: Guard Sunday schedule |
| 190 | + id: guard_schedule |
| 191 | + shell: bash |
| 192 | + run: | |
| 193 | + set -euo pipefail |
| 194 | +
|
| 195 | + # Skip production job on push events (we only test on dev) |
| 196 | + if [ "${{ github.event_name }}" == "push" ]; then |
| 197 | + echo "Push event: skipping production job." |
| 198 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 199 | + exit 0 |
| 200 | + fi |
| 201 | +
|
| 202 | + # Allow workflow_dispatch based on input |
| 203 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 204 | + JOB_TO_RUN="${{ inputs.job_to_run }}" |
| 205 | + if [ "$JOB_TO_RUN" == "production" ]; then |
| 206 | + echo "Manual trigger: running production job (job_to_run=${JOB_TO_RUN})" |
| 207 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 208 | + else |
| 209 | + echo "Manual trigger: skipping production job (job_to_run=${JOB_TO_RUN})" |
| 210 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 211 | + fi |
| 212 | + exit 0 |
| 213 | + fi |
| 214 | +
|
| 215 | + # For schedule events, check day of week |
| 216 | + DOW=$(date -u +%u) |
| 217 | + if [ "$DOW" != "7" ]; then |
| 218 | + echo "Not Sunday in UTC (current day-of-week: $DOW). Exiting job early." |
| 219 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 220 | + exit 0 |
| 221 | + fi |
| 222 | +
|
| 223 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 224 | +
|
| 225 | + - name: Check out repository |
| 226 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' }} |
| 227 | + uses: actions/checkout@v4 |
| 228 | + with: |
| 229 | + fetch-depth: 0 |
| 230 | + |
| 231 | + - name: Determine release readiness |
| 232 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' }} |
| 233 | + id: production_status |
| 234 | + env: |
| 235 | + GH_TOKEN: ${{ github.token }} |
| 236 | + shell: bash |
| 237 | + run: | |
| 238 | + set -euo pipefail |
| 239 | +
|
| 240 | + echo "Fetching latest branches..." |
| 241 | + git fetch origin main staging |
| 242 | +
|
| 243 | + COMMITS_AHEAD=$(git rev-list --count origin/main..origin/staging) |
| 244 | + echo "commits=${COMMITS_AHEAD}" >> "$GITHUB_OUTPUT" |
| 245 | +
|
| 246 | + if [ "$COMMITS_AHEAD" -eq 0 ]; then |
| 247 | + echo "staging_not_ahead=true" >> "$GITHUB_OUTPUT" |
| 248 | + echo "Staging is up to date with main. No release PR needed." |
| 249 | + exit 0 |
| 250 | + fi |
| 251 | +
|
| 252 | + echo "staging_not_ahead=false" >> "$GITHUB_OUTPUT" |
| 253 | +
|
| 254 | + echo "Checking for existing pull requests from staging to main..." |
| 255 | + EXISTING_PR=$(gh pr list --base main --head staging --state open --limit 1 --json number --jq '.[0].number // ""') |
| 256 | + echo "existing_pr=${EXISTING_PR}" >> "$GITHUB_OUTPUT" |
| 257 | +
|
| 258 | + if [ -n "$EXISTING_PR" ]; then |
| 259 | + echo "Found existing production release PR: #${EXISTING_PR}. Skipping creation." |
| 260 | + else |
| 261 | + echo "No existing production release PR found. Ready to create a new one." |
| 262 | + fi |
| 263 | +
|
| 264 | + PR_DATE=$(date +%Y-%m-%d) |
| 265 | + echo "date=${PR_DATE}" >> "$GITHUB_OUTPUT" |
| 266 | +
|
| 267 | + - name: Log staging up to date |
| 268 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.production_status.outputs.staging_not_ahead == 'true' }} |
| 269 | + run: | |
| 270 | + echo "Staging branch is up to date with main. Skipping production release PR creation." |
| 271 | +
|
| 272 | + - name: Log existing PR |
| 273 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.production_status.outputs.existing_pr != '' }} |
| 274 | + run: | |
| 275 | + echo "Production release PR already exists: #${{ steps.production_status.outputs.existing_pr }}" |
| 276 | +
|
| 277 | + - name: Ensure release labels exist |
| 278 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.production_status.outputs.staging_not_ahead != 'true' && steps.production_status.outputs.existing_pr == '' }} |
| 279 | + env: |
| 280 | + GH_TOKEN: ${{ github.token }} |
| 281 | + shell: bash |
| 282 | + run: | |
| 283 | + set -euo pipefail |
| 284 | +
|
| 285 | + for LABEL in release automated production; do |
| 286 | + if ! gh label list --json name --jq '.[].name' | grep -q "^${LABEL}$"; then |
| 287 | + echo "Creating missing label: ${LABEL}" |
| 288 | + gh label create "${LABEL}" --color BFD4F2 |
| 289 | + else |
| 290 | + echo "Label ${LABEL} already exists." |
| 291 | + fi |
| 292 | + done |
| 293 | +
|
| 294 | + - name: Create staging to main release PR |
| 295 | + if: ${{ steps.guard_schedule.outputs.continue == 'true' && steps.production_status.outputs.staging_not_ahead != 'true' && steps.production_status.outputs.existing_pr == '' }} |
| 296 | + env: |
| 297 | + GH_TOKEN: ${{ github.token }} |
| 298 | + PR_DATE: ${{ steps.production_status.outputs.date }} |
| 299 | + COMMITS_AHEAD: ${{ steps.production_status.outputs.commits }} |
| 300 | + shell: bash |
| 301 | + run: | |
| 302 | + set -euo pipefail |
| 303 | +
|
| 304 | + python <<'PY' |
| 305 | + import os |
| 306 | + import pathlib |
| 307 | + import textwrap |
| 308 | +
|
| 309 | + commits_ahead = os.environ["COMMITS_AHEAD"] |
| 310 | +
|
| 311 | + pathlib.Path("pr_body.md").write_text(textwrap.dedent(f"""\ |
| 312 | + ## 🎯 Production Release |
| 313 | +
|
| 314 | + This automated PR promotes tested changes from `staging` to `main` for production deployment. |
| 315 | +
|
| 316 | + ### What's Included |
| 317 | + All changes that have been verified in the staging environment. |
| 318 | +
|
| 319 | + **Commits ahead**: {commits_ahead} |
| 320 | +
|
| 321 | + ### Pre-Deployment Checklist |
| 322 | + - [ ] All staging tests passed |
| 323 | + - [ ] QA sign-off received |
| 324 | + - [ ] Stakeholder approval obtained |
| 325 | + - [ ] Deployment plan reviewed |
| 326 | + - [ ] Rollback plan confirmed |
| 327 | +
|
| 328 | + ### Deployment Notes |
| 329 | + Merging this PR will trigger production deployment. |
| 330 | +
|
| 331 | + --- |
| 332 | + *This PR was automatically created by the Release Calendar workflow* |
| 333 | + """)) |
| 334 | + PY |
| 335 | +
|
| 336 | + TITLE="Release to Production - ${PR_DATE}" |
| 337 | + echo "Creating PR with title: ${TITLE} and ${COMMITS_AHEAD} commits ahead." |
| 338 | +
|
| 339 | + gh pr create \ |
| 340 | + --base main \ |
| 341 | + --head staging \ |
| 342 | + --title "${TITLE}" \ |
| 343 | + --label release \ |
| 344 | + --label automated \ |
| 345 | + --label production \ |
| 346 | + --body-file pr_body.md |
0 commit comments