Skip to content

Commit eaebb9e

Browse files
committed
extract run npm script ci logic
1 parent 521babe commit eaebb9e

2 files changed

Lines changed: 69 additions & 58 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: 'Run NPM Script'
2+
description: 'Run an npm script if present, and output status'
3+
4+
inputs:
5+
script:
6+
description: 'The npm script to run (e.g., build, lint:check, test)'
7+
required: true
8+
default: ''
9+
working-directory:
10+
description: 'Directory containing package.json (optional)'
11+
required: false
12+
default: '.'
13+
14+
outputs:
15+
status:
16+
description: 'success, failure, or notpresent'
17+
value: ${{ steps.run-script.outputs.status }}
18+
19+
runs:
20+
using: 'composite'
21+
steps:
22+
- name: Run npm script
23+
id: run-script
24+
shell: bash
25+
env:
26+
WORKING_DIRECTORY: ${{ inputs.working-directory }}
27+
SCRIPT: ${{ inputs.script }}
28+
run: |
29+
cd "$WORKING_DIRECTORY"
30+
if ! [ -f package.json ]; then
31+
echo "No package.json found. Skipping $SCRIPT."
32+
echo "status=notpresent" >> "$GITHUB_OUTPUT"
33+
exit 0
34+
fi
35+
if ! jq -e --arg s "$SCRIPT" '.scripts | has($s)' package.json > /dev/null; then
36+
echo "script $SCRIPT not found in package.json. Skipping."
37+
echo "status=notpresent" >> "$GITHUB_OUTPUT"
38+
exit 0
39+
fi
40+
if npm run "$SCRIPT"; then
41+
echo "status=success" >> "$GITHUB_OUTPUT"
42+
else
43+
echo "status=failure" >> "$GITHUB_OUTPUT"
44+
fi

.github/workflows/run_npm_ci_scripts.yml

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ jobs:
4141
cache: 'npm'
4242
registry-url: 'https://npm.pkg.github.com/'
4343
cache-dependency-path: '**/package-lock.json'
44-
always-auth: true
4544

4645
- name: Install dependencies
4746
run: npm ci
@@ -52,74 +51,42 @@ jobs:
5251
continue-on-error: true
5352

5453
- name: Build
54+
uses: ./.github/actions/run-npm-script
5555
id: build
56-
continue-on-error: true
57-
run: |
58-
if ! [ -f package.json ] || ! jq -e '.scripts.build' package.json > /dev/null; then
59-
echo "No build script found in package.json."
60-
echo "build_status=notpresent" >> $GITHUB_OUTPUT
61-
exit 0
62-
fi
63-
if npm run build; then
64-
echo "build_status=success" >> $GITHUB_OUTPUT
65-
else
66-
echo "build_status=failure" >> $GITHUB_OUTPUT
67-
fi
56+
with:
57+
working-directory: '.'
58+
script: build
6859

6960
- name: Lint
61+
uses: ./.github/actions/run-npm-script
7062
id: lint
71-
continue-on-error: true
72-
run: |
73-
if ! [ -f package.json ] || ! jq -e '.scripts.["lint:check"]' package.json > /dev/null; then
74-
echo "No lint script found in package.json."
75-
echo "lint_status=notpresent" >> $GITHUB_OUTPUT
76-
exit 0
77-
fi
78-
if npm run lint:check; then
79-
echo "lint_status=success" >> $GITHUB_OUTPUT
80-
else
81-
echo "lint_status=failure" >> $GITHUB_OUTPUT
82-
fi
63+
with:
64+
working-directory: '.'
65+
script: lint:check
8366

8467
- name: Format
68+
uses: ./.github/actions/run-npm-script
8569
id: format
86-
continue-on-error: true
87-
run: |
88-
if ! [ -f package.json ] || ! jq -e '.scripts.["format:check"]' package.json > /dev/null; then
89-
echo "No format script found in package.json."
90-
echo "format_status=notpresent" >> $GITHUB_OUTPUT
91-
exit 0
92-
fi
93-
if npm run format:check; then
94-
echo "format_status=success" >> $GITHUB_OUTPUT
95-
else
96-
echo "format_status=failure" >> $GITHUB_OUTPUT
97-
fi
70+
with:
71+
working-directory: '.'
72+
script: format:check
9873

