Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action fails: Cannot find detached HEAD ref in "grafted, HEAD, tag: x.x.x" #362

Closed
3 tasks done
Programie opened this issue Dec 3, 2023 · 10 comments · Fixed by docker/actions-toolkit#217 or docker/actions-toolkit#224
Labels

Comments

@Programie
Copy link

Programie commented Dec 3, 2023

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

Since version v5.1.0, the action fails with the following error whenever a specific git tag is checked out in combination with using context: git:

Error: Cannot find detached HEAD ref in "grafted, HEAD, tag: 1.0.0"

The issue does not exist with version v5.0.0 of the action. I also can't reproduce it when checking out the default branch or any other branch. It only seems to be broken for tags.

Expected behaviour

The action completes without any issues and provides the expected tags for the image.

Actual behaviour

The action fails with the following error message:

Error: Cannot find detached HEAD ref in "grafted, HEAD, tag: 1.0.0"

Repository URL

https://github.com/Programie/PHP-Docker

Workflow run URL

https://github.com/Programie/PHP-Docker/actions/runs/7073444694

YAML workflow

name: Build

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: 1.0.0

      - uses: docker/[email protected]
        with:
          context: git

Workflow logs

2023-12-03T07:52:59.1533184Z ##[group]Run docker/metadata-action@v5
2023-12-03T07:52:59.1533673Z with:
2023-12-03T07:52:59.1534229Z   images: ghcr.io/***/php-docker
***/php

2023-12-03T07:52:59.1534686Z   context: git
2023-12-03T07:52:59.1535449Z   tags: type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=ref,event=branch

2023-12-03T07:52:59.1536367Z   github-token: ***
2023-12-03T07:52:59.1536669Z env:
2023-12-03T07:52:59.1537036Z   USE_CACHE: false
2023-12-03T07:52:59.1537464Z   GHCR_IMAGE: ghcr.io/***/php-docker
2023-12-03T07:52:59.1537885Z ##[endgroup]
2023-12-03T07:52:59.3841670Z ##[error]Cannot find detached HEAD ref in "grafted, HEAD, tag: 1.0.0"

BuildKit logs

2023-12-03T07:52:53.8211749Z ##[group]Run docker/setup-buildx-action@v3
2023-12-03T07:52:53.8212279Z with:
2023-12-03T07:52:53.8212693Z   driver: docker-container
2023-12-03T07:52:53.8213413Z   buildkitd-flags: --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host
2023-12-03T07:52:53.8214266Z   install: false
2023-12-03T07:52:53.8214565Z   use: true
2023-12-03T07:52:53.8214901Z   cleanup: true
2023-12-03T07:52:53.8215275Z env:
2023-12-03T07:52:53.8215590Z   USE_CACHE: false
2023-12-03T07:52:53.8216089Z   GHCR_IMAGE: ghcr.io/***/php-docker
2023-12-03T07:52:53.8216581Z ##[endgroup]
2023-12-03T07:52:53.9719926Z ##[group]Docker info
2023-12-03T07:52:53.9759966Z [command]/usr/bin/docker version
2023-12-03T07:52:54.0562752Z Client: Docker Engine - Community
2023-12-03T07:52:54.0564358Z  Version:           24.0.7
2023-12-03T07:52:54.0565409Z  API version:       1.43
2023-12-03T07:52:54.0566393Z  Go version:        go1.20.10
2023-12-03T07:52:54.0567692Z  Git commit:        afdd53b
2023-12-03T07:52:54.0568566Z  Built:             Thu Oct 26 09:07:41 2023
2023-12-03T07:52:54.0569581Z  OS/Arch:           linux/amd64
2023-12-03T07:52:54.0570261Z  Context:           default
2023-12-03T07:52:54.0571581Z 
2023-12-03T07:52:54.0572236Z Server: Docker Engine - Community
2023-12-03T07:52:54.0575170Z  Engine:
2023-12-03T07:52:54.0575822Z   Version:          24.0.7
2023-12-03T07:52:54.0576891Z   API version:      1.43 (minimum version 1.12)
2023-12-03T07:52:54.0577953Z   Go version:       go1.20.10
2023-12-03T07:52:54.0578662Z   Git commit:       311b9ff
2023-12-03T07:52:54.0582030Z   Built:            Thu Oct 26 09:07:41 2023
2023-12-03T07:52:54.0582860Z   OS/Arch:          linux/amd64
2023-12-03T07:52:54.0583538Z   Experimental:     false
2023-12-03T07:52:54.0584180Z  containerd:
2023-12-03T07:52:54.0584684Z   Version:          1.6.25
2023-12-03T07:52:54.0585434Z   GitCommit:        d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
2023-12-03T07:52:54.0586311Z  runc:
2023-12-03T07:52:54.0586748Z   Version:          1.1.10
2023-12-03T07:52:54.0587543Z   GitCommit:        v1.1.10-0-g18a0cb0
2023-12-03T07:52:54.0588215Z  docker-init:
2023-12-03T07:52:54.0588553Z   Version:          0.19.0
2023-12-03T07:52:54.0589026Z   GitCommit:        de40ad0
2023-12-03T07:52:54.0609873Z [command]/usr/bin/docker info
2023-12-03T07:52:54.2873841Z Client: Docker Engine - Community
2023-12-03T07:52:54.2874680Z  Version:    24.0.7
2023-12-03T07:52:54.2875486Z  Context:    default
2023-12-03T07:52:54.2876000Z  Debug Mode: false
2023-12-03T07:52:54.2876958Z  Plugins:
2023-12-03T07:52:54.2877710Z   buildx: Docker Buildx (Docker Inc.)
2023-12-03T07:52:54.2878426Z     Version:  v0.11.2
2023-12-03T07:52:54.2880432Z     Path:     /usr/libexec/docker/cli-plugins/docker-buildx
2023-12-03T07:52:54.2881783Z   compose: Docker Compose (Docker Inc.)
2023-12-03T07:52:54.2882463Z     Version:  v2.23.3
2023-12-03T07:52:54.2883349Z     Path:     /usr/libexec/docker/cli-plugins/docker-compose
2023-12-03T07:52:54.2883767Z 
2023-12-03T07:52:54.2883920Z Server:
2023-12-03T07:52:54.2884220Z  Containers: 0
2023-12-03T07:52:54.2884680Z   Running: 0
2023-12-03T07:52:54.2885010Z   Paused: 0
2023-12-03T07:52:54.2885396Z   Stopped: 0
2023-12-03T07:52:54.2885741Z  Images: 16
2023-12-03T07:52:54.2886046Z  Server Version: 24.0.7
2023-12-03T07:52:54.2886523Z  Storage Driver: overlay2
2023-12-03T07:52:54.2886921Z   Backing Filesystem: extfs
2023-12-03T07:52:54.2887306Z   Supports d_type: true
2023-12-03T07:52:54.2887720Z   Using metacopy: false
2023-12-03T07:52:54.2888105Z   Native Overlay Diff: false
2023-12-03T07:52:54.2888517Z   userxattr: false
2023-12-03T07:52:54.2888998Z  Logging Driver: json-file
2023-12-03T07:52:54.2889381Z  Cgroup Driver: cgroupfs
2023-12-03T07:52:54.2889765Z  Cgroup Version: 2
2023-12-03T07:52:54.2890129Z  Plugins:
2023-12-03T07:52:54.2890439Z   Volume: local
2023-12-03T07:52:54.2890869Z   Network: bridge host ipvlan macvlan null overlay
2023-12-03T07:52:54.2891672Z   Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
2023-12-03T07:52:54.2892685Z  Swarm: inactive
2023-12-03T07:52:54.2893016Z  Runtimes: io.containerd.runc.v2 runc
2023-12-03T07:52:54.2893525Z  Default Runtime: runc
2023-12-03T07:52:54.2893949Z  Init Binary: docker-init
2023-12-03T07:52:54.2894738Z  containerd version: d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
2023-12-03T07:52:54.2895367Z  runc version: v1.1.10-0-g18a0cb0
2023-12-03T07:52:54.2895801Z  init version: de40ad0
2023-12-03T07:52:54.2896229Z  Security Options:
2023-12-03T07:52:54.2896580Z   apparmor
2023-12-03T07:52:54.2896899Z   seccomp
2023-12-03T07:52:54.2897242Z    Profile: builtin
2023-12-03T07:52:54.2897596Z   cgroupns
2023-12-03T07:52:54.2897956Z  Kernel Version: 6.2.0-1016-azure
2023-12-03T07:52:54.2898445Z  Operating System: Ubuntu 22.04.3 LTS
2023-12-03T07:52:54.2898874Z  OSType: linux
2023-12-03T07:52:54.2899191Z  Architecture: x86_64
2023-12-03T07:52:54.2899795Z  CPUs: 4
2023-12-03T07:52:54.2900350Z  Total Memory: 15.61GiB
2023-12-03T07:52:54.2900990Z  Name: fv-az802-647
2023-12-03T07:52:54.2901501Z  ID: 8fd12658-c561-4704-8f40-f8581cdb93b9
2023-12-03T07:52:54.2901979Z  Docker Root Dir: /var/lib/docker
2023-12-03T07:52:54.2902514Z  Debug Mode: false
2023-12-03T07:52:54.2902960Z  Username: githubactions
2023-12-03T07:52:54.2903419Z  Experimental: false
2023-12-03T07:52:54.2903935Z  Insecure Registries:
2023-12-03T07:52:54.2904585Z   127.0.0.0/8
2023-12-03T07:52:54.2905095Z  Live Restore Enabled: false
2023-12-03T07:52:54.2905461Z 
2023-12-03T07:52:54.2906295Z ##[endgroup]
2023-12-03T07:52:54.3520662Z ##[group]Buildx version
2023-12-03T07:52:54.3545575Z [command]/usr/bin/docker buildx version
2023-12-03T07:52:54.4054558Z github.com/docker/buildx v0.11.2 9872040
2023-12-03T07:52:54.4083361Z ##[endgroup]
2023-12-03T07:52:54.4092115Z ##[group]Creating a new builder instance
2023-12-03T07:52:54.4794965Z [command]/usr/bin/docker buildx create --name builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b77 --driver docker-container --buildkitd-flags --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host --use
2023-12-03T07:52:54.5505121Z builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b77
2023-12-03T07:52:54.5539362Z ##[endgroup]
2023-12-03T07:52:54.5540597Z ##[group]Booting builder
2023-12-03T07:52:54.5605458Z [command]/usr/bin/docker buildx inspect --bootstrap --builder builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b77
2023-12-03T07:52:54.6287135Z #1 [internal] booting buildkit
2023-12-03T07:52:54.7789016Z #1 pulling image moby/buildkit:buildx-stable-1
2023-12-03T07:52:55.3298995Z #1 pulling image moby/buildkit:buildx-stable-1 0.7s done
2023-12-03T07:52:55.4804637Z #1 creating container buildx_buildkit_builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b770
2023-12-03T07:52:57.3779202Z #1 creating container buildx_buildkit_builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b770 2.0s done
2023-12-03T07:52:57.3780445Z #1 DONE 2.7s
2023-12-03T07:52:57.4867629Z Name:          builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b77
2023-12-03T07:52:57.4868636Z Driver:        docker-container
2023-12-03T07:52:57.4869407Z Last Activity: 2023-12-03 07:52:54 +0000 UTC
2023-12-03T07:52:57.4869996Z 
2023-12-03T07:52:57.4870175Z Nodes:
2023-12-03T07:52:57.4870908Z Name:      builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b770
2023-12-03T07:52:57.4871747Z Endpoint:  unix:///var/run/docker.sock
2023-12-03T07:52:57.4872498Z Status:    running
2023-12-03T07:52:57.4873680Z Flags:     --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host
2023-12-03T07:52:57.4874771Z Buildkit:  v0.12.3
2023-12-03T07:52:57.4875590Z Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386
2023-12-03T07:52:57.4876441Z Labels:
2023-12-03T07:52:57.4877163Z  org.mobyproject.buildkit.worker.executor:         oci
2023-12-03T07:52:57.4878215Z  org.mobyproject.buildkit.worker.hostname:         e8c5ce71d83f
2023-12-03T07:52:57.4879404Z  org.mobyproject.buildkit.worker.network:          host
2023-12-03T07:52:57.4880641Z  org.mobyproject.buildkit.worker.oci.process-mode: sandbox
2023-12-03T07:52:57.4881713Z  org.mobyproject.buildkit.worker.selinux.enabled:  false
2023-12-03T07:52:57.4883117Z  org.mobyproject.buildkit.worker.snapshotter:      overlayfs
2023-12-03T07:52:57.4884118Z GC Policy rule#0:
2023-12-03T07:52:57.4884598Z  All:           false
2023-12-03T07:52:57.4885430Z  Filters:       type==source.local,type==exec.cachemount,type==source.git.checkout
2023-12-03T07:52:57.4886538Z  Keep Duration: 48h0m0s
2023-12-03T07:52:57.4887127Z  Keep Bytes:    488.3MiB
2023-12-03T07:52:57.4887755Z GC Policy rule#1:
2023-12-03T07:52:57.4888324Z  All:           false
2023-12-03T07:52:57.4888861Z  Keep Duration: 1440h0m0s
2023-12-03T07:52:57.4889437Z  Keep Bytes:    8.382GiB
2023-12-03T07:52:57.4890090Z GC Policy rule#2:
2023-12-03T07:52:57.4890604Z  All:        false
2023-12-03T07:52:57.4891107Z  Keep Bytes: 8.382GiB
2023-12-03T07:52:57.4891787Z GC Policy rule#3:
2023-12-03T07:52:57.4892307Z  All:        true
2023-12-03T07:52:57.4893213Z  Keep Bytes: 8.382GiB
2023-12-03T07:52:57.4918386Z ##[endgroup]
2023-12-03T07:52:57.6274269Z ##[group]Inspect builder
2023-12-03T07:52:57.6327957Z {
2023-12-03T07:52:57.6328497Z   "nodes": [
2023-12-03T07:52:57.6329143Z     {
2023-12-03T07:52:57.6331425Z       "name": "builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b770",
2023-12-03T07:52:57.6332474Z       "endpoint": "unix:///var/run/docker.sock",
2023-12-03T07:52:57.6333186Z       "status": "running",
2023-12-03T07:52:57.6334500Z       "buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
2023-12-03T07:52:57.6335616Z       "buildkit": "v0.12.3",
2023-12-03T07:52:57.6336317Z       "platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/386",
2023-12-03T07:52:57.6337214Z       "labels": {
2023-12-03T07:52:57.6337830Z         "org.mobyproject.buildkit.worker.executor": "oci",
2023-12-03T07:52:57.6338821Z         "org.mobyproject.buildkit.worker.hostname": "e8c5ce71d83f",
2023-12-03T07:52:57.6339478Z         "org.mobyproject.buildkit.worker.network": "host",
2023-12-03T07:52:57.6340328Z         "org.mobyproject.buildkit.worker.oci.process-mode": "sandbox",
2023-12-03T07:52:57.6341059Z         "org.mobyproject.buildkit.worker.selinux.enabled": "false",
2023-12-03T07:52:57.6341721Z         "org.mobyproject.buildkit.worker.snapshotter": "overlayfs"
2023-12-03T07:52:57.6342275Z       },
2023-12-03T07:52:57.6342601Z       "gcPolicy": [
2023-12-03T07:52:57.6343203Z         {
2023-12-03T07:52:57.6343628Z           "all": false,
2023-12-03T07:52:57.6343980Z           "filter": [
2023-12-03T07:52:57.6344304Z             "type==source.local",
2023-12-03T07:52:57.6344797Z             "type==exec.cachemount",
2023-12-03T07:52:57.6345242Z             "type==source.git.checkout"
2023-12-03T07:52:57.6345725Z           ],
2023-12-03T07:52:57.6346043Z           "keepDuration": "48h0m0s",
2023-12-03T07:52:57.6346460Z           "keepBytes": "488.3MiB"
2023-12-03T07:52:57.6346948Z         },
2023-12-03T07:52:57.6347271Z         {
2023-12-03T07:52:57.6347576Z           "all": false,
2023-12-03T07:52:57.6347989Z           "keepDuration": "1440h0m0s",
2023-12-03T07:52:57.6348425Z           "keepBytes": "8.382GiB"
2023-12-03T07:52:57.6348801Z         },
2023-12-03T07:52:57.6349190Z         {
2023-12-03T07:52:57.6349496Z           "all": false,
2023-12-03T07:52:57.6349820Z           "keepBytes": "8.382GiB"
2023-12-03T07:52:57.6350278Z         },
2023-12-03T07:52:57.6350574Z         {
2023-12-03T07:52:57.6350906Z           "all": true,
2023-12-03T07:52:57.6351289Z           "keepBytes": "8.382GiB"
2023-12-03T07:52:57.6351662Z         }
2023-12-03T07:52:57.6351969Z       ]
2023-12-03T07:52:57.6352313Z     }
2023-12-03T07:52:57.6352596Z   ],
2023-12-03T07:52:57.6353050Z   "name": "builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b77",
2023-12-03T07:52:57.6353661Z   "driver": "docker-container",
2023-12-03T07:52:57.6354148Z   "lastActivity": "2023-12-03T07:52:54.000Z"
2023-12-03T07:52:57.6354630Z }
2023-12-03T07:52:57.6355174Z ##[endgroup]
2023-12-03T07:52:57.6355788Z ##[group]BuildKit version
2023-12-03T07:52:57.6356303Z builder-52329fe5-a25e-49d4-89ca-1dbcb7da1b770: v0.12.3
2023-12-03T07:52:57.6357208Z ##[endgroup]

