Repository navigation
Closed
docker/actions-toolkit
#856Description
Contributing guidelines
- I've read the contributing guidelines and wholeheartedly agree
I've found a bug, and:
- The documentation does not mention anything about my problem
- There are no open or closed issues that are related to my problem
Description
I have this simple workflow. In the real project this workflow also can run on a schedule, with a default branch. But for manual dispatch events the user can input a ref as the base of the checkout. This can be a branch, tag or a SHA. However, it does not work for SHAs. I get an error.
I am using the git context, since the pipeline is in a different repo than what I actually want to use for metadata-action.
I have found one similar closed issue, but it was never reopened after new comments were posted. #362
Expected behaviour
The workflow works for commit SHAs
Actual behaviour
The workflow does not work for commit SHAs
Repository URL
No response
Workflow run URL
No response
YAML workflow
name: 'some workflow'
on:
workflow_dispatch:
inputs:
branch:
description: 'The `some-repo` branch to use'
required: true
default: 'main'
type: string
permissions: { }
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
env:
CI: 'true'
DOTNET_NOLOGO: 'true'
DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
DO_NOT_TRACK: '1'
REPOSITORY: 'some-repo'
jobs:
publish-dev:
runs-on: ubuntu-latest
permissions:
contents: read
env:
BRANCH: ${{ github.event.inputs.branch || 'main' }}
steps:
- name: Generate Github App token to clone submodules
uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4
id: app-token
with:
app-id: ${{ secrets.SUBMODULE_APP_ID }}
private-key: ${{ secrets.SUBMODULE_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: |
${{ env.REPOSITORY }}
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
repository: '${{ github.repository_owner }}/${{ env.REPOSITORY }}'
ref: ${{ env.BRANCH }}
persist-credentials: false
token: ${{ steps.app-token.outputs.token }}
lfs: true
fetch-tags: true
fetch-depth: 0
- name: Get Git commit timestamp
run: |
echo "GIT_TIMESTAMP=$(git log -1 --pretty=%ct)" >> "$GITHUB_ENV"
echo "GIT_SHA=$(git log -1 --format=%H)" >> "$GITHUB_ENV"
- name: Determine if branch is a SHA
id: check-sha
run: |
if git show-ref --verify -- refs/heads/${{ env.BRANCH }} >/dev/null 2>&1; then
echo "is_sha=false" >> "$GITHUB_OUTPUT"
elif git show-ref --verify -- refs/tags/${{ env.BRANCH }} >/dev/null 2>&1; then
echo "is_sha=false" >> "$GITHUB_OUTPUT"
else
# Check if it's a valid commit SHA
if git rev-parse --verify ${{ env.BRANCH }} >/dev/null 2>&1; then
echo "is_sha=true" >> "$GITHUB_OUTPUT"
else
echo "is_sha=false" >> "$GITHUB_OUTPUT"
fi
fi
- name: Docker meta
id: meta
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0
with:
context: 'git'
images: |
name=some-image
tags: |
type=raw,value=latest,enable=${{ env.BRANCH == 'main' && 'true' || 'false' }},priority=1001
type=ref,event=branch,enable=${{ !fromJSON(steps.check-sha.outputs.is_sha) }}
type=ref,event=tag,enable=${{ !fromJSON(steps.check-sha.outputs.is_sha) }}
type=schedule,enable=${{ github.event_name == 'schedule' }}
type=sha,enable=${{ fromJSON(steps.check-sha.outputs.is_sha) }},prefix=sha-
env:
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
- name: Export metadata to env
run: |
META_JSON='${{ steps.meta.outputs.json }}'Workflow logs
Error: Cannot find detached HEAD ref in "HEAD"
BuildKit logs
Additional info
No response
Activity
crazy-max commentedon Nov 4, 2025
cc @neilime
crazy-max commentedon Nov 27, 2025
Should be fixed in latest
v5.10.0alexaka1 commentedon Dec 1, 2025
@crazy-max Hi. I just tried it out, now I get this error:
The only thing I have changed since reporting is
fetch-depth: 1is used instead of zero.neilime commentedon Dec 1, 2025
@crazy-max @alexaka1 I will check that really soon
neilime commentedon Dec 23, 2025
@crazy-max docker/actions-toolkit#915
alexaka1 commentedon Jan 30, 2026
@crazy-max @neilime Can we reopen this issue? I still get the same error:
Error: Cannot find detached HEAD ref in "grafted, HEAD"fordocker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0crazy-max commentedon Jan 30, 2026
@alexaka1 Has been fixed but not released yet, keep you posted
alexaka1 commentedon Sep 10, 2026
Sorry for not testing it sooner. It's very disorienting that the issue is closed many months before the fix actually lands in a release. However, the issue is not fixed. I am creating a public reproduction right now.
alexaka1 commentedon Sep 10, 2026
Could we reopen this for the shallow SHA-checkout case? I still reproduce it with v6.2.0,
context: git, andfetch-depth: 1.I put together a minimal repro with three jobs checking out the same commit:
Cannot infer ref from detached HEAD.Here’s the completed run.
The SHA case uses
type=sha,prefix=sha-,format=short. It only needs to generate a tag from the checked-out commit, but fails during ref inference before reaching that step. I’d like to keep the shallow checkout, since fetching the full history is expensive for large repositories.