-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add production deploy and automatic staging sync #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| name: Sync Staging | ||
|
|
||
| # Keeps the `staging` branch tracking `main` so hosting platforms configured | ||
| # to auto-deploy from `staging` pick up every merge automatically. This | ||
| # mirrors what `make deploy-prod` (scripts/deploy-prod.sh) does for | ||
| # `production`, but runs unattended — no confirmation prompt — since staging | ||
| # is meant to always match main. | ||
| # | ||
| # Uses a PAT (SYNC_STAGING_PAT), not the default GITHUB_TOKEN, for the push: | ||
| # GitHub's built-in loop-prevention rule means pushes made with the default | ||
| # GITHUB_TOKEN do NOT trigger other `on: push` workflows — so any CD workflow | ||
| # with a `push: branches: [staging]` trigger would silently never fire, and | ||
| # this workflow would update the staging ref without actually deploying | ||
| # anything. A PAT belonging to a real account avoids that. Generate a PAT | ||
| # (repo scope, push access to this repo) and add it as a repository secret | ||
| # named SYNC_STAGING_PAT. Commit authorship is resolved from github.actor | ||
| # below, not hardcoded, so this works unmodified for any fork. | ||
| on: | ||
| push: | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| sync: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| ref: main | ||
| fetch-depth: 0 | ||
| token: ${{ secrets.SYNC_STAGING_PAT }} | ||
|
|
||
| - name: Configure git identity | ||
| run: | | ||
| git config user.name "${{ github.actor }}" | ||
| git config user.email "${{ github.actor }}@users.noreply.github.com" | ||
|
|
||
| - name: Sync staging onto main | ||
| run: bash scripts/sync-staging.sh |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # scripts/deploy-prod.ps1 — rebase production onto main and force-push (with lease) | ||
| # Usage: .\scripts\deploy-prod.ps1 (or via: make deploy-prod) | ||
| # | ||
| # Fast-forwards `production` to include everything on `main` by rebasing it | ||
| # onto origin/main, then force-pushes (with lease, never a bare --force) so | ||
| # the push fails instead of clobbering anyone else's work if the remote | ||
| # `production` moved since we last fetched. | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| $Remote = "origin" | ||
| $BaseBranch = "main" | ||
| $DeployBranch = "production" | ||
|
|
||
| $status = git status --porcelain | ||
| if ($status) { | ||
| Write-Host "Working tree is not clean. Commit, stash, or discard changes before deploying." -ForegroundColor Red | ||
| exit 1 | ||
| } | ||
|
|
||
| $originalBranch = git rev-parse --abbrev-ref HEAD | ||
| $cleanExit = $false | ||
|
|
||
| try { | ||
| Write-Host "Fetching from $Remote..." -ForegroundColor Yellow | ||
| git fetch $Remote | ||
| if ($LASTEXITCODE -ne 0) { throw "git fetch failed" } | ||
|
|
||
| git show-ref --verify --quiet "refs/remotes/$Remote/$DeployBranch" | ||
| $deployBranchExists = ($LASTEXITCODE -eq 0) | ||
| if ($deployBranchExists) { | ||
| git show-ref --verify --quiet "refs/heads/$DeployBranch" | ||
| if ($LASTEXITCODE -eq 0) { | ||
| git checkout --quiet $DeployBranch | ||
| } else { | ||
| git checkout --quiet -b $DeployBranch "$Remote/$DeployBranch" | ||
| } | ||
| if ($LASTEXITCODE -ne 0) { throw "git checkout failed" } | ||
|
|
||
| # Make sure the local branch starts from exactly what's on the remote | ||
| # before rebasing, so we never rebase stale local commits onto main. | ||
| git reset --hard "$Remote/$DeployBranch" | ||
| if ($LASTEXITCODE -ne 0) { throw "git reset failed" } | ||
| } else { | ||
| # Remote branch doesn't exist yet (e.g. first deployment). | ||
| git checkout --quiet -b $DeployBranch "$Remote/$BaseBranch" | ||
| if ($LASTEXITCODE -ne 0) { throw "git checkout failed" } | ||
| } | ||
|
|
||
| Write-Host "Rebasing $DeployBranch onto $Remote/$BaseBranch..." -ForegroundColor Yellow | ||
| git rebase "$Remote/$BaseBranch" | ||
| if ($LASTEXITCODE -ne 0) { | ||
| Write-Host "Rebase hit conflicts. Resolve them, then run:" -ForegroundColor Red | ||
| Write-Host " git rebase --continue" | ||
| Write-Host " git push --force-with-lease $Remote $DeployBranch" | ||
| Write-Host "Or abort with: git rebase --abort" | ||
| # Leave the branch mid-rebase for the user to resolve — do not clean up. | ||
| exit 1 | ||
| } | ||
|
|
||
| Write-Host "" | ||
| Write-Host "About to force-push (with lease) $DeployBranch to $Remote. This will trigger a production deploy." -ForegroundColor Yellow | ||
| if ($deployBranchExists) { | ||
| $commits = git log "$Remote/$DeployBranch..$DeployBranch" --oneline | ||
| if (-not $commits) { | ||
| Write-Host "No new commits -- $DeployBranch already matches $BaseBranch." | ||
| } else { | ||
| $commits | ForEach-Object { Write-Host $_ } | ||
| } | ||
| } else { | ||
| Write-Host "Creating $DeployBranch on $Remote from $BaseBranch (first deployment)." | ||
| } | ||
|
|
||
| if ($env:CONFIRM -ne "yes") { | ||
| $reply = Read-Host "Continue? [y/N]" | ||
| if ($reply -notmatch '^(y|yes)$') { | ||
| Write-Host "Aborted. No push was made." | ||
| exit 1 | ||
| } | ||
| } | ||
|
|
||
| git push --force-with-lease $Remote $DeployBranch | ||
| if ($LASTEXITCODE -ne 0) { throw "git push failed" } | ||
|
|
||
| Write-Host "Deployed: $DeployBranch pushed to $Remote." -ForegroundColor Green | ||
| $cleanExit = $true | ||
| } | ||
| finally { | ||
| if ($cleanExit) { | ||
| git checkout --quiet $originalBranch 2>$null | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #!/usr/bin/env bash | ||
| # scripts/deploy-prod.sh — rebase production onto main and force-push (with lease) | ||
| # Usage: ./scripts/deploy-prod.sh (or via: make deploy-prod) | ||
| # | ||
| # Fast-forwards `production` to include everything on `main` by rebasing it | ||
| # onto origin/main, then force-pushes (with lease, never a bare --force) so | ||
| # the push fails instead of clobbering anyone else's work if the remote | ||
| # `production` moved since we last fetched. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| REMOTE="origin" | ||
| BASE_BRANCH="main" | ||
| DEPLOY_BRANCH="production" | ||
|
|
||
| RED='\033[1;31m' | ||
| YELLOW='\033[1;33m' | ||
| GREEN='\033[1;32m' | ||
| RESET='\033[0m' | ||
|
|
||
| if [ -n "$(git status --porcelain)" ]; then | ||
| echo -e "${RED}Working tree is not clean. Commit, stash, or discard changes before deploying.${RESET}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| ORIGINAL_BRANCH="$(git rev-parse --abbrev-ref HEAD)" | ||
| cleanup() { | ||
| git checkout --quiet "$ORIGINAL_BRANCH" 2>/dev/null || true | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| echo -e "${YELLOW}Fetching from ${REMOTE}...${RESET}" | ||
| git fetch "$REMOTE" | ||
|
|
||
| if git show-ref --verify --quiet "refs/remotes/$REMOTE/$DEPLOY_BRANCH"; then | ||
| DEPLOY_BRANCH_EXISTS=true | ||
| if git show-ref --verify --quiet "refs/heads/$DEPLOY_BRANCH"; then | ||
| git checkout --quiet "$DEPLOY_BRANCH" | ||
| else | ||
| git checkout --quiet -b "$DEPLOY_BRANCH" "$REMOTE/$DEPLOY_BRANCH" | ||
| fi | ||
| # Make sure the local branch starts from exactly what's on the remote before | ||
| # rebasing, so we never rebase stale local commits onto main by accident. | ||
| git reset --hard "$REMOTE/$DEPLOY_BRANCH" | ||
| else | ||
| # Remote branch doesn't exist yet (e.g. first deployment). | ||
| DEPLOY_BRANCH_EXISTS=false | ||
| git checkout --quiet -b "$DEPLOY_BRANCH" "$REMOTE/$BASE_BRANCH" | ||
| fi | ||
|
|
||
| echo -e "${YELLOW}Rebasing ${DEPLOY_BRANCH} onto ${REMOTE}/${BASE_BRANCH}...${RESET}" | ||
| if ! git rebase "$REMOTE/$BASE_BRANCH"; then | ||
| echo -e "${RED}Rebase hit conflicts. Resolve them, then run:${RESET}" >&2 | ||
| echo " git rebase --continue" >&2 | ||
| echo " git push --force-with-lease $REMOTE $DEPLOY_BRANCH" >&2 | ||
| echo "Or abort with: git rebase --abort" >&2 | ||
| # Leave the branch mid-rebase for the user to resolve — do not clean up. | ||
| trap - EXIT | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "" | ||
| echo -e "${YELLOW}About to force-push (with lease) ${DEPLOY_BRANCH} to ${REMOTE}. This will trigger a production deploy.${RESET}" | ||
| if [ "$DEPLOY_BRANCH_EXISTS" = true ]; then | ||
| COMMITS="$(git log "$REMOTE/$DEPLOY_BRANCH..$DEPLOY_BRANCH" --oneline)" | ||
| if [ -z "$COMMITS" ]; then | ||
| echo "No new commits — ${DEPLOY_BRANCH} already matches ${BASE_BRANCH}." | ||
| else | ||
| echo "$COMMITS" | ||
| fi | ||
| else | ||
| echo "Creating ${DEPLOY_BRANCH} on ${REMOTE} from ${BASE_BRANCH} (first deployment)." | ||
| fi | ||
|
|
||
| if [ "${CONFIRM:-}" != "yes" ]; then | ||
| read -r -p "Continue? [y/N] " reply || true | ||
| case "$reply" in | ||
| [yY][eE][sS]|[yY]) ;; | ||
| *) echo "Aborted. No push was made."; exit 1 ;; | ||
| esac | ||
| fi | ||
|
|
||
| git push --force-with-lease "$REMOTE" "$DEPLOY_BRANCH" | ||
| echo -e "${GREEN}Deployed: ${DEPLOY_BRANCH} pushed to ${REMOTE}.${RESET}" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.