Skip to content

Commit 93b07ff

Browse files
committed
optimizing ci usage
1 parent 3dc2af8 commit 93b07ff

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

docs/src/content/docs/faq.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,6 @@ jobs:
9494
- name: Run a step only when the stack targets a release branch
9595
if: github.event.pull_request.stack != null && startsWith(github.event.pull_request.stack.base.ref, 'release/')
9696
run: echo "This stack targets a release branch"
97-
98-
- name: Run a step only on the bottom PR of the stack
99-
if: github.event.pull_request.stack.position == 1
100-
run: echo "This is the bottom PR"
101-
102-
- name: Run a step only on the lowest unmerged PR of the stack
103-
if: github.event.pull_request.stack.base.ref == github.event.pull_request.base.ref
104-
run: echo "This is the bottom PR"
105-
106-
- name: Run a step only on the top PR of the stack
107-
if: github.event.pull_request.stack.position == github.event.pull_request.stack.size
108-
run: echo "This is the top PR"
10997
```
11098
11199
| Expression | Description |
@@ -118,6 +106,33 @@ jobs:
118106

119107
See the [Webhooks reference](/gh-stack/reference/webhooks/) for the full details on the `stack` object in webhook payloads, or the [REST API reference](/gh-stack/reference/rest-api/) to read the same object on demand from a pull request.
120108

109+
### How can I optimize CI usage for a stack?
110+
111+
Because a workflow runs for every PR in a stack, a large stack can multiply your CI usage. You can use the `stack` fields to selectively run jobs based on the position of the current PR in the stack.
112+
113+
Two conditions are especially useful for deciding where a job should run:
114+
115+
- **Lowest unmerged PR** — the PR currently at the bottom of the remaining stack. Because it targets the stack base directly, `github.event.pull_request.stack.base.ref` equals `github.event.pull_request.base.ref`.
116+
- **Top PR** — the last PR in the stack, containing the full set of changes. It's the PR where `github.event.pull_request.stack.position` equals `github.event.pull_request.stack.size`.
117+
118+
```yaml
119+
jobs:
120+
build:
121+
runs-on: ubuntu-latest
122+
steps:
123+
- uses: actions/checkout@v4
124+
125+
- name: Run for the lowest unmerged PR in the stack
126+
if: github.event.pull_request.stack.base.ref == github.event.pull_request.base.ref
127+
run: echo "Lowest unmerged PR in the stack"
128+
129+
- name: Run for the top PR in the stack
130+
if: github.event.pull_request.stack.position == github.event.pull_request.stack.size
131+
run: echo "Top PR in the stack"
132+
```
133+
134+
As PRs merge from the bottom up, the lowest unmerged PR changes: once the bottom PR lands, the next PR is rebased to target the stack base directly, so it becomes the new lowest unmerged PR on the following workflow run. You can also gate on the original bottom PR with `github.event.pull_request.stack.position == 1`, or on any specific layer using `position`.
135+
121136
### Do all previous PRs need to be passing checks before I can merge?
122137

123138
Yes. In order to merge a PR in the stack, **all PRs below it** must also have passing checks and meet all merge requirements. For example, in a stack of `main <- PR1 <- PR2 <- PR3`, if you want to merge PR #3, both PR #1 and PR #2 must have passing checks, required reviews, and satisfy all branch protection rules.

docs/src/content/docs/reference/webhooks.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,25 @@ The top-level `stack` object is unique to the `stacked` event; other `pull_reque
106106
GitHub Actions automatically evaluates workflow triggers using the stack's base branch. If a PR is part of a stack targeting `main`, any workflow configured to run on pull requests targeting `main` will run for every PR in the stack — no workflow changes are required.
107107

108108
The `stack` object is also available in GitHub Actions workflow expressions via `github.event.pull_request.stack`. See [How do I access stack metadata in my GitHub Actions workflow?](/gh-stack/faq/#how-do-i-access-stack-metadata-in-my-github-actions-workflow) in the FAQ for examples.
109+
110+
### Optimizing CI usage
111+
112+
Because a workflow runs for every PR in a stack, you can use the `stack` fields to selectively run jobs. For example, if you only plan on merging one PR at a time, you can choose to only run CI for the lowest unmerged PR. Compare the stack's base ref to the PR's own base ref to detect the **lowest unmerged PR**, and compare `position` to `size` to detect the **top PR**:
113+
114+
```yaml
115+
jobs:
116+
build:
117+
runs-on: ubuntu-latest
118+
steps:
119+
- uses: actions/checkout@v4
120+
121+
- name: Run for the lowest unmerged PR in the stack
122+
if: github.event.pull_request.stack.base.ref == github.event.pull_request.base.ref
123+
run: echo "Lowest unmerged PR in the stack"
124+
125+
- name: Run for the top PR in the stack
126+
if: github.event.pull_request.stack.position == github.event.pull_request.stack.size
127+
run: echo "Top PR in the stack"
128+
```
129+
130+
See [How can I optimize CI usage for a stack?](/gh-stack/faq/#how-can-i-optimize-ci-usage-for-a-stack) for more detail.

0 commit comments

Comments
 (0)