Additional info

No response

@crazy-max
Copy link
Member

Looks related to docker/actions-toolkit#173, cc @neilime

@neilime
Copy link

neilime commented Dec 3, 2023

Looks related to docker/actions-toolkit#173, cc @neilime

Looks like it occurs when doing a shallow clone (using the --depth option).

I've made a fix to handle these case: docker/actions-toolkit#217

@crazy-max
Copy link
Member

crazy-max commented Dec 4, 2023

@Programie Should be fixed with latest metadata-action release.

@Programie
Copy link
Author

I just tested the new version and it is working again.

Thanks for the fix. 👍

@achille-roussel
Copy link

Hello,

This issue might still be occuring in some cases, I tried v5.3.0 of the metadata action and if I set context: git I get this error:

Error: Unsupported detached HEAD ref in "grafted, HEAD, pull/221/merge"

The reason I'm trying to use context: git is because the tags selected by tags=sha don't seem to match the git commit sha, unless running on a merge into the main branch.

The simplest configuration I used to trigger the issue is:

    - uses: actions/checkout@v4
    - uses: docker/metadata-action@v5
      id: meta
      with:
        images: ...
        context: git
        tags: |
          type=semver,pattern={{version}}
          type=sha,prefix=

@neilime
Copy link

neilime commented Dec 12, 2023

grafted, HEAD, pull/221/merge

@achille-roussel I'm curious about which event trigger this case (in your workflow, on: xxx)

@achille-roussel
Copy link

It's a pull request event.

@neilime
Copy link

neilime commented Dec 12, 2023

@achille-roussel can you share the workflow run url if it's possible (or the yml if not)

@achille-roussel
Copy link

This is from a private repository so I can't share the file as-is, but I'm attaching an altered version of the whole workflow (with private details removed).

This is the fixed version where I switched back to context: workflow and fixed the commit sha by extracting it from different places depending on whether the event is a pull request or a push. This meant I also had to switch from tag=sha to tag=raw.

Setting context: git and tag=sha on the metadata action will trigger the error:

name: build
on:
  push:
    tags:
      - v*
    branches:
    - main
  pull_request:
    branches:
    - main

concurrency:
  group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
    - uses: aws-actions/configure-aws-credentials@v4
      with:
        role-to-assume: <ARN>
        aws-region: <REGION>
    - uses: aws-actions/amazon-ecr-login@v2
      id: ecr

    - run: echo "version=${VERSION:0:7}" >> $GITHUB_OUTPUT
      env:
        VERSION: ${{ github.event.pull_request.head.sha || github.sha }}
      id: vars

    - uses: actions/checkout@v4
    - uses: docker/metadata-action@v5
      id: meta
      with:
        images: ${{ steps.ecr.outputs.registry }}/<IMAGE>
        tags: |
          type=semver,pattern={{version}}
          type=raw,value=${{ steps.vars.outputs.version }}
    - uses: docker/setup-buildx-action@v3
    - uses: docker/build-push-action@v5
      with:
        context: .
        cache-from: type=registry,ref=${{ steps.ecr.outputs.registry }}/<IMAGE>
        cache-to: type=gha,mode=max
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        platforms: linux/arm64
        push: true

@mmenozzi
Copy link

mmenozzi commented Jan 3, 2024

I'm also still experiencing this issue.
This is my workflow:

on:
    workflow_dispatch:
        inputs:
            sha:
                description: 'Commit SHA'
                type: string
                required: true
            push:
                description: 'Push images'
                type: boolean
                required: true

jobs:
    build:
        name: Build Docker containers
        timeout-minutes: 15
        runs-on: ubuntu-latest

        steps:
            -   uses: actions/checkout@v2
                with:
                    ref: ${{ github.event.inputs.sha }}

            -
                name: Docker metadata for PHP image
                id: php_meta
                uses: docker/metadata-action@v5
                with:
                    context: git
                    images: ...
                    tags: |
                        type=raw,priority=100,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
                        type=ref,priority=200,event=branch
                        type=sha,priority=300,format=long,prefix=,suffix=
                    flavor: |
                        latest=false
                        prefix=
                        suffix=

As you can see is a workflow_dispatch workflow where we can specify the exact commit SHA we want to build. We want that the sha tag is the one we specified in the input and not the HEAD commit SHA of the branch where we dispatched the workflow.

We fixed with:

            -
                name: Docker metadata for PHP image
                id: php_meta
                uses: docker/metadata-action@v5
                with:
                    context: git
                    images: ...
                    tags: |
                        type=raw,priority=100,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
                        type=ref,priority=200,event=branch
                        type=raw,priority=300,value=${{ github.event.inputs.sha }}
                    flavor: |
                        latest=false
                        prefix=
                        suffix=

But maybe you can use our example to reproduce the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants