Welcome to Abdul Malik Ikhsan's Blog

Using Github Access Token with Composer for Private Repository

Posted in GIT, Tutorial PHP by samsonasik on April 16, 2017

If you successfully run composer install which clone Github private repository by hand, you may found a blocker when run the command via script even already added ssh public key and added ssh private key to ssh-agent.

Use case

For example, you have a git hook on post-receive which run composer install.

#!/bin/sh
GIT_WORK_TREE=/var/www/app git checkout -f
cd /var/www/html/app && composer install --no-dev

And your composer.json of your app has a github private repository, for example, as follow:

{
    "require": {
        "yourcompany/lib": "^1.0"
    },

    "repositories" : [
        {
            "type": "vcs",
            "url" : "git@github.com:yourcompany/Lib.git"
        }
    ]

}

Solution

To make it work, first, you need to create a token for it in https://github.com/settings/tokens .

When you get generated token, you can register it in composer.json so the configuration like below:

{
    // ...
    "config": {
      "github-oauth": {
        "github.com": "th3t0k3nth4tG3n3r4t3d"
      }
    }
}

Now, your complete composer.json will be as follow:

{

    "require": {
        "yourcompany/lib": "^1.0"
    },

    "repositories" : [
        {
            "type": "vcs",
            "url" : "git@github.com:yourcompany/Lib.git"
        }
    ],

    "config": {
      "github-oauth": {
        "github.com": "th3t0k3nth4tG3n3r4t3d"
      }
    }

}

That’s it.

Advertisements