Skip to content

Checking out wrong commit? #299

Closed
Closed

Description

@jwilner

I just introduced a failing test into the main branch of a project; the failing tests (introduced in the commit triggering the build) appeared not to have run in the PR build.

When I look at the actions/checkout output, I am surprised by what I see.

Specifically, the build is running on commit 2dca06e -- however, actions/checkout appears to be merging the prior commit -- c0121d5 -- (without the tests) into the project's main branch.

Is this expected?

Screen Shot 2020-07-10 at 9 55 09 PM

The project's checkout configuration is completely vanilla: https://github.com/golangci/golangci-lint/blob/master/.github/workflows/pr.yml#L46

Activity

  1. jwilner commented on Jul 11, 2020

    @jwilner
    Author

    Local discussion of issue: golangci/golangci-lint#1229

  2. AustinShalit commented on Jul 11, 2020

    @AustinShalit

    This is similar to #237

  3. kenorb commented on Jul 13, 2020

    @kenorb

    Same happens when using with ad-m/github-push-action, related:

    I've described more details at #237 (comment)

  4. ericsciple commented on Jul 15, 2020

    @ericsciple
    Contributor
  5. medikoo commented on Jul 22, 2020

    @medikoo

    This looks quite serious (allows to sneak not validated code as valid)

    @ericsciple is there any specific timeline for this?

  6. tjanez commented on Jul 23, 2020

    @tjanez

    I was able to use the following work-around:

    [... trimmed ...]
          - name: Checkout code
            uses: actions/checkout@v2
            with:
              # Check out pull request's HEAD commit instead of the merge commit to
              # work-around an issue where wrong a commit is being checked out.
              # For more details, see:
              # https://github.com/actions/checkout/issues/299.
              ref: ${{ github.event.pull_request.head.sha }}
    [... trimmed ...]
    
  7. ruz commented on Jul 24, 2020

    @ruz

    This affects us, we found this documentation: https://developer.github.com/v3/pulls/#response-1. I searched code in this repository for mergeable and merge_commit_sha, but couldn't find anything. Feels like it's related.

  8. ssantichaivekin commented on Jul 24, 2020

    @ssantichaivekin

    I'm having the same problem in this pull request run: https://github.com/ssantichaivekin/empress/runs/900765234

    Screen Shot 2020-07-24 at 11 34 46 AM

    It seems that this can be fixed by making a commit that is up to date with the current origin/master. See ssantichaivekin/empress#184

  9. lethosor commented on Jul 25, 2020

    @lethosor

    We just ran into what I think is another instance of this issue in https://github.com/DFHack/scripts/runs/910104637?check_suite_focus=true#step:4:375
    This check ran on DFHack/scripts@5177a12, but ended up running on a merge between master and the previous commit on that branch (DFHack/scripts@1ec3d9b, as seen in the actions/checkout output), which produced a lot of irrelevant errors that the newer commit was intended to address. This issue repeated itself when we re-ran the check, so it appears to be reproducible in this case, but I'm not seeing an obvious reason why - the newer commit in that PR was pushed well after the checks run on the older commit failed, and we haven't seen this in other PRs. Update: we've seen this in at least two PRs so far.

    About 2 minutes after re-running the check reproduced the issue, I ran this locally, which resulted in fetching a correct merge, so I'm pretty sure the issue is on the action's end:

    $ git fetch origin refs/pull/159/merge
    $ git show FETCH_HEAD 
    commit 5be19d326c828ac38ce307eb6db068e691504a7e
    Merge: ad67c79 5177a12
    
  10. 13 remaining items

  11. wchargin commented on Mar 3, 2021

    @wchargin

    I had the same problem in the pull-request.

    Fix with:

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

    Beware: this checks out the current head of the branch, whereas by
    default the action checks out the merge commit. By using this
    workaround, you will no longer catch conflicting changes in the base
    branch.

    Is there a way to check out the correct merge commit without race
    conditions? There’s the merge_commit_sha field, but the docs
    indicate that that may be null if mergeability has not yet been
    computed.

  12. tonyhallett commented on Mar 23, 2021

    @tonyhallett

    @wchargin
    Was you running on pull_request_target ? The ref and sha is different to pull_request

    You can wait for the merge_commit_sha

    - name: wait for mergeable
            id: wait-for-mergeable
            uses: tonyhallett/ReleaseManager/wait_for_mergeable@main
            env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          - name: checkout
            uses: actions/checkout@v2
            with:
                ref: ${{ steps.wait-for-mergeable.outputs.mergeCommitSha}}
    

    I have not released my actions from ReleaseManager yet so here is the typescript code.

    import * as core from '@actions/core'
    import * as github from '@actions/github'
    import {PullRequest} from '@octokit/webhooks-definitions/schema'
    import {getProcessEnv} from './helpers/getProcessEnv'
    import {getNumberInput} from './helpers/inputHelpers'
    
    async function run(): Promise<void> {
      try {
        const initialWait = getNumberInput('initialWait')
        const waitTime = getNumberInput('wait')
        const maxWait = getNumberInput('timeout')
    
        const pullRequest = github.context.payload.pull_request as PullRequest
        const pullNumber = pullRequest.number
    
        const githubToken = getProcessEnv('GITHUB_TOKEN')
        if (!githubToken) {
          core.error('no token')
        } else {
          let timedOut = false
          let mergeable: boolean | null = null
          let mergeableState: string | undefined
          let mergeCommitSha: string | null = null
          const octokit = github.getOctokit(githubToken)
    
          await wait(initialWait)
          const start = new Date().getTime()
          // eslint-disable-next-line no-constant-condition
          while (true) {
            const response = await octokit.pulls.get({
              ...github.context.repo,
              pull_number: pullNumber
            })
            mergeable = response.data.mergeable
            if (mergeable != null) {
              mergeCommitSha = response.data.merge_commit_sha
              mergeableState = response.data.mergeable_state
              break
            }
            await wait(waitTime)
            const now = new Date().getTime()
            if (now - start > maxWait) {
              timedOut = true
              break
            }
          }
    
          core.setOutput('timedOut', timedOut)
          core.setOutput('mergeable', mergeable)
          core.setOutput('mergeableState', mergeableState)
          core.setOutput('mergeCommitSha', mergeCommitSha)
        }
      } catch (e) {
        core.error(e)
      }
    }
    
    const wait = async (interval: number): Promise<void> => {
      return new Promise<void>(resolve => {
        setTimeout(() => {
          resolve()
        }, interval)
      })
    }
    
    run()
    
    
  13. wchargin commented on Mar 23, 2021

    @wchargin

    @tonyhallett: This is also incorrect, because it fetches the most
    recent
    merge commit rather than the merge commit for the commit being
    tested. Consider the following sequence:

    1. Push commit A to PR 123.
    2. CI queued on commit A. Merge commit not yet computed.
    3. Push commit B to PR 123 (same PR).
    4. GitHub computes the new merge commit to merge B into master.
    5. CI run for commit A unqueues and starts running.

    If at point (5) you fetch the current merge commit for PR 123, you
    will be running on the code in commit B, not commit A. This would mean
    that when you click the CI status indicator on commit A, it actually
    shows you tests that ran at a different commit, which is obviously
    unacceptable.

  14. added a commit that references this issue on Jan 31, 2022
  15. ethomson commented on Feb 4, 2022

    @ethomson
    Contributor

    Closing, this is a dup of #237 and was resolved there. If you believe that you're seeing any problems here, please contact support.

  16. mistercrunch commented on May 14, 2025

    @mistercrunch

    This issue [or one very similar] bit me today while using actions/checkout@v4 and got me extremely confused. Looked like some ghost merge commit was sitting on top of mine and making me think I was going nuts.

    At first I noticed that HEAD wasn't the same as the HEAD on my PR

    $ git rev-parse --short HEAD
    c7e92e65a9

    This commit was nowhere to be found on my branch or local repo.

    Found it on the public repo here, unattached to anything...:
    Image

    Looking a bit deeper ->

    $ git show -s --format=raw HEAD
    commit c7e92e65a99c531dbc3a16e6ffff5b58368196e4
    tree 62d4ddfd7f526e63ea23be15b3e9537fa7f07f71
    parent fcea7e4af940999a4068ef6981e945e0ac3ec36b
    parent 46bb1463593dfedf9ef61a075eb3a74ffd5aabf5
    author Maxime Beauchemin <[email protected]> 1747261433 +0000
    committer GitHub <[email protected]> 1747261433 +0000
    gpgsig

    What is committer GitHub here? Where was that commit coming from?

    For the record, I was able to fix the issue by adding:

    with:
      ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
    

    Now should I add this to all my GHAs to make sure it doesn't bite us again?

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions