checkout icon indicating copy to clipboard operation
checkout copied to clipboard

detached HEAD

Open ondrados opened this issue 2 years ago • 2 comments

Recently I run into this issue where I cant deploy my application using AWS EB CLI. I have simple workflow with actions/checkout@v2. Then i have simple job with custom Dockerfile where I only install CLI and run deploy command. CLI uses current git branch to decide which environment should be deployed (dev branch -> dev environment, master branch -> production environment, etc.) - based on my config file.

But now i get this message:

WARNING: Git is in a detached head state. Using branch "default".

And since branch default is not set (in my config file), because i need to only deploy specific branches to specific environments, nothing is deployed.

Relevant parts from main.yml

  steps:
  - uses: actions/checkout@v2

  - name: AWS EB deployment
    uses: ./deployment

deployment/action.yml

runs:
  using: "docker"
  image: "Dockerfile"

I tried some things from https://github.com/actions/checkout/issues/124 but without any luck. I also tried older version of v2 and newest v3.

ondrados avatar Apr 20 '22 14:04 ondrados

I ran into a similar problem where branch names are missing locally (i.e. inside of the action container). However, git branch -r shows that all branches are there

  origin/develop
  origin/master
  origin/test
  ...

and git branch -a gives me

  remotes/origin/develop
  remotes/origin/master
  remotes/origin/test
  ...

Interestingly, if I use

      - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0

then git branch tells me

* test

but if I check out without the ref: or using the head.sha then git branch tells me

* (HEAD detached at pull/8/merge)

So, what I ended up with in my Action are constructs like

origin/${{ github.event.pull_request.base.ref }}

which refer to branch names that actually do exist locally.

Alternatively, I guess you could loop over the branches and check them out locally, and there’s some more information here.

jenstroeger avatar Jul 16 '22 22:07 jenstroeger

This is another option if you just need HEAD to be mapped to the default branch, which it isn't by default when using the checkout action in a workflow triggered by a pull request event:

- name: Set default branch.
  run: git remote set-head origin --auto
  shell: bash

Kurt-von-Laven avatar Aug 30 '22 08:08 Kurt-von-Laven