9974
- name: Test
75+
uses: ./.github/actions/run-npm-script
10076
id: test
101-
continue-on-error: true
102-
run: |
103-
if ! [ -f package.json ] || ! jq -e '.scripts.test' package.json > /dev/null; then
104-
echo "No test script found in package.json."
105-
echo "test_status=notpresent" >> $GITHUB_OUTPUT
106-
exit 0
107-
fi
108-
if npm test; then
109-
echo "test_status=success" >> $GITHUB_OUTPUT
110-
else
111-
echo "test_status=failure" >> $GITHUB_OUTPUT
112-
fi
77+
with:
78+
working-directory: '.'
79+
script: test
11380

11481
- name: Job Summary
11582
if: always()
11683
env:
11784
AUDIT_STATUS: ${{ steps.audit-npm.outputs.gate_passed == 'true' && 'success' || 'failure' }}
11885
AUDIT_SUMMARY: ${{ steps.audit-npm.outputs.gate_summary }}
119-
BUILD_STATUS: ${{ steps.build.outputs.build_status || steps.build.outcome }}
120-
LINT_STATUS: ${{ steps.lint.outputs.lint_status || steps.lint.outcome }}
121-
FORMAT_STATUS: ${{ steps.format.outputs.format_status || steps.format.outcome }}
122-
TEST_STATUS: ${{ steps.test.outputs.test_status || steps.test.outcome }}
86+
BUILD_STATUS: ${{ steps.build.outputs.status || steps.build.outcome }}
87+
LINT_STATUS: ${{ steps.lint.outputs.status || steps.lint.outcome }}
88+
FORMAT_STATUS: ${{ steps.format.outputs.status || steps.format.outcome }}
89+
TEST_STATUS: ${{ steps.test.outputs.status || steps.test.outcome }}
12390
run: |
12491
status_emoji () {
12592
case "$1" in
@@ -146,13 +113,13 @@ jobs:
146113
cat job-summary.md >> "$GITHUB_STEP_SUMMARY"
147114
148115
- name: Set Run Outcome
149-
if: ${{ steps.audit-npm.outputs.gate_passed == 'false' || steps.build.outputs.build_status == 'failure' || steps.lint.outputs.lint_status == 'failure' || steps.format.outputs.format_status == 'failure' || steps.test.outputs.test_status == 'failure' }}
116+
if: ${{ steps.audit-npm.outputs.gate_passed == 'false' || steps.build.outputs.status == 'failure' || steps.lint.outputs.status == 'failure' || steps.format.outputs.status == 'failure' || steps.test.outputs.status == 'failure' }}
150117
env:
151118
AUDIT_STATUS: ${{ steps.audit-npm.outputs.gate_passed == 'true' && 'passed' || 'failed' }}
152-
BUILD_STATUS: ${{ steps.build.outputs.build_status == 'success' && 'passed' || 'failed' }}
153-
LINT_STATUS: ${{ steps.lint.outputs.lint_status == 'success' && 'passed' || 'failed' }}
154-
FORMAT_STATUS: ${{ steps.format.outputs.format_status == 'success' && 'passed' || 'failed' }}
155-
TEST_STATUS: ${{ steps.test.outputs.test_status == 'success' && 'passed' || 'failed' }}
119+
BUILD_STATUS: ${{ steps.build.outputs.status == 'success' && 'passed' || 'failed' }}
120+
LINT_STATUS: ${{ steps.lint.outputs.status == 'success' && 'passed' || 'failed' }}
121+
FORMAT_STATUS: ${{ steps.format.outputs.status == 'success' && 'passed' || 'failed' }}
122+
TEST_STATUS: ${{ steps.test.outputs.status == 'success' && 'passed' || 'failed' }}
156123
run: |
157124
echo "❌ One or more gate steps failed:"
158125
echo " Audit Gate: $AUDIT_STATUS"

0 commit comments

Comments
 (0)