Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/maintain-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Maintain Staging

# Rebuilds the staging branch from the primary branch plus every open pull request labeled "Staging". Call it on
# pull_request events (labeled, unlabeled, closed, synchronize); it runs only when the event can change staging.
# A pull request that won't merge loses the label and gets a comment listing the conflicts.

on:
workflow_call:
inputs:
primary-branch:
description: Branch the staging branch is rebuilt from.
type: string
default: main

merge-drivers:
description: >
Git merge drivers for staging-auto-merge, as a YAML list. See its `merge-drivers` input. Pass `[]` for
none.
type: string
default: railsschema

runner:
description: Runner to use
type: string
default: ubuntu-latest

secrets:
deploy-key:
description: >
SSH deploy key with write access. A push made with the default GITHUB_TOKEN doesn't trigger other
workflows, so pushing with this key is what lets the push to staging start a deploy.
required: true

jobs:
rebuild:
name: Rebuild Staging
if: contains(github.event.pull_request.labels.*.name, 'Staging') || github.event.label.name == 'Staging' || (github.event.action == 'closed' && github.event.pull_request.merged)
runs-on: ${{ inputs.runner }}
timeout-minutes: 5
concurrency:
group: maintain-staging
permissions:
contents: write
issues: read # finds closed pull requests that still carry the Staging label
pull-requests: write # comments on and unlabels pull requests that won't merge
steps:
- uses: actions/checkout@v7
with:
ref: staging
fetch-depth: 0
ssh-key: ${{ secrets.deploy-key }}

- uses: RoleModel/actions/staging-auto-merge@v3
with:
primary-branch: ${{ inputs.primary-branch }}
merge-drivers: ${{ inputs.merge-drivers }}
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Example:

## staging-auto-merge

Use this action to maintain a `staging` branch with the changes from all open PRs labeled `Staging`. This is recommended to be used with a workflow that auto-deploys the `staging` branch.
Use this action to maintain a `staging` branch with the changes from all open PRs labeled `Staging`. This is recommended to be used with a workflow that auto-deploys the `staging` branch. Most apps should call the [Maintain Staging reusable workflow](#maintain-staging-reusable-workflow), which wraps this action.

If a PR cannot be merged cleanly, the action comments on the PR with conflict details and removes the `Staging` label.

Expand All @@ -49,6 +49,34 @@ Inputs:

- `github-token`: GitHub token used to list/update pull requests. Default: `${{ github.token }}`
- `primary-branch`: Branch used as the reset base before staged merges. Default: `main`
- `merge-drivers`: [Git merge drivers](https://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver) to use while merging, as a YAML list. Default: none

### Merge drivers

A merge driver resolves conflicts git can't, such as two PRs that each bump the same version line. Each entry in `merge-drivers` is either the name of a built-in driver or a mapping for a custom one. You can list several and mix both kinds.

Built-in drivers:

- `railsschema`: for `db/schema.rb`. Two PRs that each add a migration both change the `define(version: …)` line. This sets that line to the newest version, then merges the rest of the file as git normally would, so a real schema conflict still fails.

A custom driver is a mapping with:

- `name`: letters, numbers, `-` and `_`; can't be a built-in driver's name
- `files`: a [gitattributes pattern](https://git-scm.com/docs/gitattributes#_description) or a list of them
- `script`: the driver itself, run with bash unless it starts with a `#!`. Git calls it with the base, ours, and theirs versions of the file, the conflict marker size, and the file's path. It writes the result to ours (`$2`) and exits non-zero to leave a conflict.

A built-in driver given as a mapping takes `files` to apply it to other files.

```yaml
merge-drivers: |
- name: railsschema
files: [db/schema.rb, db/animals_schema.rb]
- name: lockfile
files: [Gemfile.lock, yarn.lock]
script: exec bin/merge-lockfile "$@" # a script committed in the app
```

The action registers drivers in `.git/info/attributes` and the checkout's git config, so nothing is committed to `staging`.

Example:

Expand Down Expand Up @@ -83,8 +111,49 @@ jobs:

- name: Rebuild staging branch
uses: RoleModel/actions/staging-auto-merge@v3
with:
merge-drivers: railsschema
```

## Maintain Staging (reusable workflow)

`.github/workflows/maintain-staging.yml` is a `workflow_call` reusable workflow that runs `staging-auto-merge` with the trigger filter, checkout, and concurrency it needs. Runs happen one at a time.

```yaml
# .github/workflows/maintain-staging.yml
name: Maintain Staging Branch

on:
pull_request:
types: [labeled, unlabeled, closed, synchronize]

jobs:
staging:
uses: RoleModel/actions/.github/workflows/maintain-staging.yml@v3
permissions:
contents: write
issues: read
pull-requests: write
with:
primary-branch: main
secrets:
deploy-key: ${{ secrets.AUTOMATED_COMMITS_KEY }}
```

The caller must grant every permission the reusable job declares; if it grants less, the run fails at startup with no logs.

Inputs:

- `primary-branch`: Branch the staging branch is rebuilt from. Default: `main`
- `merge-drivers`: Passed to `staging-auto-merge`; see [Merge drivers](#merge-drivers). Pass `[]` for none. Default: `railsschema`
- `runner`: Runner to use. Default: `ubuntu-latest`

Secrets:

- `deploy-key`*: SSH deploy key with write access. A push with the default `GITHUB_TOKEN` doesn't trigger other workflows, so pushing with this key is what lets the push to `staging` start a deploy.

The repository needs a `staging` branch and a `Staging` label.

## Release (reusable workflow)

`.github/workflows/release.yml` is a `workflow_call` reusable workflow that cuts a release end to end: version bump, changelog entry, version commit and tag, GitHub release, and publishing to RubyGems and/or npm with trusted publishing. Call it from a workflow with two triggers:
Expand Down
9 changes: 9 additions & 0 deletions staging-auto-merge/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ inputs:
required: false
default: main

merge-drivers:
description: >
Git merge drivers to use while merging pull requests, as a YAML list.
Each entry is the name of a built-in driver (railsschema) or a mapping
with name, files (a pattern or list of patterns) and script (a custom
driver, called with base, ours, theirs, marker size and path). Default: none.
required: false
default: ''

runs:
using: node24
main: dist/index.js
Loading