Skip to content

Action fails: Cannot find detached HEAD ref in "HEAD" #552

Closed
docker/actions-toolkit
#856

Description

@alexaka1

Contributing guidelines

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

  1. crazy-max commented on Nov 4, 2025

    @crazy-max
    Member
  2. crazy-max commented on Nov 27, 2025

    @crazy-max
    Member

    Should be fixed in latest v5.10.0

  3. alexaka1 commented on Dec 1, 2025

    @alexaka1
    Author

    @crazy-max Hi. I just tried it out, now I get this error:

    Cannot find detached HEAD ref in "grafted, HEAD"
    

    The only thing I have changed since reporting is fetch-depth: 1 is used instead of zero.

    Run docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051
      with:
        context: git
        images: name=***.***/redacted
      
        labels: redacted
      
        flavor: latest=false
      suffix=,onlatest=true
      
        tags: type=raw,value=latest,enable=false,priority=1001
      type=ref,event=branch,enable=false
      type=ref,event=tag,enable=false
      type=schedule,enable=false
      type=sha,enable=true,prefix=sha-
      
        github-token: ***
      env:
        redacted...
        GIT_TIMESTAMP: 1764600778
        GIT_SHA: 98c56f6fe00d12575603288b9e4e1bd02fe12ad9
        DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
    Error: Cannot find detached HEAD ref in "grafted, HEAD"
    
  4. neilime commented on Dec 1, 2025

    @neilime

    @crazy-max @alexaka1 I will check that really soon

  5. neilime commented on Dec 23, 2025

    @neilime
  6. alexaka1 commented on Jan 30, 2026

    @alexaka1
    Author

    @crazy-max @neilime Can we reopen this issue? I still get the same error:
    Error: Cannot find detached HEAD ref in "grafted, HEAD" for docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0

  7. crazy-max commented on Jan 30, 2026

    @crazy-max
    Member

    @alexaka1 Has been fixed but not released yet, keep you posted

  8. alexaka1 commented on Sep 10, 2026

    @alexaka1
    Author

    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.

  9. alexaka1 commented on Sep 10, 2026

    @alexaka1
    Author

    Could we reopen this for the shallow SHA-checkout case? I still reproduce it with v6.2.0, context: git, and fetch-depth: 1.

    I put together a minimal repro with three jobs checking out the same commit:

    • Branch ref: passes.
    • Tag ref: passes.
    • Commit SHA: checkout succeeds, but metadata generation fails with 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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions