Skip to content

Commit a2636af

Browse files
Jean85ramsey
andauthored
Add instructions on how to use the automatic GHA token (#263)
Co-authored-by: Ben Ramsey <ben@ramsey.dev>
1 parent 2da6381 commit a2636af

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

README.md

+50-4
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,54 @@ execution if it does not find a lock file.
209209

210210
### Fork and private repositories
211211

212-
Sometimes it's needed to use the `repositories` key in your `composer.json` to pull in forks, PRs with patches or private repositories. In this case, your GitHub Action may start failing with a `Could not authenticate against github.com` error message. To solve this, you need to add a GitHub Personal Access token, and this bit to your Action configuration:
212+
Sometimes it's necessary to use the `repositories` key in your `composer.json` to
213+
pull in forks, PRs with patches, or private repositories. In this case, your
214+
GitHub Action may start failing with a `Could not authenticate against github.com`
215+
error message. To solve this, you need to use an authorized token. Luckily,
216+
[GHA provides you with one automatically at each run][]; all you need to do is
217+
set the `repository-projects` permission to `read`:
218+
219+
```yaml
220+
job:
221+
permissions:
222+
repository-projects: read
223+
steps:
224+
# ...
225+
- uses: ramsey/composer-install@v3
226+
env:
227+
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.GITHUB_TOKEN }}"}}'
228+
```
229+
230+
In the example above, `COMPOSER_AUTH` is the [default environment variable that Composer supports][]
231+
to dynamically configure its authentication. If you have other authentication tokens
232+
in use, and you don't want to conflict with those, you can record the token
233+
programmatically, as in the example below:
234+
235+
```yaml
236+
job:
237+
permissions:
238+
repository-projects: read
239+
steps:
240+
# ...
241+
- run: composer config -- github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
242+
name: Register GitHub token for Composer
243+
- uses: ramsey/composer-install@v3
244+
```
245+
246+
Note that this approach is only valid for public forks; if you need to access
247+
private repositories, you must create a secret in the repository that runs the
248+
action, and use it to store a Personal Access Token with a `read:project` scope
249+
from a user that is allowed to read those repositories, and use in place of
250+
`secrets.GITHUB_TOKEN`. In the following example, the PAT is stored in a secret
251+
called `COMPOSER_PAT`:
252+
213253
```yaml
214254
env:
215-
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.COMPOSER_AUTH }}"}}'
255+
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.COMPOSER_PAT }}"}}'
216256
```
217-
In this example, `COMPOSER_AUTH` is the name of the secret that you'll need to create. To access public repositories, the `public_repo` scope is sufficient, while for private repositories (that you can access), `read:project` will be needed.
218257

219-
For more information on how to do that on your repository, see [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) and [Creating encrypted secrets for a repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) on GitHub documentation.
258+
For more information on how to do that on your repository, see [Creating a personal access token][]
259+
and [Creating encrypted secrets for a repository][] on GitHub documentation.
220260

221261
### Matrix Example
222262

@@ -263,3 +303,9 @@ yourself with [CONTRIBUTING.md](CONTRIBUTING.md).
263303
The ramsey/composer-install GitHub Action is copyright © [Ben Ramsey](https://benramsey.com)
264304
and licensed for use under the terms of the MIT License (MIT). Please see
265305
[LICENSE](LICENSE) for more information.
306+
307+
308+
[GHA provides you with one automatically at each run]: https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication
309+
[default environment variable that Composer supports]: https://getcomposer.org/doc/articles/authentication-for-private-packages.md#authentication-using-the-composer-auth-environment-variable
310+
[Creating a personal access token]: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
311+
[Creating encrypted secrets for a repository]: https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository

0 commit comments

Comments
 (0)