Skip to content

Commit 7e84f65

Browse files
committed
feat(publish-npm): init publish npm workflow
1 parent 8271151 commit 7e84f65

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

.github/workflows/publish-npm.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Publish Package NPM
2+
on:
3+
workflow_call:
4+
inputs:
5+
scope:
6+
description: 'NPM package scope (e.g., @iExecBlockchainComputing)'
7+
required: true
8+
type: string
9+
node-version:
10+
description: 'Node.js version to use'
11+
required: false
12+
default: '20'
13+
type: string
14+
registry-url:
15+
description: 'NPM registry URL'
16+
required: false
17+
default: 'https://registry.npmjs.org'
18+
type: string
19+
20+
jobs:
21+
build:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
25+
packages: write
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
# Setup Node and configure .npmrc for publishing to GitHub Packages
30+
- uses: actions/setup-node@v4
31+
with:
32+
node-version: ${{ inputs.node-version }}
33+
registry-url: ${{ inputs.registry-url }}
34+
scope: ${{ inputs.scope }}
35+
36+
- run: npm ci
37+
- run: npm publish --provenance --access public
38+
env:
39+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ This repository contains a reusable workflow for iExec. It is a monorepo that co
88
This workflow builds a Docker image from a Dockerfile. It is a reusable workflow that can be used in other workflows.
99

1010
### [Release Please](./release-please)
11-
This workflow uses the [release-please-action](https://github.com/googleapis/release-please-action) to automate the release of a project.
11+
This workflow uses the [release-please-action](https://github.com/googleapis/release-please-action) to automate the release of a project.
12+
13+
### [Publish NPM Package](./publish-npm)
14+
This workflow publishes an NPM package to the NPM registry.

publish-npm/CHANGELOG.md

Whitespace-only changes.

publish-npm/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.

publish-npm/version.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)