Using a private repo with composer and bitbucket

Setting up composer to use a private repo is pretty easy… after you’ve done it once. There are a few things that can trip you up, such as the naming of the package and the version to include. The docs for getting the configuration set up got me about 95% of the way there.

This guide assumes you already have a composer.json file created, and you’re looking to add a new private repository to that.

Part 1: Bitbucket configuration

Composer is going to need access to your Bitbucket account in order to fetch the private git repo. An OAuth consumer can be used to give composer access.
– In Bitbucket, go to your “Bitbucket Settings” page https://bitbucket.org/account/user/<username>/

– Click “OAuth”
oauth_link

– Click the “Add consumer” button
consumer_button

– Fill in the name, description, callback URL (this can be anything – composer won’t use it but it is required by Bitbucket), select This is a private consumer, and select Repositories: Read for the permissions.
consumer_config_pt1

consumer_config_pt2

 

Part 2: Composer configuration

Using the Key and Secret from the OAuth consumer you created in part 1, you will need to add an auth.json file in composer’s home directory.

 vi ~/.composer/auth.json
{
    "bitbucket-oauth": {
        "bitbucket.org": {
            "consumer-key": "myKey",
            "consumer-secret": "mySecret"
        }
    }
}

 

Part 3: Configure composer.json

1) Add your private repository to the configuration so composer can look there for packages. The URL for the repo can be found in Bitbucket by clicking the “Clone” button in the top right of the repository view.

package_url

"repositories": [
  {
    "type": "git",
    "url": "https://aerosox@bitbucket.org/aerosox/dataconverter.git"
  }
]

2) Require the package in composer.json. With either of these methods.
CLI: composer require aerosox/dataconverter
Edit composer.json

"require": {
    "aerosox/dataconverter": "master@dev"
  }

 

Troubleshooting

1)  Ensure the package name in the require matches the name from the composer.json configuration for the package you are including.

// composer.json from aerosox/dataconvert

{
    // even though the git repo is configured as aerosox/dataconverter.git, this name *could* be anything. This is what composer is going to be looking for when it searches for the package to install.
    "name": "aerosox/dataconverter",
    "description": "Convert data formats",
    "type": "library",
    "authors": [
        {
            "name": "Levi Jackson",
            "email": "aerosox@gmail.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "DataConverter\\": "src/"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^6.5"
    }
}

2) I saw a lot of guides say to load the package as “aerosox/dataconverter”: “master-dev”. I initially thought that master-dev was a composer standard, but it’s not. If you want to use that, you’d want to set up an alias in the package so it can be found.

"extra": {
  "branch-alias": {
    "dev-master": "actual-branch-name"
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>