# Composer registry

> For AI agents: the complete documentation index is at https://buddy.works/llms.txt

Publish private PHP packages to composer.registry.sh from a Buddy pipeline and install them with composer install using a personal access token.

A Composer Package artifact is a private PHP package repository at `https://composer.registry.sh`. The **Composer Publish** action publishes the package, and the application installs it with a regular `composer install`. Each published package version is an artifact version.

## Package naming

The package name has the form `<workspace>/<artifact>`. The `my-php-lib` artifact in the `my-workspace` workspace accepts only the `my-workspace/my-php-lib` package, so the `name` field in the package's `composer.json` must have exactly this value.

You do not need to change the PHP namespaces in your code.

## Artifact creation

You create the artifact in the UI: in **Artifacts**, click **New artifact** and select **Composer Package**. If an artifact with that name does not exist, the **Composer Publish** action creates it on the first run. Such an artifact has project scope and **Buddy Auth** access.

## Package publishing in pipelines

The **Composer Publish** action publishes the contents of a directory containing a `composer.json` file. All files in the specified directory are included, including dotfiles and files excluded by `.gitignore`. Only the `.git` directory is excluded. To limit the package contents, copy the required files to a separate directory before publishing:

```yaml
- action: Prepare package
  type: BUILD
  docker_image_name: library/composer
  docker_image_tag: "2"
  commands: |-
    composer validate --no-check-publish
    rm -rf dist && mkdir dist
    cp -r composer.json src dist/
- action: Publish to Composer registry
  type: COMPOSER_PUBLISH
  artifact: my-php-lib
  path: dist
  version: 1.0.0
```

To point the `path` field at the root of the pipeline filesystem, use `/`, not `.`.

The version comes from the action's **Version** field or from `composer.json`. It must be a valid semantic version without a `v` prefix, so a version taken from a Git tag, such as `v1.1.0`, fails validation. The action fields are described in the [Composer Publish reference](https://buddy.works/docs/yaml/yaml-actions/composer-publish.md). Outside a pipeline, you can publish the package with [`bdy artifact composer publish`](https://buddy.works/docs/cli/artifact/composer/publish.md).

![Publish Composer package action](https://buddy.works/docs/artifacts/composer-publish-action.png)

## Version immutability

Published package versions cannot be overwritten. Package changes require publication of a new version.

## Package installation

In the application's `composer.json`, add the repository and the dependency:

```json
{
    "repositories": [
        { "type": "composer", "url": "https://composer.registry.sh" }
    ],
    "require": {
        "my-workspace/my-php-lib": "^1.0"
    }
}
```

The **Pull artifact** window in the artifact view shows a ready `composer.json` snippet for that artifact.

![Pull artifact window of a Composer Package artifact](https://buddy.works/docs/artifacts/composer-pull-artifact.png)

## Registry authentication

The registry requires a [personal access token](https://buddy.works/docs/api/getting-started/oauth2/personal-access-token.md) with permission to read artifacts. On a developer machine, save the token in the global Composer configuration:

```bash
composer config --global bearer.composer.registry.sh "$REGISTRY_TOKEN"
```

In a pipeline, store the token in an encrypted variable, for example `COMPOSER_TOKEN`, and pass it through `COMPOSER_AUTH`. The credentials then do not end up in an `auth.json` file, and the token is masked in the action log:

```yaml
- action: Install dependencies
  type: BUILD
  docker_image_name: library/composer
  docker_image_tag: "2"
  commands: |-
    export COMPOSER_AUTH="{\"http-basic\":{\"composer.registry.sh\":{\"username\":\"token\",\"password\":\"$COMPOSER_TOKEN\"}}}"
    composer install --no-interaction --prefer-dist
```

The same token and command work in another CI system.

## Publishing permissions

An artifact created by the **Composer Publish** action initially allows publishing only from the pipeline that created it. Other pipelines need the **Read-write** level in the artifact's **Permissions** tab.

Publishing a version is an artifact event, so it can start another pipeline, for example a test of the application with the new package version. The trigger configuration is described in [On artifact event](https://buddy.works/docs/pipelines/introduction/triggering-pipelines/on-artifact-event.md).

See also:

- [Private Composer dependencies](https://buddy.works/docs/pipelines/private-dependencies/composer-php.md) - private PHP dependencies in pipelines
- [Permissions and access](https://buddy.works/docs/artifacts/permissions-and-access.md) - Read-only, Read-write and Manage levels for pipelines
- [npm registry](https://buddy.works/docs/artifacts/npm-registry.md) - the same model for npm packages
- [Host private Composer packages in Buddy](https://buddy.works/blog/composer-private-packages) - example with a Laravel package and a `/health` test


---
Original source: https://buddy.works/docs/artifacts/composer-registry