Skip to content

fix: resolve {sha} in custom image tag to the tag commit for webhook tag builds (#7092) - #7099

Open
prakash100198 wants to merge 1 commit into
developfrom
fix/custom-tag-sha-webhook-tag-oss
Open

prakash100198 wants to merge 1 commit into
developfrom
fix/custom-tag-sha-webhook-tag-oss

Conversation

@prakash100198

@prakash100198 prakash100198 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #7092

Enterprise counterpart (identical diff): https://github.com/devtron-labs/devtron-enterprise/pull/3454

With a custom image tag such as dev-{sha}-{x} (#7060, #7071), a CI build triggered by a tag creation webhook got the tag name in place of the commit. Pushing release/1.2 produced dev-release_-<x>, and release/1.3 gave the same release_, so the image could not be traced back to a commit.

Cause: webhook builds carry no commit on the material, so getCommitShaForCustomTag fell back to the webhook's target checkout value. For tag creation events git-sensor fills target checkout with the tag name: GitHub ref, Bitbucket push.changes.0.new.name. That value was cut to 8 characters, with / replaced by _.

Fix (pkg/build/trigger/HandlerService.go):

  • When the material has no commit and the webhook event is not a merged PR (the existing code's definition of a tag build), the tag in target checkout is resolved to the commit it points to. The lookup uses git-sensor commit-metadata with gitTag, which fetches the repo (--tags --force) before resolving.
  • The lookup runs only when the custom tag pattern contains {sha}/{SHA}. Builds without {sha} make no extra git-sensor call.
  • If the tag can't be resolved, {sha} is empty and the existing commit sha not found for variable {sha} in image tag error fails the trigger, instead of the image being tagged with the tag name.

Unchanged:

  • Manual and branch builds use the material commit.
  • PR-merged webhook events use the target branch commit already fetched at trigger time.
  • GitLab tag events: their target checkout is checkout_sha, and GitLab marks them as merged.
  • The legacy image tag (custom tag disabled), and custom tag patterns without {sha}.

Known limitation: annotated tags. git-sensor's tag lookup does not peel annotated tags. I checked this by running git-sensor's own GetCommitsForTag on a repo with one annotated and one lightweight tag:

tag go-git mode (default) git CLI mode
lightweight correct commit correct commit
annotated object not found JSON parse error: git show <tag> prints the tag header before the commit

With this PR, an annotated tag build whose pattern uses {sha} fails with the missing-sha error above, where today it gets a tag-name based image tag. Making annotated tags resolve needs a small git-sensor change: peel the tag object to its commit (TagObject(...).Commit() in go-git, <tag>^{commit} in the CLI). That will be a separate PR.

How Has This Been Tested?

New unit tests in pkg/build/trigger/HandlerService_customTagSha_test.go:

  • Manual/branch build: commit from the material, no git-sensor call
  • Tag webhook (release/1.2): tag resolved, {sha} = first 8 chars of the tag commit
  • Two tags with the same 8 char prefix (release/1.2, release/1.3): each gets its own commit
  • Unresolvable tag: empty {sha}, so the existing missing-sha error fires
  • PR merged webhook: target checkout commit used, no tag lookup
  • Webhook without target checkout: empty {sha}, no lookup
  • Dockerfile in the second git material: that material's commit is used; no matching material, or no commit data: empty {sha}
  • Tag lookup sends pipelineMaterialId + gitTag and handles a git-sensor error and an empty result
  • Real git-sensor REST client against a fake commit-metadata endpoint: request is POST /commit-metadata with {"pipelineMaterialId", "gitTag"}; 200 with a commit, 400 not found, and a 200 null result are handled
  • {sha} / {SHA} detection for the lookup guard

All existing tests in pkg/build/trigger give the same result before and after this change. TestUpdateWorkflowRequestWithBuildxFlags already panics on develop and is unrelated.

These tests ran on the enterprise build, which has the same change and test file. This branch was not compiled locally; the identifiers it uses were checked against this repo.

Checklist:

  • The title of the PR states what changed and the related issues number (used for the release note).
  • Does this PR requires documentation updates?
  • I've updated documentation as required by this PR.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have tested it for all user roles.
  • I have added all the required unit/api test cases.

Does this PR introduce a user-facing change?

{sha} in custom image tags now resolves to the commit the tag points to for builds triggered by tag creation webhooks, instead of the tag name. If the commit can't be resolved (for example annotated tags), the build fails with "commit sha not found for variable {sha} in image tag".

🤖 Generated with Claude Code

…tag builds

On tag-creation webhook builds the material has no commit, so {sha} fell
back to the webhook target checkout, which is the tag name for GitHub and
Bitbucket (e.g. release/1.2 became "release_"). Resolve the tag to its
commit through git-sensor when the custom tag pattern uses {sha}. If the
tag cannot be resolved, {sha} stays empty and the existing "commit sha not
found" error fails the trigger instead of producing a misleading tag.
PR-merged events and manual builds are unchanged.

Fixes #7092

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bito-code-review

Copy link
Copy Markdown

Bito Automatic Review Skipped - Branch Excluded

Bito didn't auto-review because the source or target branch is excluded from automatic reviews. To trigger review, type /review in the comment and save. You can change the settings here, or contact the agent instance creator at shivam@devtron.ai.

@github-actions

Copy link
Copy Markdown

Some linked issues are invalid. Please update the issue links:\nIssue #7092 in devtron-labs/devtron is not found or invalid (HTTP 403).\n

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant