-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
218 lines (194 loc) · 8.67 KB
/
action.yml
File metadata and controls
218 lines (194 loc) · 8.67 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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@5e8d0e6d1e76d8577a070db6d0128a91b1c9d5ad # 0.30.2
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@5e8d0e6d1e76d8577a070db6d0128a91b1c9d5ad # 0.30.2
with:
title: "Code Coverage Report"
body: ${{ steps.parse-coverage-reports.outputs.markdown }}
# Check and install dependencies for codecov in container mode
- name: Check and install Codecov dependencies
if: always() && inputs.coverage == 'codecov' && inputs.container == 'true'
id: check-codecov-deps
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
// Check which dependencies are missing
const deps = {"git": "git", "curl": "curl", "gpg": "gnupg.org"};
const missingDeps = [];
for (const [dep, pkg] of Object.entries(deps)) {
try {
await io.which(dep, true);
} catch {
missingDeps.push(pkg);
}
}
if (missingDeps.length > 0) {
core.setOutput('missing-deps', missingDeps.join(' '));
}
- name: Install missing Codecov dependencies
if: always() && steps.check-codecov-deps.outputs.missing-deps
uses: pkgxdev/setup@f211ee4db3110b42e5a156282372527e7c1ed723 # v4.0.0
with:
+: ${{ steps.check-codecov-deps.outputs.missing-deps }}
# Fix pkgxdev gnupg's gpgconf.ctl which contains unexpanded environment variables
- name: Fix GPG configuration
if: always() && contains(steps.check-codecov-deps.outputs.missing-deps, 'gnupg.org')
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
// Find and remove the malformed gpgconf.ctl file that contains unexpanded shell variables
// The pkgxdev gnupg installs gpgconf.ctl next to the gpgconf binary
try {
const gpgconfPath = await io.which('gpgconf', false);
if (gpgconfPath) {
const gpgconfCtl = path.join(path.dirname(gpgconfPath), 'gpgconf.ctl');
if (fs.existsSync(gpgconfCtl)) {
core.info(`Removing malformed gpgconf.ctl: ${gpgconfCtl}`);
await io.rmRF(gpgconfCtl);
}
}
} catch (error) {
core.warning(`Failed to check/remove gpgconf.ctl: ${error.message}`);
}
// Ensure GNUPGHOME is set up correctly
const gnupgHome = path.join(os.homedir(), '.gnupg');
await io.mkdirP(gnupgHome);
fs.chmodSync(gnupgHome, 0o700);
- name: 📊 Upload coverage to Codecov
if: always() && inputs.coverage == 'codecov'
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
directory: ${{ inputs.working-directory }}
root_dir: ${{ inputs.working-directory }}
working-directory: ${{ inputs.working-directory }}
use_oidc: true
disable_telem: true
fail_ci_if_error: false
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
- shell: bash
if: always()
run: rm -fr ./self-test-action