Skip to content

Commit b27a09b

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

2 files changed

Lines changed: 63 additions & 52 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
18+
runs:
19+
using: 'composite'
20+
steps:
21+
- name: Run npm script
22+
id: run-script
23+
shell: bash
24+
env:
25+
WORKING_DIRECTORY: ${{ inputs.working-directory }}
26+
SCRIPT: ${{ inputs.script }}
27+
run: |
28+
cd "$WORKING_DIRECTORY"
29+
if ! [ -f package.json ]; then
30+
echo "No package.json found. Skipping $SCRIPT."
31+
echo "status=notpresent" >> "$GITHUB_OUTPUT"
32+
exit 0
33+
fi
34+
if ! jq -e --arg s "$SCRIPT" '.scripts[$s]' package.json > /dev/null; then
35+
echo "script `$SCRIPT` not found in package.json. Skipping."
36+
echo "status=notpresent" >> "$GITHUB_OUTPUT"
37+
exit 0
38+
fi
39+
if npm run "$SCRIPT"; then
40+
echo "status=success" >> "$GITHUB_OUTPUT"
41+
else
42+
echo "status=failure" >> "$GITHUB_OUTPUT"
43+
fi

.github/workflows/run_npm_ci_scripts.yml

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -52,74 +52,42 @@ jobs:
5252
continue-on-error: true
5353

5454
- name: Build
55+
uses: ./.github/actions/run-npm-script
5556
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
57+
with:
58+
working-directory: '.'
59+
script: build
6860

6961
- name: Lint
62+
uses: ./.github/actions/run-npm-script
7063
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
64+
with:
65+
working-directory: '.'
66+
script: lint:check
8367

8468
- name: Format
69+
uses: ./.github/actions/run-npm-script
8570
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
71+
with:
72+
working-directory: '.'
73+
script: format:check
9874

9975
- name: Test
76+
uses: ./.github/actions/run-npm-script
10077
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
78+
with:
79+
working-directory: '.'
80+
script: test
11381

11482
- name: Job Summary
11583
if: always()
11684
env:
11785
AUDIT_STATUS: ${{ steps.audit-npm.outputs.gate_passed == 'true' && 'success' || 'failure' }}
11886
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 }}
87+
BUILD_STATUS: ${{ steps.build.outputs.status || steps.build.outcome }}
88+
LINT_STATUS: ${{ steps.lint.outputs.status || steps.lint.outcome }}
89+
FORMAT_STATUS: ${{ steps.format.outputs.status || steps.format.outcome }}
90+
TEST_STATUS: ${{ steps.test.outputs.status || steps.test.outcome }}
12391
run: |
12492
status_emoji () {
12593
case "$1" in

0 commit comments

Comments
 (0)