Skip to content

Commit c3feea5

Browse files
committed
Follow up on GitHub tag build documentation and ref classification
1 parent 03ac0ff commit c3feea5

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

docs/input/docs/reference/build-servers/github-actions.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ without it, GitHub Actions might perform a shallow clone, which will cause GitVe
1717
:::
1818

1919
More information can be found at [gittools/actions](https://github.com/GitTools/actions/blob/main/docs/examples/github/gitversion/index.md).
20+
21+
## Branch and tag builds
22+
23+
GitVersion uses the following [GitHub Actions default environment variables](https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables) to identify the ref being built:
24+
25+
| Variable | How GitVersion uses it |
26+
| --- | --- |
27+
| `GITHUB_REF_TYPE` | Distinguishes tag builds (`tag`) from branch builds (`branch`). |
28+
| `GITHUB_REF` | Supplies the full ref, such as `refs/heads/main`, `refs/pull/42/merge`, or `refs/tags/1.2.3`. |
29+
30+
For a tag build, GitVersion treats `GITHUB_REF` as the selected tag rather than a branch name. This also applies when manually running a workflow with `workflow_dispatch` and selecting a tag.
31+
32+
A historical tag can point to a commit that is no longer the tip of any branch. If normalization cannot attach HEAD to a local branch, it checks the selected local tag. When that tag resolves to the checked-out commit, GitVersion keeps HEAD detached and avoids an unnecessary remote lookup for pull-request refs. Existing branch selection takes precedence when a local branch identifies HEAD.
33+
34+
The selected tag must exist locally and resolve to HEAD for this behavior to apply. Checking out a different ref from the one reported by `GITHUB_REF` does not make an unrelated local tag identify the build. Keep `fetch-depth: 0` as described above so GitVersion has the full history available for version calculation.

src/GitVersion.BuildAgents/Agents/GitHubActions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO.Abstractions;
22
using GitVersion.Extensions;
3+
using GitVersion.Git;
34
using GitVersion.OutputVariables;
45

56
namespace GitVersion.Agents;
@@ -64,7 +65,7 @@ public override void WriteIntegration(Action<string?> writer, GitVersionVariable
6465
var reference = this.environment.GetEnvironmentVariable("GITHUB_REF");
6566
return string.Equals(refType, "tag", StringComparison.OrdinalIgnoreCase)
6667
&& reference != null
67-
&& reference.StartsWith("refs/tags/", StringComparison.Ordinal)
68+
&& new ReferenceName(reference).IsTag
6869
? reference
6970
: null;
7071
}

0 commit comments

Comments
 (0)