Skip to content

Checkout action with branch as ref not consistent through workflow #1634

Closed
@Martiix

Description

@Martiix

I recently moved my github actions workflow to trigger on pull_request which has a different checkout behavior than on push. Then to ensure that the git repo was consistent both on push and on pr, I used the checkout action to check out the current branch.

My main issue with the default checkout on pr was that running git commands doesn't give expected output for instance git branch and git show -s --format=%B.

The issue that now occurs is that a workflow starts running on the main branch with one commit, but during the workflow, something else is merged to that branch and the next job then checks out the latest commit instead of the commit it started running with. I tried checkout out the current commit throughout the workflow instead. In python in the workflow a: return Repo(repo_root_path).active_branch.name works on push events, but in prs, results in: HEAD is a detached symbolic reference as it points to SHA. Also with ref: ${{ github.event.pull_request.head.sha }}, but works with ref: current_branch_name

Is there a way to check out a branch at a given commit consistently through a workflow both on push and on pr, for instance:

- name: Check out code
              uses: actions/checkout@v4
              with:
                  ref: branch/commit

or any other way to check out the same commit throughout a workflow while also having access to the git information?
uaing fetch-depth:0 makes no difference.

Activity

jamesbradlee

jamesbradlee commented on Aug 20, 2024

@jamesbradlee

I also have the same problem.

When not specifying a ref, the checkout action will use the ${{ github.ref }} and ${{ github.sha }} properties automatically. But there is currently no way to 'manually' specify both a commit and a ref into the checkout action. relevant code

I will open a pull request for this.

jamesbradlee

jamesbradlee commented on Aug 20, 2024

@jamesbradlee

I didn't actually fully read through your issue. Sorry!

The behavior of the pull request synchronization is that it fetches the pull/<number>/merge ref, which for some can be problematic because the latest commit will be a merge commit. Alternatively it will be possible to pull down the pull/<number>/head branch to only get the contents of that branch.

The problem I had is that I have jobs that can span between minutes, and between the first job run and the last, if a new commit is added to the pull/<number>/head branch, then that new commit will be included in later jobs for a workflow that was meant for a specific commit of a pull request.

Either way - #1858 should solve this issue.

Martiix

Martiix commented on Aug 21, 2024

@Martiix
Author

I have both problems, where either can be the solution, thank you very much :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Martiix@jamesbradlee

      Issue actions

        Checkout action with branch as ref not consistent through workflow · Issue #1634 · actions/checkout