Skip to content

Use pull request safe git_ref and git_commit in GitHub Actions examples - #3462

Merged
zentron merged 2 commits into
mainfrom
robe/github-actions-pull-request-git-ref
Sep 16, 2026
Merged

zentron merged 2 commits into
mainfrom
robe/github-actions-pull-request-git-ref

Conversation

@zentron

@zentron zentron commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Background

As noted in an old CaC document, the GitHub variables available during a GitHub Action differ between some of he events.

On a pull_request event those two values are refs/pull/<number>/merge and the merge commit GitHub creates for the pull request. That commit is not reachable from any branch or tag, so the release is pinned to a commit that Octopus can later fail to resolve. It also disagrees with what the workflow actually built, because a build normally checks out the branch head.

The Creating releases from a build server plugin page already warns about this but isn't completely accurate. The GitHub Actions page did not, and its examples are the ones people copy.

Without this specific configuration, a CaC deployment can fail as the branch specified with just github.ref may reference the merge branch that is removed post-merge.
E.g the error we see is along the lines of

No git resource with the name '718841c5...' exists in the repository.`

GitHub Variables

Commit Sha

Variable / Context Push Events Pull Request Events What does it point to?
github.sha Yes Yes push: The latest commit on the branch.
pull_request: A temporary ⚠️, simulated merge commit (refs/pull/PR_NUMBER/merge).
github.event.pull_request.head.sha No (Null) Yes The actual latest commit pushed to the source/feature branch of the PR.
github.event.push.after / github.event.after Yes No (Null) The HEAD commit SHA of the branch after the push was executed.

Ref

Variable / Context Push Events Pull Request Events Availability
github.ref Branch or tag pushed (refs/heads/main) PR merge ref (refs/pull/:num/merge) Always available
github.head_ref Empty ("") Source branch name (e.g., feature-branch) Only on pull_request / pull_request_target

Results

github-actions.mdx

  • All three create-release examples now use git_ref: ${{ github.head_ref || github.ref }} and git_commit: ${{ github.event.pull_request.head.sha || github.sha }}. Both expressions fall back to the push values on every other event, so the examples still work unchanged for workflow_dispatch, push, and schedule.
  • Added a hint after the first example explaining why the pull request values differ.
  • Expanded the git_ref and git_commit input descriptions to say the same thing in the table.

creating-release-from-a-build-server-plug-in.mdx

  • Fixed the Git Commit expression. It read ${{ github.event.push.after || github.event.pull_request.head.sha }}, but a push payload exposes the commit as github.event.after, so github.event.push.after was always empty and a push fell through to the pull request value. It now reads ${{ github.event.pull_request.head.sha || github.sha }}.
  • Replaced the note about manually triggered runs, because the new expression covers them.

Testing

Documentation only, no code changes. Both files render as MDX with existing syntax, and the hint uses the same :::div{.hint} block already used on the TeamCity page.

Verified the underlying claim against the live GitHub API for the pull request in the original report. The merge commit exists only on refs/pull/282/merge, and the branch head is a different commit.

🤖 Generated with Claude Code

On a pull_request event, github.ref is refs/pull/<number>/merge and
github.sha is the merge commit GitHub creates for the pull request.
That commit is not reachable from any branch or tag, so an Octopus
release pinned to it can fail to deploy later.

Switch the three create-release examples to github.head_ref and
github.event.pull_request.head.sha, which fall back to the push values
on every other event, and add a note explaining why.

Also fix github.event.push.after on the build server plugin page. A
push payload exposes the commit as github.event.after, so the old
expression was always empty on a push.
@team-marketing-branch-protections

Copy link
Copy Markdown

Pull request environment is available at https://stoctodocspr3462.z22.web.core.windows.net/.

You can view the ephemeral environment status in Octopus Deploy.

This environment will be automatically deprovisioned when the pull request is closed, or after 7 days of inactivity.

@zentron zentron left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating our docs around GitHub references to use for CaC projects

MD060 requires table pipes to align with the header. The longer
git_ref and git_commit descriptions pushed those two rows past the
width of the rest of the table. The hint above the table already
explains the pull request behavior, so revert the two cells.
@zentron
zentron merged commit c91433e into main Sep 16, 2026
7 checks passed
@zentron
zentron deleted the robe/github-actions-pull-request-git-ref branch September 16, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants