CI with Buildkite
What happens when you land#
Before a review lands, GitStack asks Buildkite to build the review's commit, and the land waits until the checks your policy requires pass. After the land, Buildkite builds the new tip of your default branch, and GitStack stops the build of an older tip if one is still running. If a required check fails on the default branch, GitStack runs the failed jobs again, and reverts the land when they fail a second time. Each build shows on the review as checks that link to the build in Buildkite, and the repository's home page shows the default branch's checks.
Pushing a review starts no build. Builds start only when a land needs
them, when someone presses Run checks on the review, or with
gs review ci.
New to this? Quickstart: CI with Buildkite walks through the same steps with an example repository. Moving a repository off GitHub Actions? See Moving from GitHub Actions.
Set it up#
You need owner access to the GitStack repository and a Buildkite organization. These steps use Buildkite's hosted agents, which need nothing installed. With your own agents, step 5 changes (see Your own agents).
-
Add the pipeline file. Give each step a
key: GitStack reports each keyed step as a check of its own. Theenvblock lets the agent clone from GitStack (step 5 stores the token it reads):yaml# .buildkite/pipeline.yml env: GIT_CONFIG_COUNT: "1" GIT_CONFIG_KEY_0: credential.https://gitstack.sh.helper GIT_CONFIG_VALUE_0: >- !f() { test "$$1" = get || exit 0; echo username=buildkite; printf 'password=%s\n' "$$(buildkite-agent secret get GITSTACK_FETCH_TOKEN)"; }; f steps: - label: "Lint" key: lint command: make lint - label: "Test" key: test command: make testWrite
$$where you mean$, since Buildkite reads a single$as a variable when it uploads the file. Land the file as a review. Nothing requires a check yet, so it lands at once. -
Create the pipeline in Buildkite. Set its repository to the GitStack clone URL, such as
https://gitstack.sh/acme/api.git. Give it the sameenvblock and one step that uploads the file, since the agent clones before it reads anything from the repository:yamlenv: GIT_CONFIG_COUNT: "1" GIT_CONFIG_KEY_0: credential.https://gitstack.sh.helper GIT_CONFIG_VALUE_0: >- !f() { test "$$1" = get || exit 0; echo username=buildkite; printf 'password=%s\n' "$$(buildkite-agent secret get GITSTACK_FETCH_TOKEN)"; }; f steps: - label: ":pipeline: upload" command: buildkite-agent pipeline uploadLeave Cancel intermediate builds off: GitStack cancels the builds it no longer needs itself, and keeps the ones auto-revert is using. If the repository is public, make the pipeline public too (its Settings, General), so contributors can read a build's log from the review without a Buildkite account.
-
Make an API token. Under your Buildkite user's API Access Tokens, make one for the pipeline's organization with the
read_buildsandwrite_buildsscopes. -
Connect the pipeline in GitStack. In the repository's Settings, CI checks, under Connect Buildkite, enter the organization and pipeline slugs from the pipeline's URL (
buildkite.com/<organization>/<pipeline>) and the API token, then choose Connect. GitStack shows three values. Copy all three now, since the fetch token is shown only once:- Webhook URL,
https://gitstack.sh/hooks/buildkite/<id> - Signing secret
- Fetch token, which starts with
gsi_
- Webhook URL,
-
Store the fetch token in Buildkite. In the cluster your hosted agents run in (Agents, then the cluster), open Secrets and add one named
GITSTACK_FETCH_TOKENwith the fetch token as its value. Give it an access policy that names the pipeline. Theenvblock reads it whenever git asks for a password, so the token never shows in a build's environment or log. -
Add the webhook in Buildkite. In the organization's Settings, Notification Services, add a Webhook:
- Webhook URL: the webhook URL from GitStack.
- Token: the signing secret from GitStack, with Send the token as X-Buildkite-Signature.
- Events:
build.scheduled,build.running,build.failing,build.finished,build.skipped,job.startedandjob.finished. - Pipelines: the connected pipeline.
The integration's status in GitStack changes from no deliveries to ok once a delivery arrives.
-
Require the checks. Name them under
[checks]in.gitstack/policy.tomland land that change as a review:toml[checks] required = ["buildkite:api/lint", "buildkite:api/test"]The policy on the default branch applies to a land, so this land doesn't wait for the checks it adds. The next one does.
Your own agents#
Agents you run take the fetch token from an agent environment hook
instead of a Buildkite secret. Leave the env block out of the pipeline
then:
# Buildkite agent hook: hooks/environment
export GITSTACK_FETCH_TOKEN="$(cat /etc/buildkite-agent/gitstack-fetch-token)"
git config --global credential.https://gitstack.sh.helper \
'!f() { echo username=buildkite; echo "password=${GITSTACK_FETCH_TOKEN}"; }; f'
The agents need to reach gitstack.sh over HTTPS. The token fetches every
commit of this one repository, review commits too. Unless you give it more
rights (Writing back from a build), that is
all it does.
Landing and running checks#
gs submit asks Buildkite for any build the land needs and waits for it:
gs submit
# checks started on R12 (a1b2c3d); waiting for buildkite:api/lint, buildkite:api/test (Ctrl-C to stop)
A failed check refuses the land and names the check. To try again, press
Run checks on the review or run gs review ci R12: when the last
build failed or was cancelled, they retry its failed jobs, and gs review ci prints retried. A job you retry in Buildkite counts the same way. A
land never retries on its own.
Buildkite builds any commit it is asked to, so a lower review of a stack
can land by itself: gs submit R12 builds R12's commit. GitStack never
builds a contributor's lane on its own, since the build runs on your
agents. A maintainer's land, Run checks or gs review ci builds it.
To stop a build GitStack started for a review, cancel the review's checks
with POST /api/v1/repos/{namespace}/{name}/reviews/{id}/checks/cancel.
It cancels only builds GitStack started, and answers with the ones it
stopped. A cancelled build reads as not reported, so the next land or
Run checks builds the commit again.
When the default branch goes red#
After every land, Buildkite builds the default branch's new tip. When a
required check fails there, GitStack retries the failed jobs once. If they
fail again and the commit before the land passed, GitStack reverts the
land: it opens a Revert: <title> review and lands it at once. See
When a land breaks the branch
for every case.
Three things keep this from reverting a land that wasn't at fault:
- Setup failures don't count. A step that fails while it installs a
tool or starts a service never tested the land. Have your pipeline's
scripts exit with status
75for those failures, and for nothing else. GitStack runs such a job again, and only comments if setup keeps failing. Any other exit status counts as a failure. A land that changes.buildkite/or a file setup reads (go.mod,.nvmrc,package.jsonand the like) counts its setup failures as failures, since it may have broken setup itself. - Cancelled builds don't count. When a land moves the default branch while an older tip's build still runs, GitStack cancels that build. A cancelled build is no result. If a later build fails, GitStack builds the commits it needs again before it decides which land to revert.
- Auto-revert's own builds are never cancelled. That covers the build it is retrying, the builds of the commits it is checking, and the build of its revert.
Builds for branches and tags#
Pushes to other branches and new tags build when the policy names them,
under [ci] in .gitstack/policy.toml:
[ci]
branches = ["release/*"]
tags = ["v*"]
Review branches and the default branch never build this way: they build through their reviews and their lands. A tag builds once, when it is made with gs tag, a push or the tags API. It names a landed commit and never moves, and deleting it builds nothing.
Writing back from a build#
When you make the fetch token in the repository's settings, you can give it more rights, each limited to this repository:
| Right | Lets the token |
|---|---|
checks |
Post check results with the checks API, POST /api/v1/repos/{namespace}/{name}/commits/{sha}/checks. |
tags |
Create tags on landed commits, by push or POST /api/v1/repos/{namespace}/{name}/tags. It can never move or delete one. |
branches |
Push the branches matching the patterns you give it. Never the default branch or a review branch. |
comment |
Post a comment on a review, shown as the integration with a bot label. |
checksPOST /api/v1/repos/{namespace}/{name}/commits/{sha}/checks.tagsPOST /api/v1/repos/{namespace}/{name}/tags. It can never move or delete one.branchescommentNew token replaces the old one, which stops working at once. Branch
patterns are globs where * stays within one path segment, so
release/* covers release/1.2 but not release/1/hot.
From a build, the token works with git and the API:
# tags right, on a build of the default branch (GITSTACK_EVENT=main):
# tag the landed commit and push the tag
git tag "v1.4.0" "$BUILDKITE_COMMIT"
git push origin "v1.4.0"
# comment right: comment on the review being built
curl -sS -X POST "https://gitstack.sh/api/v1/repos/$GITSTACK_REPO/reviews/$GITSTACK_REVIEW/integration_comments" \
-H "Authorization: Bearer $(buildkite-agent secret get GITSTACK_FETCH_TOKEN)" \
-H "Content-Type: application/json" \
-d '{"body": "Coverage is 81%, down 2 points from main."}'
Every push a token makes is recorded in the admin audit log under the integration. A branch a token pushes never starts a build on its own pipeline, so a job that pushes a branch can't start itself again.
Reference#
Check names#
A connected pipeline reports one check named after the pipeline (api),
and one for each command step with a key, named <pipeline>/<key>
(api/lint). Steps without a key report only through the pipeline's
check, since their labels can change.
In required |
Passes when |
|---|---|
buildkite:api |
The whole build passed. |
buildkite:api/lint |
The lint step passed. The land doesn't wait for the rest of the build. |
api |
Any connected CI service reports a passing api, and none reports it failing. |
buildkite:apibuildkite:api/lintlint step passed. The land doesn't wait for the rest of the build.apiapi, and none reports it failing.The buildkite: prefix pins a check to Buildkite, so no other CI service
can meet it. A step that soft fails counts as passing. See
Configuration & files
for the rest of [checks].
Build states#
| Buildkite state | Check |
|---|---|
scheduled, running, failing, waiting, blocked at a block step |
running |
passed |
passing |
failed, not_run, waiting_failed |
failing |
canceled, canceling, skipped |
not reported, so the next request builds it again |
scheduled, running, failing, waiting, blocked at a block steppassedfailed, not_run, waiting_failedcanceled, canceling, skippedA job reads the same way. A job that soft fails is passing, and one that never ran (skipped, or broken by a failed dependency) is not reported.
What a build carries#
Each build GitStack starts has these in its environment:
| Variable | Value |
|---|---|
GITSTACK_EVENT |
review (a review's commit), main (the default branch after a land), push (a branch [ci] branches names) or tag (a tag [ci] tags names) |
GITSTACK_REPO |
The repository, <namespace>/<name> |
GITSTACK_REF |
The full ref the build is for, such as refs/heads/billing |
GITSTACK_REVIEW |
The review id, on review builds only |
GITSTACK_STACK |
The review's branch, on review builds only |
GITSTACK_DELIVERY |
The request's id, the same if GitStack asks again |
GITSTACK_EVENTreview (a review's commit), main (the default branch after a land), push (a branch [ci] branches names) or tag (a tag [ci] tags names)GITSTACK_REPO<namespace>/<name>GITSTACK_REFrefs/heads/billingGITSTACK_REVIEWreview builds onlyGITSTACK_STACKreview builds onlyGITSTACK_DELIVERYThe build's branch is the review's branch (the tag's name for a tag
build), and its message is the review's title. Its meta-data holds the
integration's id, gitstack_integration. Only builds that carry it
report to GitStack: a build you start in Buildkite, by hand or on a
schedule, shows in Buildkite only, and never gates a land or starts an
auto-revert. Retrying a job of a build GitStack started still counts.
GitStack never sees your agents' secrets, and puts none of its own in a build.
Webhook events#
The build events (build.scheduled, build.running, build.failing,
build.finished, build.skipped) carry the pipeline's check, and the job
events (job.started, job.finished) each step's as it happens. Without
the job events, the steps' results arrive when the build finishes.
GitStack refuses deliveries that carry the token in plain text, and
ignores deliveries for other pipelines. It also reads builds still running
every few minutes, so a delivery that never comes doesn't leave a check
waiting for good.
Setup exit status#
Exit status 75 from a job says it failed while setting up. GitStack
counts a failed job as setup only when it exited 75, or expired before
any agent ran it, and a build's check only when every failed job did.
Make sure nothing else in a job exits 75, so a failure in your tests
never reads as setup.
API token scopes#
GitStack uses read_builds to find and read its builds, and
write_builds to create, retry and cancel them. It cancels only builds it
started.
When the integration breaks#
If Buildkite refuses the API token (it was revoked, or lost its scopes), the integration shows as broken, its checks read as missing, and a land names it. Connect it again with a new token. Disconnect removes the integration, its fetch token and the checks it reported.
Self-hosting#
The connector works the same on a self-hosted server. The server needs
outbound HTTPS to api.buildkite.com, and Buildkite needs to reach the
server's webhook URL. GITSTACK_CI_POLL_INTERVAL sets how often running
builds are read again (default 2m).
For a builder other than Buildkite, see CI with any builder.