Skip to content

Commit 27e1a51

Browse files
feat(publish-npm): enhance workflow (#62)
1 parent f3779d5 commit 27e1a51

File tree

2 files changed

+150
-39
lines changed

2 files changed

+150
-39
lines changed

.github/workflows/publish-npm.yml

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,87 @@
11
name: Publish Package NPM
2+
23
on:
34
workflow_call:
45
inputs:
56
scope:
67
description: 'NPM package scope (e.g., @iexec)'
7-
required: false
88
default: '@iexec'
99
type: string
1010
node-version:
1111
description: 'Node.js version to use'
12-
required: false
1312
default: '20'
1413
type: string
1514
registry:
1615
description: 'NPM registry URL'
17-
required: false
1816
default: 'https://registry.npmjs.org'
1917
type: string
2018
access:
21-
description: 'Package access level (public/restricted)'
22-
required: false
19+
description: 'Package access (public/restricted)'
2320
default: 'public'
2421
type: string
2522
provenance:
2623
description: 'Enable npm provenance'
27-
required: false
2824
default: true
2925
type: boolean
3026
install-command:
31-
description: 'Command to install dependencies'
32-
required: false
27+
description: 'Install dependencies command'
3328
default: 'npm ci'
3429
type: string
30+
build-command:
31+
description: 'Build package command'
32+
default: 'npm run build'
33+
type: string
34+
run-tests:
35+
description: 'Execute unit tests step'
36+
default: false
37+
type: boolean
38+
test-command:
39+
description: 'Run unit tests command'
40+
default: 'npm test --if-present'
41+
type: string
42+
lint-command:
43+
description: 'Run linting command'
44+
default: 'npm run lint --if-present'
45+
type: string
46+
type-check-command:
47+
description: 'Run type-checking command'
48+
default: 'npm run check-types --if-present'
49+
type: string
50+
format-check-command:
51+
description: 'Run format-checking command'
52+
default: 'npm run check-format --if-present'
53+
type: string
3554
environment:
36-
description: 'GitHub environment to use for deployment'
37-
required: false
55+
description: 'GitHub environment'
3856
default: 'production'
3957
type: string
4058
tag:
41-
description: 'NPM publish tag (e.g., latest, nightly)'
42-
required: false
59+
description: 'npm publish tag (e.g., latest, nightly)'
4360
default: ''
4461
type: string
62+
tag-prefix:
63+
description: 'Prefix for Git tag'
64+
default: 'v'
65+
type: string
4566
working-directory:
46-
description: "Change the workspace"
67+
description: 'Directory containing package.json'
68+
default: ''
69+
type: string
70+
artifact-name:
71+
description: 'Name of an artifact to download before the build (leave empty to skip)'
72+
default: ''
73+
type: string
74+
artifact-path:
75+
description: 'Destination path for the downloaded artifact'
76+
default: ''
77+
type: string
78+
version:
79+
description: 'Version to publish (leave empty to use package.json version)'
4780
default: ''
4881
type: string
4982
secrets:
5083
npm-token:
51-
description: 'NPM token for authentication'
84+
description: 'NPM auth token'
5285
required: true
5386

5487
jobs:
@@ -60,6 +93,13 @@ jobs:
6093
packages: write
6194
id-token: write
6295
steps:
96+
- name: Download extra artifact
97+
if: ${{ inputs.artifact-name != '' }}
98+
uses: actions/download-artifact@v4
99+
with:
100+
name: ${{ inputs.artifact-name }}
101+
path: ${{ inputs.artifact-path }}
102+
63103
- uses: actions/checkout@v4
64104

65105
- uses: actions/setup-node@v4
@@ -71,19 +111,46 @@ jobs:
71111
- name: Install dependencies
72112
working-directory: ${{ inputs.working-directory }}
73113
run: ${{ inputs.install-command }}
114+
115+
- name: Override version
116+
if: ${{ inputs.version != '' }}
117+
working-directory: ${{ inputs.working-directory }}
118+
run: |
119+
npm pkg set version="${{ inputs.version }}"
120+
121+
- name: Run build
122+
working-directory: ${{ inputs.working-directory }}
123+
run: ${{ inputs.build-command }}
124+
125+
- name: Run type checks
126+
working-directory: ${{ inputs.working-directory }}
127+
run: ${{ inputs.type-check-command }}
128+
129+
- name: check format
130+
working-directory: ${{ inputs.working-directory }}
131+
run: ${{ inputs.format-check-command }}
132+
133+
- name: Run linting
134+
working-directory: ${{ inputs.working-directory }}
135+
run: ${{ inputs.lint-command }}
136+
137+
- name: Run unit tests
138+
if: ${{ inputs.run-tests }}
139+
working-directory: ${{ inputs.working-directory }}
140+
run: ${{ inputs.test-command }}
74141

75142
- name: Publish package
76143
working-directory: ${{ inputs.working-directory }}
144+
env:
145+
NODE_AUTH_TOKEN: ${{ secrets.npm-token }}
77146
run: |
78-
TAG_OPTION=""
147+
TAG_OPT=""
79148
if [ -n "${{ inputs.tag }}" ]; then
80-
TAG_OPTION="--tag ${{ inputs.tag }}"
149+
TAG_OPT="--tag ${{ inputs.tag }}"
81150
fi
82151
83152
if [ "${{ inputs.provenance }}" = "true" ]; then
84-
npm publish --access ${{ inputs.access }} $TAG_OPTION --provenance
153+
npm publish --access ${{ inputs.access }} $TAG_OPT --provenance
85154
else
86-
npm publish --access ${{ inputs.access }} $TAG_OPTION
155+
npm publish --access ${{ inputs.access }} $TAG_OPT
87156
fi
88-
env:
89-
NODE_AUTH_TOKEN: ${{ secrets.npm-token }}

publish-npm/README.md

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,50 @@
22

33
## Overview 🌟
44

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, registry URL, and other options. The workflow performs the following actions:
5+
This reusable GitHub Actions workflow automates the process of publishing an NPM package. It is configurable via inputs
6+
for the package scope, Node.js version, registry URL, and other options. The workflow performs the following actions:
67

8+
- **Downloads Artifacts**: Downloads specified artifacts if needed. 📦
79
- **Checks Out Your Repository**: Retrieves your code. 📥
810
- **Sets Up Node.js**: Installs the specified Node.js version and configures the `.npmrc` file. ⚙️
9-
- **Installs Dependencies**: Uses `npm ci` to install the dependencies. 📦
10-
- **Publishes the Package**: Publishes the package with provenance (if enabled) and the specified access level using `npm publish`. 🎉
11+
- **Installs Dependencies**: Uses the specified install command (default: `npm ci`) to install the dependencies. 📦
12+
- **Builds the Package**: Builds the package using the specified build command (default: `npm run build`). 🔨
13+
- **Performs Type Checking**: Runs type checking if configured. 🔍
14+
- **Checks Code Formatting**: Verifies code formatting if configured. 🧹
15+
- **Runs Linting**: Performs code linting if configured. 🧹
16+
- **Runs Tests**: Executes unit tests if enabled. ✅
17+
- **Publishes the Package**: Publishes the package with provenance (if enabled) and the specified access level using
18+
`npm publish`. 🎉
1119

1220
## Workflow Inputs 🛠️
1321

14-
| **Input** | **Description** | **Required** | **Default** |
15-
|-------------------|-----------------------------------------------------------------------------------------------|--------------|--------------------------------------|
16-
| **scope** | Defines the NPM package scope (e.g., `@iexec`). | No | `@iexec` |
17-
| **node-version** | Specifies the Node.js version to use. | No | `20` |
18-
| **registry** | URL of the NPM registry. | No | `https://registry.npmjs.org` |
19-
| **access** | Package access level (public or restricted). | No | `public` |
20-
| **provenance** | Enable or disable npm provenance during publishing. | No | `true` |
21-
| **install-command** | Command to install dependencies. | No | `npm ci` |
22-
| **environment** | GitHub environment to use for deployment. | No | `production` |
22+
| **Input** | **Description** | **Required** | **Default** |
23+
|--------------------------|---------------------------------------------------------------|--------------|-------------------------------------|
24+
| **scope** | NPM package scope (e.g., `@iexec`). | No | `@iexec` |
25+
| **node-version** | Node.js version to use. | No | `20` |
26+
| **registry** | NPM registry URL. | No | `https://registry.npmjs.org` |
27+
| **access** | Package access (public or restricted). | No | `public` |
28+
| **provenance** | Enable npm provenance. | No | `true` |
29+
| **install-command** | Install dependencies command. | No | `npm ci` |
30+
| **build-command** | Build package command. | No | `npm run build` |
31+
| **run-tests** | Execute unit tests step. | No | `false` |
32+
| **test-command** | Run unit tests command. | No | `npm test --if-present` |
33+
| **lint-command** | Run linting command. | No | `npm run lint --if-present` |
34+
| **type-check-command** | Run type-checking command. | No | `npm run check-types --if-present` |
35+
| **format-check-command** | Run format-checking command. | No | `npm run check-format --if-present` |
36+
| **environment** | GitHub environment. | No | `production` |
37+
| **tag** | npm publish tag (e.g., latest, nightly). | No | `''` (empty string) |
38+
| **tag-prefix** | Prefix for Git tag. | No | `v` |
39+
| **working-directory** | Directory containing package.json. | No | `''` (empty string) |
40+
| **artifact-name** | Name of an artifact to download before the build. | No | `''` (empty string) |
41+
| **artifact-path** | Destination path for the downloaded artifact. | No | `''` (empty string) |
42+
| **version** | Version to publish (leave empty to use package.json version). | No | `''` (empty string) |
2343

2444
### Secrets 🔐
2545

26-
| **Secret** | **Description** | **Required** |
27-
|---------------|-------------------------------------|--------------|
28-
| **npm-token** | NPM token for authentication. | Yes |
46+
| **Secret** | **Description** | **Required** |
47+
|---------------|-----------------|--------------|
48+
| **npm-token** | NPM auth token. | Yes |
2949

3050
## Job and Steps ⚙️
3151

@@ -34,9 +54,9 @@ This reusable GitHub Actions workflow automates the process of publishing an NPM
3454
- **Runs On**: `ubuntu-latest`.
3555
- **Environment**: Uses the environment specified in `inputs.environment`.
3656
- **Permissions**:
37-
- `contents: read` – to access repository contents. 🔍
38-
- `packages: write` – to allow package publication. ✨
39-
- `id-token: write` – for authentication purposes. 🔑
57+
- `contents: read` – to access repository contents. 🔍
58+
- `packages: write` – to allow package publication. ✨
59+
- `id-token: write` – for authentication purposes. 🔑
4060

4161
## How to Use This Reusable Workflow 🔄
4262

@@ -57,9 +77,33 @@ This reusable GitHub Actions workflow automates the process of publishing an NPM
5777
uses: your-org/your-repo/.github/workflows/publish-npm.yml@main
5878
with:
5979
node-version: '22'
80+
build-command: 'npm run build:prod'
81+
run-tests: true
82+
test-command: 'npm run test:ci'
83+
lint-command: 'npm run lint'
84+
type-check-command: 'npm run check-types'
85+
format-check-command: 'npm run check-format'
86+
tag-prefix: 'v'
87+
# Optional: Download an artifact before building
88+
# artifact-name: 'my-build-artifact'
89+
# artifact-path: './dist'
6090
secrets:
6191
npm-token: ${{ secrets.NPM_TOKEN }}
6292
```
6393
6494
3. **Configure Secrets**
65-
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. 🔑
95+
Ensure that the `NPM_TOKEN` secret is added to your repository’s settings. This token is required to authenticate
96+
with the NPM registry during publishing. 🔑
97+
98+
## Workflow Steps in Detail 🔍
99+
100+
1. **Download Artifacts**: If `artifact-name` is provided, downloads the specified artifact using `actions/download-artifact@v4` to the path specified by `artifact-path`.
101+
2. **Checkout Repository**: Uses `actions/checkout@v4` to fetch your code.
102+
3. **Setup Node.js**: Configures Node.js with `actions/setup-node@v4`, including registry URL and scope configuration.
103+
4. **Install Dependencies**: Runs the specified install command (default: `npm ci`).
104+
5. **Build Package**: Builds the package using the specified build command (default: `npm run build`).
105+
6. **Run Type Checks**: Performs type checking using the specified command (default: `npm run check-types --if-present`).
106+
7. **Check Code Format**: Verifies code formatting using the specified command (default: `npm run check-format --if-present`).
107+
8. **Run Linting**: Performs code linting using the specified command (default: `npm run lint --if-present`).
108+
9. **Run Unit Tests**: If `run-tests` is set to `true`, executes tests using the specified test command (default: `npm test --if-present`).
109+
10. **Publish Package**: Publishes the package to the NPM registry with the specified configuration, including optional tag and provenance settings.

0 commit comments

Comments
 (0)