-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Checkout@v2 detached HEAD #124
Comments
The pull request refs aren't regular branches so detached head is intentional. V2 fetches depth 1 by default. It sounds like the lack of history might be the issue? Setting |
if so, related to PR #127 (added note about fetch depth to the summary) |
If you intend to push changes, you'll need to create a branch. |
Closing. Reopen if still hitting issues. |
@ericsciple I'm a little confused with this. So we have a branch with a change on it. What I want to do is complete an action on that branch (let's say I've a repo with some markdown I want to auto generate HTML from when any changes are made to the markdown files). Now this is very easy to do when completing this action on a push. However that means this Action happens each time the code is pushed to GitHub. This will then require the person working on that branch to pull the changes made by my GitHub /Action on that branch, before they can push another change. This could get tiresome if pushing a lot of commits periodically. So I thought to only run this action when upon opening a pull request as by that time any changes should hopefully have settled down a bit. But then I ran it this detached head issue, which I'll admit I don't really understand (I'm far from a Git expert!). So is it not possible to add directly to a branch that is part of a pull request and you must create another branch from that instead? If that is the case, then what is the recommended workflow for this as would have thought it was a fairly common request.
Any advice appreciated as a bit new to this! |
@bazzadp sorry for the delay, i was out and just catching up on notifications |
@bazzadp i had a similar problem, but went a different direction. I added a Automatically pushing commits is an alternate approach but tradeoffs I personally didn't want:
|
The detached head issue is because checkout during a PR happens against the merge ref, not a branch. Branches are like You could checkout the branch instead. The branch is available from the on: push
jobs:
one:
runs-on: ubuntu-16.04
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT" It would be similar to this scenario where the branch HEAD commit is checked out. Looking at the event payload docs it looks like it might be Again note, this wont work for forks. 1) because the ref from the fork repo wont exist and 2) because you would need to push to the fork repo |
@bazzadp hope that helps. Let me know if i can help further. Folks on the community forum might have better suggestions |
Thanks for getting back to me.
Yeah this is where I really want it to work to be honest. We want to be able to accept typo pull requests from people not familiar with the site build process, and then not have the next person to build the site suddenly see changes they did not do included in their PR. The Calibre Image optimisation app does this very well. When you open a PR with images, it optimised the images and updates the PR with the optimised images. See this comment as an example: Looking at their source code it appears they use the GitHub API and create a new tree and then replace the PR with that tree, so doesn't look simple. But does work very well, so would be really useful to be able to do with this Checkout action. However if that's not possible, or overly complex so something you don't want to add, then I can understand this and feel free to close this issue. |
@bazzadp cool i didnt realize the built-in token has push permission. It looks like Calibre is pushing to the user's branch. I would expect their solution to have the drawback you mentioned: "require the person working on that branch to pull the changes". Additionally, the Calibre solution will not work for forks. The built-in token does not have permission to push to the fork repo where the user's branch is located. Therefore I would suggest a workflow that runs only against master. Something like this: on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: |
if [ ! -e gen.txt ]; then
echo hello > gen.txt
git config user.email "github-actions-bot@github.com"
git config user.name "github-actions"
git add gen.txt
git commit -m "generated"
git push
fi |
Yes it does. However, by only doing this when opening a pull request, the annoyance factor is much less than doing it on every push.
It does work for forks! In this case I think the action is initiated by the fork updating it's own repo, not the upstream repo trying to update the forked repo. Of course this only works once the action is cloned down to the fork, but with actions only requiring a simple file in the And it also works for fine non-forked branches as well.
Yup that's what I've ended up doing for now. It works, but it does make some people nervous that we are committing changes directly to master without reviewing them as part of a PR. If the generate script messes up then master could get in a bad way. Hence why I was looking for a better solution to add to PR instead. |
@bazzadp I got it working with @ericsciple suggestion (passing the ref to the actual PR branch head). This allowed me to do push after my action automatically generates the changes I needed:
Just letting this here for anybody that hits the same issue. However, please note that I didn't test this on forks, however if the action runs on the fork as @bazzadp mentions it might work. |
…e commit messages because the PR branch was checked out as a detached head. The error we received is below: error running commitlint fatal: ambiguous argument '46bb5821095a7e8e1ffd48f4697eec3c593c9b24^1..46bb582': unknown revision or path not in the working tree. This was solved using a solution reported here: actions/checkout#124 (comment)
…ssages because the PR branch was checked out as a detached head. The error we received is below: error running commitlint fatal: ambiguous argument '46bb5821095a7e8e1ffd48f4697eec3c593c9b24^1..46bb582': unknown revision or path not in the working tree. This was solved using a solution reported here: actions/checkout#124 (comment)
I just experienced this issue myself. Thank to the suggestions in this thread I was able to fix the issue. FYI, my workflow triggers on pull_request and on push, so here's what my config looks like in case someone does something similar.
|
Upon further investigation it does not work for forks - it just fails silently so I presumed it was working when it wasn’t 😔 So you can do it on pull requests within the current repo (by passing the ref) but not for pull requests from forks. Therefore it does look like the best option, to also support forks, is to just use Actions for checks, and then open another pull request for changes upon merge, instead of trying to add to the current pull request. That’s quite easy with this Action: https://github.com/peter-evans/create-pull-request and also has the benefit of allowing you to review any changes. The down side is the extra PR and need to be careful to avoid an infinite loop as that merging PR may kick off this whole workflow again! |
Maybe actions/checkout#124 will change the behaviour?
actions/checkout#124 changes the behaviour!
as described here # required for GitPython checks actions/checkout#124
I wanted to leave an alternative hint here, as a summary of #773 (comment): using
worked considering that branches aren‘t checked out locally (see @ericsciple’s comment above). It feels a little hacky, though, and the alternative would be to |
Hello team, Where does this solution stand today? I have a workflow (below) that auto-formats Terraform configuration files. It pushes back to the PR branch. Is there anything that I should be worried about? Is the Thanks name: Terraform format
on:
pull_request:
branches: [main]
jobs:
terraform-fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Install Terraform
uses: hashicorp/setup-terraform@v2
- name: Format Terraform configuration files
run: terraform fmt -recursive
- name: Check for changes
run: |
if [[ -n $(git status --porcelain) ]]; then
echo "Changes detected, committing and pushing..."
else
echo "No changes detected, skipping commit and push."
exit 0
fi
- name: Commit formatted code
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add .
git commit -m "Format Terraform configuration files"
git push |
Thank you, for this solution! Worked for me. And I used actions/checkout@v3 |
I just want to leave a comment here that this approach fixed my issue: I needed |
Seems like the mentioned solution work on checkout@v4 |
Hi,
I have a workflow that runs on pull requests, what it does is:
Had to do some workarounds to make it work in checkout@v1 so now I switched to checkout@v2 but I still get:
The text was updated successfully, but these errors were encountered: