# GITHUB\_TOKEN for use with composer

**URL:** <https://forum.gitea.com/t/github-token-for-use-with-composer/8293>\
**Category:** Actions\
**Created:** [December 14, 2023, 11:48am UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293 "2023-12-14T11:48:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![liepumartins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/liepumartins/32/4270_2.png) [@liepumartins](https://forum.gitea.com/u/liepumartins)\
**Post date:** [December 14, 2023, 11:48am UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/1 "2023-12-14T11:48:17Z")

</div>

We are creating CI/CD pipeline for PHP.

Step `composer install` goes on forever and fails with messages like these

```auto
Failed to download phpstan/phpstan from dist: Could not authenticate against github.com
    Now trying to download from source

```

Internet says that github is rate-limiting requests and oauth token is required. We created a secret COMPOSER\_GITHUB\_TOKEN and added `GITHUB_TOKEN: ${{ secrets.COMPOSER_GITHUB_TOKEN }}`. This did not help.

After this error, we added `composer diagnose` as suggested at composer repository issues.

```auto
Checking github.com oauth access: The oauth token for github.com seems invalid, run "composer config --global --unset github-oauth.github.com" to remove it

```

Did some more debugging, it seems that GITHUB\_TOKEN is generated and cannot be set. Here is what we tried.

```yml
name: ci

on:
  push:
    branches:
      - "trunk"
env:
  GITHUB_TOKEN: ${{ secrets.COMPOSER_GITHUB_TOKEN }}
jobs:
  build:
    runs-on: ubuntu-latest
    container: catthehacker/ubuntu:act-latest
    steps:
      - name: Debug Print token GITHUB_TOKEN base64 before shivammathur
        run: echo $GITHUB_TOKEN|base64 # this prints base64 of random string, each time different
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
      - name: Debug Print secret
        run: echo ${{ secrets.COMPOSER_GITHUB_TOKEN }} # this prints correct token
      - name: Debug Print secret
        run: echo ${{ secrets.COMPOSER_GITHUB_TOKEN }}|base64 # this prints base64 of correct token
      - name: Debug Print token GITHUB_TOKEN base64 # since we cannot print it directly
        run: echo $GITHUB_TOKEN|base64 # this prints base64 of *the* random string
      - name: Debug print env
        run: env # this prints env variables, GITHUB_TOKEN is represented with ***
      # tried also to unset & set token manually
      - name: Register Github oauth
        run: composer config --global --unset github-oauth.github.com && composer config -g github-oauth.github.com ${{ secrets.COMPOSER_GITHUB_TOKEN }}
      - name: Debug print composer settings
        run: composer config --list
      - name: Debug print composer setting
        run: composer config github-oauth.github.com|base64 # this prints base64 of *the* random string
      - name: Diagnose composer
        run: composer diagnose # fails with message about invalid oauth token
      - name: Install dependencies
        run: composer install --prefer-dist # this is not reached; or if diagnose step is removed 

```

Question is how to use composer with GITHUB\_TOKEN on gitea actions?

---

<div class="post-metadata">

**Author:** ![lunny](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/lunny/32/4701_2.png) [@lunny](https://forum.gitea.com/u/lunny)\
**Post date:** [December 18, 2023, 2:49pm UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/2 "2023-12-18T14:49:52Z")

</div>

GITHUB\_TOKEN is generated and used by gitea server side. It cannot be used for a github repository.  
And I don’t think cloning from github has a rate limitation. It should only be applied on APIs. And of course, the action `shivammathur/setup-php` will download artifacts from github releases? I’m not familiar with that action.

---

<div class="post-metadata">

**Author:** ![liepumartins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/liepumartins/32/4270_2.png) [@liepumartins](https://forum.gitea.com/u/liepumartins)\
**Post date:** [December 18, 2023, 7:19pm UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/3 "2023-12-18T19:19:53Z")

</div>

Action [`shivammathur/setup-php`](https://github.com/shivammathur/setup-php) creates PHP environment with tools, fine grained modules etc. It was suggested by [Laravel Vapor docs](https://docs.vapor.build/)  
Maybe there is a better alternative available?

Downloading from github happens using `composer install` there are multiple github issues and stackoveflow discussions about rate-limited downloads from github for the unauthenticated (without token).

Seems problem lies with the fact, that composer takes the GITHUB\_TOKEN env variable over other token configuration methods and since gitea uses GITHUB\_TOKEN for other purposes this is a dead end.

---

<div class="post-metadata">

**Author:** ![lunny](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/lunny/32/4701_2.png) [@lunny](https://forum.gitea.com/u/lunny)\
**Post date:** [December 19, 2023, 1:46am UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/4 "2023-12-19T01:46:31Z")

</div>

Maybe you can try to use step sepical secrets like below

```auto
- name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          github_token: ${{ secrets.COMPOSER_GITHUB_TOKEN }}

```

---

<div class="post-metadata">

**Author:** ![liepumartins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/liepumartins/32/4270_2.png) [@liepumartins](https://forum.gitea.com/u/liepumartins)\
**Post date:** [December 19, 2023, 9:52am UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/5 "2023-12-19T09:52:31Z")

</div>

Already tried that, before starting this thread.

Found out another option to pass token without `GITHUB_TOKEN` use.  
There is `COMPOSER_AUTH_JSON` that should contain the `auth.json` like so:

```auto
{
  "github-oauth": {
    "github.com": "ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  }
}

```

```yml
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      COMPOSER_AUTH_JSON: ${{ secrets.COMPOSER_AUTH_JSON }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          extensions: json, mbstring, pdo, pdo_sqlsrv, simplexml
          tools: composer:v2, phpunit, phpstan
          coverage: none     
      - name: debug auth.json file content
        run: cat /root/.composer/auth.json
      - name: Diagnose composer
        run: composer diagnose

```

I checked the contents of auth.json using `cat /root/.composer/auth.json`

However the error is still the same and this now looks like either composer or shivammathur/setup-php bug.

Thank You!

---

<div class="post-metadata">

**Author:** ![liepumartins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.gitea.com/liepumartins/32/4270_2.png) [@liepumartins](https://forum.gitea.com/u/liepumartins)\
**Post date:** [December 21, 2023, 7:28am UTC](https://forum.gitea.com/t/github-token-for-use-with-composer/8293/6 "2023-12-21T07:28:54Z")

</div>

It appears that composer is not reading the `auth.json` file, it relies on env variable `COMPOSER_AUTH` which shivammathur/setup-php [overwrites](https://github.com/shivammathur/setup-php/discussions/806) installing GITHUB\_TOKEN as token value.

Workaround is to pass for each step using composer

```yml
      - name: Install dependencies 
        run: composer install # will not work       
      - name: Install dependencies
        env:
          COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH_JSON }} # only this works
        run: composer install # will work

```
