id: git-bisect-first-bad-commit
namespace: company.team
inputs:
- id: repository_url
type: STRING
displayName: Repository
description: Leave empty to use the built-in demo repository, 40 commits with
one regression. Set a clone URL to bisect your own code.
defaults: ""
- id: good_ref
type: STRING
displayName: Known good commit
description: A commit or tag where the test passed. Ignored by the demo repository.
defaults: v1.0.0
- id: bad_ref
type: STRING
displayName: Known bad commit
defaults: HEAD
- id: test_command
type: STRING
displayName: Test command
description: Exit 0 when the commit is good, 1 to 124 when it is bad, 125 to
skip a commit that cannot be tested.
defaults: sh test.sh
- id: demo_regression_at
type: INT
displayName: Demo regression at commit
description: Which of the 40 demo commits breaks the test. Use 0 to build a demo
repository with no regression.
defaults: 27
variables:
report_kv: last_bisect_result
tasks:
- id: bisect
type: io.kestra.plugin.scripts.shell.Commands
description: >
Check the good and bad ends first, so a wrong range fails with a clear
message, then let git bisect run the test until it names the first bad
commit. Reports the commit, its author and message, the files it changed
and how many test runs it took.
containerImage: alpine/git:2.47.2
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: [ "" ]
env:
REPO_URL: "{{ inputs.repository_url }}"
GOOD: "{{ inputs.good_ref }}"
BAD: "{{ inputs.bad_ref }}"
TEST_COMMAND: "{{ inputs.test_command }}"
REGRESSION_AT: "{{ inputs.demo_regression_at }}"
commands:
- |
# Failing tests are expected here, so Kestra's default set -e is turned off.
set +e -u
git config --global user.email ci@example.invalid
git config --global user.name "Kestra CI"
git config --global advice.detachedHead false
if [ -n "$REPO_URL" ]; then
git clone -q "$REPO_URL" repo
else
# Demo repository: a pricing module, 40 commits, one of them breaks the discount rule.
mkdir repo && cd repo && git init -q -b main
printf 'discount() { echo $(( $1 * 90 / 100 )); }\n' > pricing.sh
printf '. ./pricing.sh\n[ "$(discount 200)" = 180 ] || { echo "expected 180, got $(discount 200)"; exit 1; }\n' > test.sh
git add . && git commit -qm "Add pricing with a 10% discount" && git tag v1.0.0
i=2
while [ "$i" -le 40 ]; do
echo "change $i" >> CHANGELOG.md
if [ "$i" -eq "$REGRESSION_AT" ]; then
printf 'discount() { echo $(( $1 * 90 / 10 )); }\n' > pricing.sh
git add . && git -c user.name="Dana Dev" -c user.email=dana@example.invalid commit -qm "Refactor discount rounding"
else
git add . && git commit -qm "Housekeeping change $i"
fi
i=$((i + 1))
done
cd ..
fi
cd repo
# Resolve both ends to commit ids first: after checking out the good end, HEAD would point there.
GOOD=$(git rev-parse --verify "$GOOD^{commit}") && BAD=$(git rev-parse --verify "$BAD^{commit}") || { echo "Unknown ref"; exit 1; }
commits=$(git rev-list --count "$GOOD..$BAD")
git checkout -q "$GOOD" && sh -c "$TEST_COMMAND" >/dev/null 2>&1; good_rc=$?
git checkout -q "$BAD" && sh -c "$TEST_COMMAND" >/dev/null 2>&1; bad_rc=$?
git checkout -q "$BAD"
if [ "$good_rc" -ne 0 ]; then verdict=GOOD_REF_FAILS
elif [ "$bad_rc" -eq 0 ]; then verdict=NO_REGRESSION
else
git bisect start "$BAD" "$GOOD" >/dev/null
git bisect run sh -c "$TEST_COMMAND" > bisect.log 2>&1
sha=$(grep -oE '^[0-9a-f]{40} is the first bad commit' bisect.log | cut -c1-40)
steps=$(grep -c '^running' bisect.log)
git bisect reset >/dev/null 2>&1
verdict=FOUND
fi
if [ "$verdict" = FOUND ] && [ -n "$sha" ]; then
author=$(git log -1 --format='%an <%ae>' "$sha"); subject=$(git log -1 --format='%s' "$sha" | sed 's/"/\\"/g')
files=$(git show --name-only --format= "$sha" | paste -sd ',' -); date=$(git log -1 --format=%cI "$sha")
failure=$(git checkout -q "$sha" && sh -c "$TEST_COMMAND" 2>&1 | tail -1 | sed 's/"/\\"/g'; git checkout -q "$BAD")
else
sha=""; author=""; subject=""; files=""; date=""; failure=""; steps=0
fi
printf '::{"outputs":{"verdict":"%s","commits_in_range":%s,"test_runs":%s,"first_bad":"%s","author":"%s","subject":"%s","files":"%s","committed":"%s","failure":"%s"}}::\n' \
"$verdict" "$commits" "${steps:-0}" "$sha" "$author" "$subject" "$files" "$date" "$failure"
- id: verdict
type: io.kestra.plugin.core.flow.Switch
value: "{{ outputs.bisect.vars.verdict }}"
cases:
FOUND:
- id: report_culprit
type: io.kestra.plugin.core.log.Log
message: >-
First bad commit {{ outputs.bisect.vars.first_bad | slice(0, 10) }}
"{{ outputs.bisect.vars.subject }}" by {{ outputs.bisect.vars.author
}} ({{ outputs.bisect.vars.committed }}), changing {{
outputs.bisect.vars.files }}. Found with {{
outputs.bisect.vars.test_runs }} test runs across {{
outputs.bisect.vars.commits_in_range }} commits. Test output at that
commit: {{ outputs.bisect.vars.failure }}
- id: record_culprit
type: io.kestra.plugin.core.kv.Set
key: "{{ vars.report_kv }}"
kvType: JSON
value: "{{ outputs.bisect.vars | toJson }}"
NO_REGRESSION:
- id: nothing_to_find
type: io.kestra.plugin.core.log.Log
message: "The test passes at {{ inputs.bad_ref }}, so there is no regression
between {{ inputs.good_ref }} and {{ inputs.bad_ref }}."
defaults:
- id: bad_range
type: io.kestra.plugin.core.execution.Fail
description: Bisecting from a good end that already fails would blame the wrong
commit.
errorMessage: "The test already fails at the good end {{ inputs.good_ref }}.
Pick an older good commit before bisecting."
triggers:
- id: on_red_build
type: io.kestra.plugin.core.trigger.Webhook
disabled: true
description: Call from CI when main turns red, with good_ref set to the last
green commit. Replace the key first.
key: replace-with-a-long-random-key