We trigger another pipeline at the end of the main CI build. However, we’d like for certain steps to only run if the original/main build was successful. Is there a way to do this check in the if:
key?
Hi @ngan! Hrmm good question.
Without an example pipeline, it might be a bit tricky to know what makes most sense for you, but I’ll outline a few things that might help.
- You can use
continue_on_failure
(https://buildkite.com/docs/pipelines/wait-step), orallow_dependency_failure
(https://buildkite.com/docs/pipelines/dependencies#allowing-dependency-failures) if you’re using DAG, to run future steps in the face of failures. - With the above options, you could have a script that conditionally sets an environment variable on the triggered build, and that child build could then use the
if
property based on the environment variable value.
For example, this could be the parent pipeline’s pipeline.yml file:
steps:
- label: "🔨 Tests"
command: tests.sh
key: tests
- label: ":pipeline:"
command: scripts/trigger-child-build.sh
depends_on: tests
allow_dependency_failure: true
The scripts/trigger-child-build.sh
script has the logic to upload an additional trigger step, with the env variable set for the child build, and the trigger step will be added immediately to the current build:
#!/bin/bash
set -euo pipefail
test_step_outcome="$(buildkite-agent step get outcome --step tests)"
buildkite-agent pipeline upload <<YAML
steps:
- label: "🚀 Child"
trigger: "child-pipeline"
build:
env:
TESTS_OUTCOME: "${test_step_outcome}"
YAML
And then in the child pipeline’s pipeline.yml, you can use an if
conditional like so:
steps:
- command: some-script.sh
if: 'build.env("TESTS_OUTCOME") == "passed"'
The different step outcomes that you can check for are:
passed
hard_failed
soft_failed
-
neutral
(e.g. wait steps, skipped steps) -
errored
(e.g. canceled, timed out, etc)
Would something like that solve what you’re trying to do? If not, perhaps let us know a little more about the context and pipeline, and we provide some more alternatives.