Using a private Composer package with Laravel and Docker.

Categories: Development

Developing packages for use with laravel is reasonably straightforward. But we don’t all make the code for public consumption on Packagist.

Adding a custom repository to your composer.json file will allow us to pull code straight from your Github account rather than Packagist.

repositories": [
    {
      "type": "package",
      "package": {
        "name": "adampatterson/app-core",
        "version": "0.0.4",
        "source": {
          "url": "[email protected]:adampatterson/app-core.git",
          "type": "git",
          "reference": "master"
        }
      }
    }
],

Now you should be able to add your package under require "adampatterson/app-core": "0.0.4"

It’s common practice to place your source code in a src/ folder. You will also want to create your packages composer.json file with at least the following details…

{
    "name": "adampatterson/core",
    "description": "Some kind of Core package for an app.",
    "type": "library",
    "homepage": "https://github.com/adampatterson/app-core",
    "support": {
        "issues": "https://github.com/adampatterson/app-core/issues",
        "source": "https://github.com/adampatterson/app-core"
    },
    "autoload": {
        "psr-4": {
            "AdamPatterson\\Core\\": "src/"
        },
        "files": [
            "src/Helpers.php"
        ]
    },
    "authors": [
        {
            "name": "Adam Patterson",
            "email": "[email protected]"
        }
    ]
}

In order for composer and other users to pull your package, you will need to add the repository manually to your project as it will not be discoverable under Packagist.

This can be done through GitHub by generating an OAuth key through their guide on Github.

"repositories": [
        {
            "type": "package",
            "package": {
                "name": "com:adam/core",
                "version": "master",
                "source": {
                    "url": "[email protected]:adampatterson/app-core.git",
                    "type": "git",
                    "reference": "master"
                }
            }
        }
    ],
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true,
        "bin-dir": "bin",
        "github-oauth": {
            "github.com": "someKindOfKeyHere"
        }
    }

If you are using Docker or another automated build system you will want to add an oAuth token to your projects composer.json file otherwise you will get a build error.

RUN /usr/local/bin/composer config -g github-oauth.github.com someKindOfKeyHere

Otherwise, your builds will fail.


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!