2 min read
Second chances for Buildkite block steps
Jacob Fenton
We’ve setup Buildkite block steps for deploying to staging envs/deploying canaries from Buildkite; I alluded to them a bit in a recent post: https://community.fly.io/t/first-class-canary-deploys-make-for-fearless-dependency-updates/29039
When you unblock one of the steps, a modal pops up asking how long you want to take a lease on the staging env/canary, and, if you’re deploying to staging, which staging env you want to deploy to.
What always frustrated me about these “block steps” is that once you select your choices (e.g. staging-1 for 15 mins), you can’t change them. Even if the step fails because someone else holds the lease!
At that point, I obviously want to try deploying to a different staging env. But I only have the option to retry the step with the choices I’ve already locked in. The only choice you have is to start a whole new build, which can be a huge pain; our builds don’t take crazy long, but 3-4 mins is too long in this context.
When I mentioned this to :robot: , it came up with a clever idea - when the staging/canary step fails, we can call buildkite-agent pipeline upload within the step itself to add a fresh block step. That’s a bit of a mouthful, but basically I mean:
- I hit “Release canary deploy” to release the block step, and select to hold the lease for 15 minutes.
- The step runs the canary deploy script, which first checks if the canary lease is already held
- This check fails, and the deploy script then branches to
buildkite-agent pipeline uploadwith a config that creates a fresh “Release canary deploy (attempt 2)” block step. - The config comes from the main pipeline YAML file - the deploy script runs
buildkite-agent pipeline upload --dry-run --format json .buildkite/pipeline.ymland then usesjqto parse out the canary step. It then does some small transformation to add the “(attempt N)”. See this gist if you’re curious.- The reason for doing it this way is to keep everything DRY. If someone changes the canary step at all, it only needs to be updated in one place - the main pipeline YAML file.
An important detail is that the canary/staging step in the main pipeline file must be marked with soft_fail. Without it, Buildkite will fail the whole build and refuse to run anything further.
Hope someone finds this useful! :)