# Find last built commit

**URL:** <https://forum.buildkite.community/t/find-last-built-commit/4416>\
**Category:** Pipelines\
**Created:** [July 1, 2025, 9:43am UTC](https://forum.buildkite.community/t/find-last-built-commit/4416 "2025-07-01T09:43:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenogueira](https://avatars.discourse-cdn.com/v4/letter/z/e480ec/32.png) [@zenogueira](https://forum.buildkite.community/u/zenogueira)\
**Post date:** [July 1, 2025, 9:43am UTC](https://forum.buildkite.community/t/find-last-built-commit/4416/1 "2025-07-01T09:43:41Z")

</div>

Hello,

Is there a way to know what the last built commit was on the current branch and pipeline?

We have many servers running builds from different branches at the same time on a few pipelines, and wanted to know at build start which was the last commit _from that branch_ to be run _on that pipeline_.

Currently we `git diff` against the main development branch or HEAD, but this doesn’t give correct results, as branches drift apart, as well as we can push more than one commit at a time.

Is there a better solution already built-in?

Thank you

---

<div class="post-metadata">

**Author:** ![lukasz-buildkite](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/lukasz-buildkite/32/1590_2.png) [@lukasz-buildkite](https://forum.buildkite.community/u/lukasz-buildkite)\
**Post date:** [July 1, 2025, 2:05pm UTC](https://forum.buildkite.community/t/find-last-built-commit/4416/2 "2025-07-01T14:05:16Z")

</div>

Hey @zenogueira 👋

You can use our [builds REST API](https://buildkite.com/docs/apis/rest-api/builds) for this.  
It’d require an API Access Token with `read_builds` permission to be available via the environment variable `$BUILDKITE_API_TOKEN`.

A sample script that could be executed from the pipeline:

```bash
#!/usr/bin/env bash
set -euo pipefail

# Get last 2 successful builds from current branch
BUILDS=$(curl -sf -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" \
  "https://api.buildkite.com/v2/organizations/${BUILDKITE_ORGANIZATION_SLUG}/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds?branch=${BUILDKITE_BRANCH}&state=passed&per_page=2")

CURRENT_BUILD_NUM="${BUILDKITE_BUILD_NUMBER}"
FIRST_BUILD_NUM=$(echo "$BUILDS" | jq -r '.[0].number')
FIRST_COMMIT=$(echo "$BUILDS" | jq -r '.[0].commit')
SECOND_COMMIT=$(echo "$BUILDS" | jq -r '.[1].commit // empty')

if ["$CURRENT_BUILD_NUM" = "$FIRST_BUILD_NUM"] && [-n "$SECOND_COMMIT"]; then
  LAST_COMMIT="$SECOND_COMMIT"
else
  LAST_COMMIT="$FIRST_COMMIT"
fi

echo "Current commit: ${BUILDKITE_COMMIT}"
echo "Last successful commit: ${LAST_COMMIT}"
echo "Commits since last build:"
git log --oneline "${LAST_COMMIT}".."${BUILDKITE_COMMIT}"

```

Depending on your usecase, [GitHub - buildkite-plugins/annotate-git-diff-buildkite-plugin: A Buildkite plugin to show the diff of the current build compared to the previous one on a chosen branch.](https://github.com/buildkite-plugins/annotate-git-diff-buildkite-plugin) can be another option to consider.

---

<div class="post-metadata">

**Author:** ![zenogueira](https://avatars.discourse-cdn.com/v4/letter/z/e480ec/32.png) [@zenogueira](https://forum.buildkite.community/u/zenogueira)\
**Post date:** [July 1, 2025, 2:29pm UTC](https://forum.buildkite.community/t/find-last-built-commit/4416/3 "2025-07-01T14:29:56Z")

</div>

That’s a good idea, I will take a look at it, and adapt it to our needs.

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/system/32/1321_2.png) [@system](https://forum.buildkite.community/u/system)\
**Post date:** [August 1, 2025, 12:30am UTC](https://forum.buildkite.community/t/find-last-built-commit/4416/4 "2025-08-01T00:30:15Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
