|
| 1 | +# Publish Package NPM - Reusable Workflow Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This reusable GitHub Actions workflow automates the process of publishing an NPM package. It is configurable via inputs for the package scope, Node.js version, and registry URL. The workflow performs the following actions: |
| 6 | + |
| 7 | +- Checks out your repository code. |
| 8 | +- Sets up Node.js and configures the `.npmrc` file. |
| 9 | +- Installs package dependencies using `npm ci`. |
| 10 | +- Publishes the package with provenance and public access using `npm publish`. |
| 11 | + |
| 12 | +## Detailed Explanation |
| 13 | + |
| 14 | +### Triggering the Workflow |
| 15 | + |
| 16 | +- **`on: workflow_call`** |
| 17 | + This setting makes the workflow reusable, allowing it to be invoked by other workflows. Inputs can be passed during the call. |
| 18 | + |
| 19 | +### Workflow Inputs |
| 20 | + |
| 21 | +- **`scope`** |
| 22 | + - **Description:** Defines the NPM package scope (e.g., `@iExecBlockchainComputing`). |
| 23 | + - **Required:** Yes. |
| 24 | + |
| 25 | +- **`node-version`** |
| 26 | + - **Description:** Specifies the version of Node.js to use. |
| 27 | + - **Default:** `20` |
| 28 | + - **Required:** No. |
| 29 | + |
| 30 | +- **`registry-url`** |
| 31 | + - **Description:** URL of the NPM registry. |
| 32 | + - **Default:** `https://registry.npmjs.org` |
| 33 | + - **Required:** No. |
| 34 | + |
| 35 | +### Job and Steps |
| 36 | + |
| 37 | +- **Job Name (`build`):** |
| 38 | + - Runs on `ubuntu-latest`. |
| 39 | + - **Permissions:** |
| 40 | + - `contents: read` – to access repository contents. |
| 41 | + - `packages: write` – to allow package publication. |
| 42 | + |
| 43 | +- **Steps:** |
| 44 | + - **Checkout Repository:** |
| 45 | + Uses `actions/checkout@v4` to retrieve your code. |
| 46 | + |
| 47 | + - **Setup Node.js:** |
| 48 | + Uses `actions/setup-node@v4` to configure Node.js. This step also sets up the `.npmrc` file with the provided registry URL and scope. |
| 49 | + |
| 50 | + - **Install Dependencies:** |
| 51 | + Executes `npm ci` to install dependencies from the `package-lock.json` file. |
| 52 | + |
| 53 | + - **Publish Package:** |
| 54 | + Executes `npm publish --provenance --access public` to publish the package. |
| 55 | + - The `NODE_AUTH_TOKEN` environment variable is set from `${{ secrets.NPM_TOKEN }}` for authentication. |
| 56 | + |
| 57 | +## How to Use This Reusable Workflow |
| 58 | + |
| 59 | +1. **Save the Workflow File:** |
| 60 | + Place this YAML file (e.g., `publish-npm.yml`) in the `.github/workflows/` directory of your repository. |
| 61 | + |
| 62 | +2. **Call the Reusable Workflow:** |
| 63 | + In another workflow file (for example, triggered by a release), invoke this reusable workflow as follows: |
| 64 | + |
| 65 | + ```yaml |
| 66 | + name: Call Publish Package NPM Workflow |
| 67 | + on: |
| 68 | + release: |
| 69 | + types: [published] |
| 70 | + |
| 71 | + jobs: |
| 72 | + publish: |
| 73 | + uses: your-org/your-repo/.github/workflows/publish-npm.yml@main |
| 74 | + with: |
| 75 | + scope: '@iExecBlockchainComputing' |
| 76 | + node-version: '20' |
| 77 | + registry-url: 'https://registry.npmjs.org' |
| 78 | + secrets: |
| 79 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 80 | + ``` |
| 81 | +
|
| 82 | +3. **Configure Secrets:** |
| 83 | + Ensure that the `NPM_TOKEN` secret is added to your repository's settings. This token is required to authenticate with the NPM registry during publishing. |
0 commit comments