---
url: /en/artifact/ohpm.md
description: >
  Guide for configuring credentials and using the CNB ohpm artifact registry to
  push and pull HarmonyOS packages, covering local development and CI pipeline
  workflows.
---
## Prerequisites

1. [Create Artifact Registry](./intro.md#creating-an-artifact-registry)
2. [Obtain Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address)
3. [Create Access Token](./intro.md#creating-an-access-token)

## Local Usage

### Configure Credentials

Two methods available:

#### Command-line setup

Replace `<YOUR_TOKEN>` with your token, `<REPO_URL>` with the Artifact Registry address,
and `<REPO_URL_SUFFIX>` with the Artifact Registry address without the protocol prefix.

```bash
ohpm config set registry <REPO_URL>
ohpm config set publish_registry <REPO_URL>
ohpm config set //<REPO_URL_SUFFIX>:_auth=<YOUR_TOKEN>
```

#### Configuration file setup

Paste the following content into the .ohpmrc file in the same directory as package.json.

Replace `<YOUR_TOKEN>` with your token, `<REPO_URL>` with the Artifact Registry address,
and `<REPO_URL_SUFFIX>` with the Artifact Registry address without the protocol prefix.

```ini
registry=<REPO_URL>
publish_registry=<REPO_URL>
//<REPO_URL_SUFFIX>:_auth=<YOUR_TOKEN>
```

### Pull Artifacts

Use the following command to pull artifacts. Replace `<PACKAGE_NAME>`, `<VERSION>`, and `<REPO_URL>`.

```bash
ohpm install <PACKAGE_NAME>@<VERSION> --registry=<REPO_URL>
```

### Push Artifacts

1. Initialize oh-package.json5. Replace `<PACKAGE_NAME>` and `<VERSION>`.

```json
{
  "name": "<PACKAGE_NAME>",
  "version": "<VERSION>",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "MIT"
}
```

2. Use the following command to publish the ohpm package. Replace `<HAR_OR_TGZ_FILE>`
   with the package file path and `<REPO_URL>` with the Artifact Registry address.

```bash
ohpm publish <HAR_OR_TGZ_FILE> --publish_registry=<REPO_URL>
```

## Cloud Native Build

### Configure Credentials

In Cloud Native Build, use the built-in environment variable `CNB_TOKEN`.
Replace `<REPO_URL>` with the Artifact Registry address and
`<REPO_URL_SUFFIX>` with the address without protocol prefix.

::: tip Other Token Methods
To use plaintext tokens, replace `<YOUR_TOKEN>` with your token.
Or reference via a secret registry variable `<ENV_NAME>`.
See \[Using Tokens in Cloud Native Build and Development]
(./intro.md#using-tokens-in-workspaces-and-cloud-native-build) for details.
:::

Two methods available:

#### Command-line setup

```bash
ohpm config set registry <REPO_URL>
ohpm config set publish_registry <REPO_URL>
ohpm config set //<REPO_URL_SUFFIX>:_auth=${CNB_TOKEN}
```

#### Configuration file setup

Paste the following content into the .ohpmrc file:

```ini
registry=<REPO_URL>
publish_registry=<REPO_URL>
//<REPO_URL_SUFFIX>:_auth=${CNB_TOKEN}
```

### Pull Artifacts

```shell
ohpm install <PACKAGE_NAME>@<VERSION> --registry=<REPO_URL>
```

### Push Artifacts

```shell
ohpm publish <HAR_OR_TGZ_FILE> --publish_registry=<REPO_URL>
```

Both operations can be integrated into the build pipeline with the following `.cnb.yml` example (auth config is set once, subsequent tasks use it directly):

```yaml title=".cnb.yml"
main:
  push:
    - docker:
        image: <ohpm_image>
      stages:
        - name: configure auth
          script:
            - ohpm config set registry <REPO_URL>
            - ohpm config set publish_registry <REPO_URL>
            - ohpm config set //<REPO_URL_SUFFIX>:_auth=${CNB_TOKEN}
        - name: ohpm install
          script: ohpm install <PACKAGE_NAME>@<VERSION>
        - name: ohpm publish
          script: ohpm publish <HAR_OR_TGZ_FILE>
```

> You can keep or remove the pull/push step as needed, or separate them into different pipelines.

## Workspaces

### Configure Credentials

Same as [Cloud Native Build Configure Credentials](./ohpm.md#configure-credentials-1)

### Configure Development Image

```yaml title=".cnb.yml"
$:
  vscode:
    - docker:
        image: <ohpm_image>
```

### Pull Artifacts

Same as [Local Development Pull Artifacts](./ohpm.md#pull-artifacts)

### Push Artifacts

Same as [Local Development Push Artifacts](./ohpm.md#push-artifacts)

## More Usage

For more ohpm usage, please refer to the [ohpm official documentation](https://ohpm.openharmony.cn/).
