---
url: /en/artifact/composer.md
---
## Configure Push Credentials

Upload artifact files to the Composer Artifact Registry via the cnb REST API. For how to obtain `CNB_TOKEN`, refer to [Creating an Access Token](./intro.md#creating-an-access-token).

### Configure .netrc to Automatically Load Push Credentials When Using curl Commands

```shell
printf "\nmachine composer.cnb.share.ralphlauren.cn\nlogin cnb\npassword %s\n" "<CNB_TOKEN>" >> ~/.netrc
```

### Or Directly Specify Push Credentials in the curl Command

```shell
curl -ucnb:<CNB_TOKEN> https://composer.cnb.share.ralphlauren.cn/xxx... -T <PATH_TO_FILE>
```

## Configure Pull Credentials

1. Add the Artifact Registry address to the global config.json (usually located at ~/.composer/config.json). For how to obtain `CNB_COMPOSER_URL`, refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address).

```shell
# Disable the default official source
composer config repos.packgist false -g
# Use cnb source
# Example: composer config repos.cnb-composer-repo composer https://composer.cnb.share.ralphlauren.cn/cnb/composer-repo/-/packages/ -g
composer config repos.cnb composer <CNB_COMPOSER_URL> -g
```

2. Add credentials to the global auth.json (usually located at ~/.composer/auth.json).

```shell
composer config http-basic.composer.cnb.share.ralphlauren.cn cnb <CNB_TOKEN> -g
```

## Push Artifacts

::: tabs
@tab Local Command Line Push

1. Paste the following content into composer.json to avoid packaging authentication information and dependencies.

```json
{
    "archive": {
        "exclude": ["auth.json", "composer.lock", "vendor"]
    }
}
```

2. Navigate to the package directory and package the Composer artifact.

```shell
composer archive -f zip --file package
```

3. Publish to CNB.

**Note:**

1. For how to obtain `CNB_COMPOSER_URL`, refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address).
2. `<vendor>/<package>/<version>` should be replaced with the corresponding values for the current package, e.g., `monolog/monolog/3.8.1`.

```shell
curl --netrc "<CNB_COMPOSER_URL>upload/<vendor>/<package>/<version>" -T package.zip
```

@tab Push in Cloud Native Build

1. Paste the following content into `.cnb.yaml`.

**Note:**

1. Replace `CNB_COMPOSER_URL`. For how to obtain it, refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address).
2. `<vendor>/<package>` should be replaced with the corresponding values for the current package, e.g., `monolog/monolog`.

```yaml title=".cnb.yml"
$:
  tag_push:
    - docker:
        image: docker.cnb.cool/examples/language/php-8.4
      stages:
        - name: Configure Authentication Credentials
          script: printf "machine composer.cnb.share.ralphlauren.cn\nlogin cnb\npassword %s\n" "${CNB_TOKEN}" > ~/.netrc
        - name: Package Composer Artifact
          script: composer archive -f zip --file package
        - name: Publish to CNB
          script: curl --netrc -T package.zip "<CNB_COMPOSER_URL>upload/<vendor>/<package>/${CNB_BRANCH}"
```

2. Paste the following content into composer.json to avoid packaging authentication information and dependencies.

```json
{
    "archive": {
        "exclude": ["auth.json", "composer.lock", "vendor"]
    }
}
```

3. Submit a Tag to trigger Cloud Native Build. Note: The Tag value will be used as the artifact version number. Please use [Semantic Versioning](https://semver.org/).

@tab Push in Cloud Native Development

1. Paste the following content into composer.json to avoid packaging authentication information and dependencies.

```json
{
    "archive": {
        "exclude": ["auth.json", "composer.lock", "vendor"]
    }
}
```

2. Paste the following content into `.cnb.yaml`. After submitting to the code repository, click the **「Workspace」** button in the upper right corner of the code repository page to enter the development environment.

```yaml title=".cnb.yml"
$:
  vscode:
    - docker:
        image: docker.cnb.cool/examples/language/php-8.4
      services:
        - vscode
        - docker
```

3. After entering the development environment, execute the following command in the command line to configure authentication credentials.

```shell
printf "machine composer.cnb.share.ralphlauren.cn\nlogin cnb\npassword %s\n" "${CNB_TOKEN}" > ~/.netrc
```

4. Package the Composer artifact.

```shell
composer archive -f zip --file package
```

5. Publish to CNB.

**Note:**

1. For how to obtain `CNB_COMPOSER_URL`, refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address).
2. `<vendor>/<package>/<version>` should be replaced with the corresponding values for the current package, e.g., `monolog/monolog/3.8.1`.

```shell
curl --netrc -T package.zip "<CNB_COMPOSER_URL>upload/<vendor>/<package>/<version>"
```

:::

## Use Artifacts

### Pull Artifacts

After configuring credentials as described in [Configure Pull Credentials](#configure-pull-credentials), use the following command to pull artifacts.

```shell
# Example with monolog/monolog
composer require monolog/monolog
```

### Delete Artifacts

Composer does not provide a delete command. Refer to [Deleting Artifacts](./intro.md#deleting-artifacts).

## FAQ

### Q1: Upload Artifact Error - No Permission

**Example:**

```shell
curl --netrc <CNB_COMPOSER_URL>upload/monolog/monolog/3.8.0 -T monolog.zip
{"errcode":16,"errmsg":"not authorized"}
```

**Solution:**

Refer to [Creating an Access Token](./intro.md#creating-an-access-token) and [Configure Push Credentials](#configure-push-credentials) to correctly configure credentials.

### Q2: Upload Artifact Error - Invalid Version Format

**Example:**

```shell
curl --netrc <CNB_COMPOSER_URL>upload/monolog/monolog/xxx3.8.0 -T monolog.zip
{"errcode":3,"errmsg":"validate [Version:xxx3.8.0] failed"}
```

**Solution:**

Use [Semantic Versioning](https://semver.org/).

### Q3: About Composer 1.x

The official source stopped supporting Composer 1.x on August 1, 2025.

The cnb Composer Artifact Registry also recommends using Composer 2.x or later versions of the command line.

If you are using Composer 1.x, you can update to the latest stable version of Composer 2.x via the `composer self-update` command.
