Skip to content

Commit f4459c7

Browse files
feat(publish-npm): enhance additional inputs (#18)
1 parent dcfa50f commit f4459c7

File tree

5 files changed

+159
-1
lines changed

5 files changed

+159
-1
lines changed

.github/workflows/publish-npm.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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:
15+
description: 'NPM registry URL'
16+
required: false
17+
default: 'https://registry.npmjs.org'
18+
type: string
19+
access:
20+
description: 'Package access level (public/restricted)'
21+
required: false
22+
default: 'public'
23+
type: string
24+
provenance:
25+
description: 'Enable npm provenance'
26+
required: false
27+
default: true
28+
type: boolean
29+
install-command:
30+
description: 'Command to install dependencies'
31+
required: false
32+
default: 'npm install'
33+
type: string
34+
environment:
35+
description: 'GitHub environment to use for deployment'
36+
required: false
37+
default: 'production'
38+
type: string
39+
secrets:
40+
npm-token:
41+
description: 'NPM token for authentication'
42+
required: true
43+
44+
jobs:
45+
build:
46+
runs-on: ubuntu-latest
47+
environment: ${{ inputs.environment }}
48+
permissions:
49+
contents: read
50+
packages: write
51+
id-token: write
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- uses: actions/setup-node@v4
56+
with:
57+
node-version: ${{ inputs.node-version }}
58+
registry-url: ${{ inputs.registry }}
59+
scope: ${{ inputs.scope }}
60+
61+
- name: Install dependencies
62+
run: ${{ inputs.install-command }}
63+
64+
- name: Publish package
65+
run: |
66+
if [ "${{ inputs.provenance }}" = "true" ]; then
67+
npm publish --access ${{ inputs.access }} --provenance
68+
else
69+
npm publish --access ${{ inputs.access }}
70+
fi
71+
env:
72+
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)