Skip to content

Commit 79ae6ee

Browse files
committed
Update README concerning pull_request_target trigger
Closes #211
1 parent 488db3d commit 79ae6ee

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Add the following step at the end of your job, after other steps that might add
2222
Note that the Action has to be used in a Job that runs on a UNIX system (eg. `ubuntu-latest`).
2323
If you don't use the default permission of the GITHUB_TOKEN, give the Job or Workflow at least the `contents: write` permission.
2424

25-
The following is an extended example with all possible options available for this Action.
25+
The following is an extended example with all available options.
2626

2727
```yaml
2828
- uses: stefanzweifel/git-auto-commit-action@v4
@@ -153,7 +153,7 @@ The goal of this Action is to be "the Action for committing files for the 80% us
153153

154154
The following is a list of edge cases the Action knowingly does not support:
155155

156-
**No `git pull` when the repository is out of the date with remote.** The Action will not do a `git pull` before doing the `git push`. **You** are responsible for keeping the repository up to date in your Workflow runs.
156+
**No `git pull` when the repository is out of date with remote.** The Action will not do a `git pull` before doing the `git push`. **You** are responsible for keeping the repository up to date in your Workflow runs.
157157

158158
**No support for running the Action in build matrices**. If your Workflow is using build matrices, and you want that each job commits and pushes files to the remote, you will run into the issue, that the repository in the workflow will become out of date. As the Action will not do a `git pull` for you, you have to do that yourself.
159159

@@ -204,9 +204,45 @@ If you use this Action in combination with a linter/fixer, it's easier if you ru
204204

205205
---
206206

207-
By default, this Action will not run on Pull Requests which have been opened by forks. (This is a limitation by GitHub, not by us.)
207+
By default, this Action will not run on Pull Requests which have been opened by forks. (This is a limitation by GitHub, not by us.)
208+
However, there are a couple of ways to use this Actions in Workflows that should be triggered by forked repositories.
208209

209-
If you want that a Workflow using this Action runs on Pull Requests opened by forks, 2 things have to be changed:
210+
### Workflow should run in **base** repository
211+
212+
The workflow below runs whenever a commit is pushed to the `main`-branch or when activity on a pull request happens, by listening to the [`pull_request_target`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target) event.
213+
214+
If the workflow is triggered by the `pull_request_target`-event, the workflow will run in the context of the base of the pull request, rather than in the context of the merge commit, as the `pull_request` event does.
215+
In other words, this will allow your workflow to be run in the repository where the pull request is opened to and will push changes back to the fork.
216+
217+
Check out the discussion in [#211](https://github.com/stefanzweifel/git-auto-commit-action/issues/211) for more information on this.
218+
219+
```yaml
220+
name: Format PHP
221+
222+
on:
223+
push:
224+
branches:
225+
- main
226+
pull_request_target:
227+
228+
jobs:
229+
php-cs-fixer:
230+
runs-on: ubuntu-latest
231+
steps:
232+
- uses: actions/checkout@v3
233+
with:
234+
repository: ${{ github.event.pull_request.head.repo.full_name }}
235+
ref: ${{ github.head_ref }}
236+
237+
- name: Run php-cs-fixer
238+
uses: docker://oskarstark/php-cs-fixer-ga
239+
240+
- uses: stefanzweifel/git-auto-commit-action@v4
241+
```
242+
243+
### Workflow should run in **forked** repository
244+
245+
If the workflow should run in the forked repository, follow these steps:
210246

211247
1. In addition to listening to the `pull_request` event in your Workflow triggers, you have to add an additional event: `pull_request_target`. You can learn more about this event in [the GitHub docs](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target).
212248
2. GitHub Action has to be enabled on the forked repository. \
@@ -239,7 +275,7 @@ jobs:
239275
commit_message: Apply php-cs-fixer changes
240276
```
241277

242-
Next time a user forks your project **and** enabled GitHub Actions **and** opened a Pull Request, the Workflow will run on the the forked repository and will push commits to the same branch.
278+
Next time a user forks your project **and** enabled GitHub Actions **and** opened a Pull Request, the Workflow will run on the the **forked** repository and will push commits to the same branch.
243279

244280
Here's how the Pull Request will look like:
245281

0 commit comments

Comments
 (0)