-
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
Checking out a merge commit in pull_request_target
workflows
#518
Comments
Here's another reason why it would be helpful if this could be supported directly in the action: I'd like to use If the merge commit could be created by the action, it could happen before the cleanup of persisted credentials. |
- name: Checkout code
uses: actions/checkout@v2
with:
ref: "refs/pull/${{ github.event.number }}/merge" this should work as you expected I think. updated 2023-08-02When I previsouly wrote this reply, github doesn't support disabling or limiting GitHub Actions for outside collaborators, so it's not a problem to simply use But if your workflow require approval for outside collaborators, you should use this one - name: Checkout code
uses: actions/checkout@v3
with:
ref: "${{ github.event.pull_request.merge_commit_sha }}" |
on.pull_request_target uses main by default here, different from on.pull_request which uses the PR ref see actions/checkout#518 (comment)
related to [actions/checkout#518 (comment)](actions/checkout#518 (comment))
I wonder if this is safe fully safe though? With this approach, if a malicious PR author pushes unsafe code between PR approval and the checkout action being run, your CI system might checkout a different version of the code than what the maintainer approved.
|
It's not safe. That's why you shouldn't execute any test code or lint plugin from PR. They can read env and maybe able to read your repo secret. you should only execute code like lint (with out loading plugin from repo) or formatter. |
I mean |
Yes, but |
yeah, I mean there is probably other ways to achieve the merge and test against the merged state. But using |
Workaround: Checkout Not ideal, but safe and works. |
Gist of the previous sugestion:
|
@antoniovazquezblanco I believe the correct syntax should be:
because the branch that is merged into is the first parent. Isn't it? |
This comment was marked as off-topic.
This comment was marked as off-topic.
I am hitting this too. Is there anyway to delay the job until the mergeability request is finished? I'm not sure if a delay within the workflow on a separate job before referencing the SHA is enough |
I'm having precisely the same problem, that when the PR is created and I checkout the |
cc @regisss |
@lujop @faximan and others who hit the same issue I worked around this by having a
This doesn't help if you want the processing to show up on the PR, but it worked for us, as we just needed the secret access to trigger a fire-and-forget deployment action. And the part we really cared to check was the artifact creation to make sure it succeeds. |
In our case I end up using |
I'm still stuck on this one. Specially as my CI task runs not only for |
This is a bit messy, but I wrote an inline script to check whether the PR is mergeable, and it seems to work decently: https://github.com/NixOS/nixpkgs/blob/c8db8bd9656ee3d373ce9445063c25c47f442118/.github/workflows/check-by-name.yml#L31-L86 |
**Problem** With `github.event.pull_request.merge_commit_sha` we're not guaranteed to get all the latest changes from the head branch included in the build. Often we only get the changes from the second last commit actions/checkout#518 (comment) Previously we used the merge branch but with the merge branch it's possible to push new changes to a PR from a forked repo and have the PR Build run on unapproved changes. **Proposed Solution** Use Github SHA if possible in the PR Build. Otherwise use the merge branch. The [pull_request](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request) trigger will use the github_sha which is the last merge commit on the GITHUB_REF branch. Thereby it should not be possible for the PR Build to run on unapproved changes. The [pull_request_target](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target) trigger cannot use the github_sha because it is the last commit on the PR base branch. Instead we'll use the merge branch. With the pull_request_target trigger PR Builds are always triggered right away which means we'll cancel any currently running PR Build once there's a new push to the PR.
…commit_sha Reason: pull_request.merge_commit_sha would consistently checkout the second last commit of the pr instead of the current one. This is unintended behaviour. With this change, the setting "Require branches to be up to date before merging" is required to run the test on the git state after the pr would be merged into main by requiring main to be merged into the pr branch before merging back to main. Source: actions/checkout#518
Part of the job checks out the pull request (this isn't done automatically because this runs with `pull_request_target` which checks out the base by default). That step was using `merge_commit_sha` which is not correct (this still checks out the base). The fix instead checks out `head.sha` which is correct. It is not possible to get a `merge_commit_sha` (there is a request for this [here](actions/checkout#518)) but `head.sha` should be good enough for our purposes.
I am coming from dependabot/dependabot-core#3253, where there is a lot of confusion of how to safely run actions with secrets when untrusted code from external PRs comes into play.
The bottom line is that there may be situations where you – after you understood the risks – might want to use the
pull_request_target
event because it has access to secrets; but combine that with a checkout of the PR.One suggested way of doing this is with
which will check out the PR head commit.
This is, however, not 100% what you'd get for a simple
uses: actions/checkout@v2
on apull_request
event, because that would check out a merge commit.I wonder whether it would be possible for this action here to also support checking out such a merge commit on
pull_request_target
events?I don't know if creating such a merge commit involves advanced Git trickery to get-it-right™️ , so I thought this was the best place to ask.
The text was updated successfully, but these errors were encountered: