Skip to content

Commit 00a0612

Browse files
committed
feat(setup-node)!: default workspace installs to disabled lifecycle scripts
1 parent 0d2f1d6 commit 00a0612

18 files changed

Lines changed: 197 additions & 8 deletions

‎.github/workflows/__test-action-package-create.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
artifact-suffix: pnpm
2626
- working-directory: tests/pnpm-package-manager
2727
artifact-suffix: pnpm-package-manager
28+
ignore-scripts: "false"
2829
- working-directory: tests/yarn
2930
artifact-suffix: yarn
3031
steps:
@@ -38,6 +39,7 @@ jobs:
3839
uses: $/actions/package/create
3940
with:
4041
working-directory: ${{ matrix.working-directory }}
42+
ignore-scripts: ${{ matrix.ignore-scripts || 'true' }}
4143
artifact-name: package-tarball-${{ github.job }}-${{ matrix.artifact-suffix }}-${{ github.run_id }}-${{ github.run_attempt }}
4244

4345
- name: Assert - Check "package/create" outputs

‎.github/workflows/__test-action-setup-node.yml‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,70 @@ jobs:
5252
echo "setup-node outputs run-script-command is not valid"
5353
exit 1
5454
fi
55+
56+
- name: Arrange - Local dependency and project with installation hooks
57+
id: lifecycle-fixture
58+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
59+
env:
60+
SOURCE_DIRECTORY: ${{ matrix.working-directory }}
61+
RUN_SCRIPT_COMMAND: ${{ steps.act-setup-node.outputs.run-script-command }}
62+
with:
63+
script: |
64+
const fs = require('node:fs');
65+
const path = require('node:path');
66+
const directory = fs.mkdtempSync(path.join(process.env.GITHUB_WORKSPACE, 'install-hooks-'));
67+
const dependency = path.join(directory, 'dependency');
68+
fs.mkdirSync(dependency);
69+
const hook = `node -e "require('node:fs').writeFileSync('install-hook-ran', '')"`;
70+
fs.writeFileSync(path.join(dependency, 'package.json'), JSON.stringify({
71+
name: 'lifecycle-dependency', version: '1.0.0', scripts: { postinstall: hook },
72+
}));
73+
await exec.exec('npm', ['pack', '--ignore-scripts', '--pack-destination', directory], { cwd: dependency });
74+
const source = JSON.parse(fs.readFileSync(path.join(process.env.SOURCE_DIRECTORY, 'package.json'), 'utf8'));
75+
fs.writeFileSync(path.join(directory, 'package.json'), JSON.stringify({
76+
name: 'lifecycle-project', version: '1.0.0', private: true,
77+
packageManager: source.packageManager,
78+
dependencies: { 'lifecycle-dependency': 'file:./lifecycle-dependency-1.0.0.tgz' },
79+
scripts: { postinstall: hook, build: `node -e "require('node:fs').writeFileSync('build-ran', '')"` },
80+
}));
81+
fs.copyFileSync('.node-version', path.join(directory, '.node-version'));
82+
const manager = process.env.RUN_SCRIPT_COMMAND.split(' ')[0];
83+
if (manager === 'pnpm') {
84+
const { stdout } = await exec.getExecOutput('pnpm', ['--version']);
85+
const approval = parseInt(stdout, 10) < 11 ? 'lifecycle-dependency' : 'lifecycle-dependency@file:lifecycle-dependency-1.0.0.tgz';
86+
fs.writeFileSync(path.join(directory, 'pnpm-workspace.yaml'), `allowBuilds:\n ${approval}: true\n`);
87+
}
88+
const installArgs = ['install', '--ignore-scripts'];
89+
if (manager === 'pnpm') installArgs.push('--no-frozen-lockfile');
90+
await exec.exec(manager, installArgs, { cwd: directory });
91+
fs.rmSync(path.join(directory, 'node_modules'), { recursive: true, force: true });
92+
core.setOutput('directory', directory);
93+
94+
- name: Act - Install with default script policy
95+
id: default-install
96+
uses: $/actions/setup-node
97+
with:
98+
working-directory: ${{ steps.lifecycle-fixture.outputs.directory }}
99+
100+
- name: Assert - Installation hooks are disabled and explicit builds still run
101+
working-directory: ${{ steps.lifecycle-fixture.outputs.directory }}
102+
env:
103+
RUN_SCRIPT_COMMAND: ${{ steps.default-install.outputs.run-script-command }}
104+
run: |
105+
test ! -f install-hook-ran
106+
test ! -f node_modules/lifecycle-dependency/install-hook-ran
107+
$RUN_SCRIPT_COMMAND build
108+
test -f build-ran
109+
rm -rf node_modules
110+
111+
- name: Act - Opt in to installation hooks
112+
uses: $/actions/setup-node
113+
with:
114+
working-directory: ${{ steps.lifecycle-fixture.outputs.directory }}
115+
ignore-scripts: "false"
116+
117+
- name: Assert - Project and approved dependency hooks ran
118+
working-directory: ${{ steps.lifecycle-fixture.outputs.directory }}
119+
run: |
120+
test -f install-hook-ran
121+
test -f node_modules/lifecycle-dependency/install-hook-ran

‎.github/workflows/__test-release.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ jobs:
244244
working-directory: ${{ steps.workspace.outputs.directory }}
245245
package-directory: packages/cli
246246
prebuilt: "true"
247+
ignore-scripts: "false"
247248
build-artifact-id: ${{ steps.build.outputs.artifact-id }}
248249
build-artifact-path: ${{ steps.workspace.outputs.directory }}
249250
artifact-name: workspace-package-${{ matrix.manager }}-${{ github.run_id }}-${{ github.run_attempt }}

‎.github/workflows/__test-workflow-continuous-integration.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
with:
7272
working-directory: tests/npm
7373
prebuilt: "true"
74+
ignore-scripts: "false"
7475
build-artifact-id: ${{ steps.fixture.outputs.artifact-id }}
7576
build-artifact-path: ${{ github.workspace }}/tests/npm
7677
- name: Verify compiled output is unchanged and dependencies were not installed
@@ -156,6 +157,7 @@ jobs:
156157
security-events: write
157158
with:
158159
working-directory: tests/npm
160+
ignore-scripts: false
159161
code-ql: typescript
160162
build: |
161163
{

‎.github/workflows/continuous-integration.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ on:
5757
permissions: {}
5858
jobs:
5959
continuous-integration:
60-
uses: hoverkraft-tech/ci-github-nodejs/.github/workflows/continuous-integration.yml@1f81dd00d941680ab227a6eae5378790d6242e02 # 1.0.0
60+
uses: hoverkraft-tech/ci-github-nodejs/.github/workflows/continuous-integration.yml@<sha>
6161
permissions:
6262
contents: read
6363
id-token: write
@@ -82,6 +82,13 @@ jobs:
8282
# Defaults to `GITHUB_TOKEN` if not provided.
8383
github-token: ""
8484
with:
85+
# Disable project and dependency lifecycle scripts during installation in build, lint and test jobs.
86+
# Set false to allow installation hooks, subject to the package manager's script approval settings.
87+
# Explicit commands are unaffected. Does not apply in container mode, which skips installation.
88+
#
89+
# Default: `true`
90+
ignore-scripts: true
91+
8592
# JSON array of runner(s) to use.
8693
# See https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job.
8794
#
@@ -240,6 +247,9 @@ jobs:
240247

241248
| **Input** | **Description** | **Required** | **Type** | **Default** |
242249
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ----------- | ------------------- |
250+
| **`ignore-scripts`** | Disable project and dependency lifecycle scripts during installation in build, lint and test jobs. | **false** | **boolean** | `true` |
251+
| | Set false to allow installation hooks, subject to the package manager's script approval settings. | | | |
252+
| | Explicit commands are unaffected. Does not apply in container mode, which skips installation. | | | |
243253
| **`runs-on`** | JSON array of runner(s) to use. | **false** | **string** | `["ubuntu-latest"]` |
244254
| | See <https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job>. | | | |
245255
| **`build`** | Build parameters. Must be a string or a JSON object. | **false** | **string** | `build` |

‎.github/workflows/continuous-integration.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ name: Node.js Continuous Integration
1010
on:
1111
workflow_call:
1212
inputs:
13+
ignore-scripts:
14+
description: |
15+
Disable project and dependency lifecycle scripts during installation in build, lint and test jobs.
16+
Set false to allow installation hooks, subject to the package manager's script approval settings.
17+
Explicit commands are unaffected. Does not apply in container mode, which skips installation.
18+
type: boolean
19+
default: true
1320
runs-on:
1421
description: |
1522
JSON array of runner(s) to use.
@@ -512,6 +519,7 @@ jobs:
512519
- name: Run lint
513520
uses: $/actions/lint
514521
with:
522+
ignore-scripts: ${{ inputs.ignore-scripts }}
515523
working-directory: ${{ needs.setup.outputs.working-directory }}
516524
container: ${{ inputs.container != '' && 'true' || 'false' }}
517525
command: ${{ steps.preparel-lint-options.outputs.command }}
@@ -539,6 +547,7 @@ jobs:
539547
if: needs.setup.outputs.build-commands
540548
uses: $/actions/build
541549
with:
550+
ignore-scripts: ${{ inputs.ignore-scripts }}
542551
container: ${{ inputs.container != '' && 'true' || 'false' }}
543552
working-directory: ${{ needs.setup.outputs.working-directory }}
544553
build-secrets: ${{ secrets.build-secrets }}
@@ -599,6 +608,7 @@ jobs:
599608
- name: Run tests
600609
uses: $/actions/test
601610
with:
611+
ignore-scripts: ${{ inputs.ignore-scripts }}
602612
working-directory: ${{ needs.setup.outputs.working-directory }}
603613
container: ${{ inputs.container != '' && 'true' || 'false' }}
604614
command: ${{ steps.prepare-test-options.outputs.command }}

‎.github/workflows/release.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ Include generated declarations, static files copied into package output and any
292292
needed documentation output. Remove npm publication from push CI when enabling
293293
this manual and scheduled release caller.
294294

295+
Dependency installation skips project and dependency lifecycle scripts by default.
296+
If your build needs installation hooks, set `ignore-scripts: false` on the build
297+
workflow or action and configure any script approvals required by your package
298+
manager. Explicit build, lint and test commands still run. Preparation and prebuilt
299+
release packaging always disable lifecycle scripts; this opt-in cannot enable them.
300+
295301
Release uses [Package](../../actions/package/create/README.md) with `prebuilt: "true"`.
296302
npm packaging skips dependency installation. pnpm packaging installs the frozen
297303
workspace with lifecycle scripts disabled so native `pnpm pack` can resolve

‎actions/build/README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ Action to build Node.js projects with support for custom commands, environment v
3535
## Usage
3636

3737
````yaml
38-
- uses: hoverkraft-tech/ci-github-nodejs/actions/build@81a4bca8445b7b273455ff26a9a5c3c31c86b2a1 # 0.25.0
38+
- uses: hoverkraft-tech/ci-github-nodejs/actions/build@<sha>
3939
with:
40+
# Disable project and dependency lifecycle scripts during installation.
41+
# Set false to allow installation hooks, subject to the package manager's script approval settings.
42+
# Explicit commands are unaffected. Does not apply in container mode, which skips installation.
43+
#
44+
# Default: `true`
45+
ignore-scripts: "true"
46+
4047
# Working directory where the build commands are executed.
4148
# Can be absolute or relative to the repository root.
4249
#
@@ -93,6 +100,9 @@ Action to build Node.js projects with support for custom commands, environment v
93100

94101
| **Input** | **Description** | **Required** | **Default** |
95102
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
103+
| **`ignore-scripts`** | Disable project and dependency lifecycle scripts during installation. | **false** | `true` |
104+
| | Set false to allow installation hooks, subject to the package manager's script approval settings. | | |
105+
| | Explicit commands are unaffected. Does not apply in container mode, which skips installation. | | |
96106
| **`working-directory`** | Working directory where the build commands are executed. | **false** | `.` |
97107
| | Can be absolute or relative to the repository root. | | |
98108
| **`build-commands`** | List of build commands to execute, one per line. | **true** | - |

‎actions/build/action.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ branding:
66
color: blue
77

88
inputs:
9+
ignore-scripts:
10+
description: |
11+
Disable project and dependency lifecycle scripts during installation.
12+
Set false to allow installation hooks, subject to the package manager's script approval settings.
13+
Explicit commands are unaffected. Does not apply in container mode, which skips installation.
14+
default: "true"
915
working-directory:
1016
description: |
1117
Working directory where the build commands are executed.
@@ -78,6 +84,7 @@ runs:
7884
uses: $/actions/setup-node
7985
with:
8086
working-directory: ${{ steps.working-directory.outputs.absolute-path }}
87+
ignore-scripts: ${{ inputs.ignore-scripts }}
8188
dependencies-cache: |
8289
nx
8390
gatsby

‎actions/lint/README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ Action to lint Node.js projects with support for pull request reporting and anno
3535
## Usage
3636

3737
```yaml
38-
- uses: hoverkraft-tech/ci-github-nodejs/actions/lint@81a4bca8445b7b273455ff26a9a5c3c31c86b2a1 # 0.25.0
38+
- uses: hoverkraft-tech/ci-github-nodejs/actions/lint@<sha>
3939
with:
40+
# Disable project and dependency lifecycle scripts during installation.
41+
# Set false to allow installation hooks, subject to the package manager's script approval settings.
42+
# Explicit commands are unaffected. Does not apply in container mode, which skips installation.
43+
#
44+
# Default: `true`
45+
ignore-scripts: "true"
46+
4047
# Working directory where lint commands are executed.
4148
# Can be absolute or relative to the repository root.
4249
#
@@ -76,6 +83,9 @@ Action to lint Node.js projects with support for pull request reporting and anno
7683
7784
| **Input** | **Description** | **Required** | **Default** |
7885
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
86+
| **`ignore-scripts`** | Disable project and dependency lifecycle scripts during installation. | **false** | `true` |
87+
| | Set false to allow installation hooks, subject to the package manager's script approval settings. | | |
88+
| | Explicit commands are unaffected. Does not apply in container mode, which skips installation. | | |
7989
| **`working-directory`** | Working directory where lint commands are executed. | **false** | `.` |
8090
| | Can be absolute or relative to the repository root. | | |
8191
| **`container`** | Whether running in container mode (skips checkout and node setup) | **false** | `false` |

0 commit comments

Comments
 (0)