I have a public repository that runs end-to-end tests. These tests require secrets that are stored in GitHub.
The corresponding workflow yaml file has pull_request entry as follows:
on:
pull_request:
....
Problem: If I (the owner) create a pull request myself, the workflow would run correctly and access the secrets. If someone creates a PR from a fork, GitHub would ask me to approve the run, however once I approve, the workflow would not see the values of those secrets and the tests would fail.
If I change pull_request to pull_request_target, it would access the secrets correctly. However this way, it doesn't ask for my approval before running the workflow (thus the secrets can easily leak).
pull_request_target:
branches:
- main
Question: How to make GitHub actions access the secrets when running on PRs from forks while still requiring to approve the run?
Unlike
pull_request,pull_request_targetrun the workflow in the context of the target repository, so you have access to the secrets. You can reduce this vulnerability by addinglabeledtype, however it doesn't really make this a safe approachFrom Keeping your GitHub Actions and workflows secure
If you still want to go that way add the
pull_request_targettrigger withlabeledtypeCreate a label via
Pull requests -> Labels -> new labeland apply it to the pull request from Labels section in the right side menu when you are ready to merge the PR, this will trigger the workflow.