Skip to content

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

@alexaka1

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

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