-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
153 lines (137 loc) · 6.06 KB
/
action.yml
File metadata and controls
153 lines (137 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: "Test"
description: "Action to test Node.js projects with support for coverage reporting and pull request annotations"
author: hoverkraft
branding:
icon: check-square
color: blue
inputs:
working-directory:
description: |
Working directory where test commands are executed.
Can be absolute or relative to the repository root.
required: false
default: "."
container:
description: "Whether running in container mode (skips checkout and node setup)"
required: false
default: "false"
command:
description: |
npm/pnpm/Yarn script command to run for testing.
This should be a script defined in your `package.json`.
The command should generate coverage report files in a standard format (Cobertura XML, lcov, etc.).
Vitest: `vitest run --reporter=default --reporter=junit --outputFile=junit.xml --coverage.enabled --coverage.reporter=lcov --coverage.reporter=text`
Jest: `jest --ci --reporters=default --reporters=jest-junit --coverage`
required: false
default: "test:ci"
coverage:
description: |
Code coverage reporter to use. Supported values:
- `github`: Parse coverage reports via [parse-ci-reports](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/parse-ci-reports) action, with GitHub summaries/PR comments
- `codecov`: Upload coverage to Codecov
- `""` or `null`: No coverage reporting
required: false
default: "github"
report-file:
description: |
Optional test and coverage report paths forwarded to the [parse-ci-reports](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/parse-ci-reports) action.
Supports multiple formats (Cobertura, OpenCover, lcov, etc.).
Provide absolute paths or paths relative to the working directory.
Multiple entries can be separated by newlines, commas, or semicolons.
When omitted, the action falls back to `auto:test,auto:coverage` detection.
required: false
default: ""
path-mapping:
description: |
Optional path mapping to adjust file paths in test and coverage reports.
See the [parse-ci-reports documentation](https://hoverkraft-tech/ci-github-common/actions/parse-ci-reports) for details.
required: false
default: ""
github-token:
description: |
GitHub token for coverage PR comments.
Required when coverage is set to `github`.
Requires permissions to create and update PR comments:
- `issues: write`
- `pull-requests: write`
required: false
default: ""
runs:
using: "composite"
steps:
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: mkdir -p ./self-test-action/ && cp -r $GITHUB_ACTION_PATH/../* ./self-test-action/
- id: setup-node
if: inputs.container != 'true'
uses: ./self-test-action/setup-node
with:
working-directory: ${{ inputs.working-directory }}
dependencies-cache: |
nx
jest
vitest
- id: get-package-manager
if: inputs.container == 'true'
uses: ./self-test-action/get-package-manager
with:
working-directory: ${{ inputs.working-directory }}
- id: run-test
name: 🧪 Run tests
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
RUN_TEST_COMMAND: ${{ inputs.container == 'true' && steps.get-package-manager.outputs.run-script-command || steps.setup-node.outputs.run-script-command }}
WORKING_DIRECTORY: ${{ inputs.working-directory }}
TEST_COMMAND: ${{ inputs.command }}
with:
script: |
const fs = require('node:fs');
const workingDirectory = process.env.WORKING_DIRECTORY;
if (!fs.existsSync(workingDirectory)) {
core.setFailed(`The specified working directory does not exist: ${workingDirectory}`);
return;
}
core.debug(`Running in working directory: ${workingDirectory}`);
process.chdir(workingDirectory);
const runScriptCommand = process.env.RUN_TEST_COMMAND;
const testCommand = process.env.TEST_COMMAND || 'test:ci';
core.info(`🧪 Running test command: ${testCommand}...`);
try {
const exitCode = await exec.exec(runScriptCommand, [testCommand], {
cwd: workingDirectory,
env: { ...process.env, CI: 'true' },
ignoreReturnCode: true
});
core.setOutput('test-exit-code', exitCode);
if (exitCode !== 0) {
core.setFailed(`Tests failed with exit code ${exitCode}`);
}
} catch (error) {
core.setOutput('test-exit-code', 1);
core.setFailed(`Test execution error: ${error.message}`);
}
- name: 📊 Parse coverage reports
if: always() && inputs.coverage == 'github'
id: parse-coverage-reports
uses: hoverkraft-tech/ci-github-common/actions/parse-ci-reports@4bfbc05c189f1177cf7d47d3060cd1cbddf04ce2 # 0.32.0
with:
working-directory: ${{ inputs.working-directory }}
report-name: "Coverage Results"
report-paths: ${{ inputs.report-file || 'auto:test,auto:coverage' }}
path-mapping: ${{ inputs.path-mapping }}
output-format: "summary,markdown"
- name: 📊 Add coverage PR comment
if: always() && inputs.coverage == 'github' && github.event_name == 'pull_request' && steps.parse-coverage-reports.outputs.markdown
uses: hoverkraft-tech/ci-github-common/actions/create-or-update-comment@4bfbc05c189f1177cf7d47d3060cd1cbddf04ce2 # 0.32.0
with:
title: "Code Coverage Report"
body: ${{ steps.parse-coverage-reports.outputs.markdown }}
- name: 📊 Upload coverage to Codecov
if: always() && inputs.coverage == 'codecov'
uses: ./self-test-action/codecov
with:
working-directory: ${{ inputs.working-directory }}
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
- shell: bash
if: always()
run: rm -fr ./self-test-action