Skip to content

Commit e711e67

Browse files
Copilotneilime
andcommitted
feat(workflows): add build artifact ID output support to release workflow
Co-authored-by: neilime <[email protected]>
1 parent e773dc8 commit e711e67

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

.github/workflows/release.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ jobs:
123123
| **`build`** | Build parameters. Must be a string or a JSON object. | **false** | **string** | `build` |
124124
| | Set to empty string to disable build step. | | | |
125125
| | For string, provide a list of commands to run during the build step, one per line. | | | |
126-
| | For JSON object, provide `commands` array and optional `env` object. | | | |
126+
| | For JSON object, provide `commands` array, optional `env` object, and `artifact`. | | | |
127127
| **`docs`** | Documentation generation parameters. | **false** | **string** | - |
128128
| | Set to empty string or `false` to disable. | | | |
129129
| | Set to `true` for default command (`docs`). | | | |
@@ -161,11 +161,12 @@ jobs:
161161

162162
## Outputs
163163

164-
| **Output** | **Description** |
165-
| ---------------------- | ----------------------------------------------- |
166-
| **`version`** | The version of the published package. |
167-
| **`package-name`** | The name of the published package. |
168-
| **`docs-artifact-id`** | ID of the documentation artifact (if uploaded). |
164+
| **Output** | **Description** |
165+
| ----------------------- | ----------------------------------------------- |
166+
| **`version`** | The version of the published package. |
167+
| **`package-name`** | The name of the published package. |
168+
| **`build-artifact-id`** | ID of the build artifact (if uploaded). |
169+
| **`docs-artifact-id`** | ID of the documentation artifact (if uploaded). |
169170

170171
<!-- outputs:end -->
171172

.github/workflows/release.yml

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ on:
2525
2626
- `commands`: Array of commands to run during the build step.
2727
- `env`: Object of environment variables to set during the build step.
28+
- `artifact`: String or array of strings specifying paths to artifacts to upload after the build.
2829
2930
Example:
3031
```json
@@ -35,7 +36,11 @@ on:
3536
],
3637
"env": {
3738
"NODE_ENV": "production"
38-
}
39+
},
40+
"artifact": [
41+
"dist/",
42+
"packages/package-a/build/"
43+
]
3944
}
4045
```
4146
type: string
@@ -154,6 +159,9 @@ on:
154159
package-name:
155160
description: "The name of the published package."
156161
value: ${{ jobs.release.outputs.package-name }}
162+
build-artifact-id:
163+
description: "ID of the build artifact (if uploaded)."
164+
value: ${{ jobs.release.outputs.build-artifact-id }}
157165
docs-artifact-id:
158166
description: "ID of the documentation artifact (if uploaded)."
159167
value: ${{ jobs.release.outputs.docs-artifact-id }}
@@ -170,6 +178,7 @@ jobs:
170178
registry-scope: ${{ steps.parse-registry.outputs.registry-scope }}
171179
build-commands: ${{ steps.parse-build.outputs.commands }}
172180
build-env: ${{ steps.parse-build.outputs.env }}
181+
build-artifact: ${{ steps.parse-build.outputs.artifact }}
173182
docs-command: ${{ steps.parse-docs.outputs.command }}
174183
docs-output: ${{ steps.parse-docs.outputs.output }}
175184
docs-artifact: ${{ steps.parse-docs.outputs.artifact }}
@@ -226,9 +235,13 @@ jobs:
226235
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
227236
env:
228237
BUILD_INPUT: ${{ inputs.build }}
238+
WORKING_DIRECTORY: ${{ inputs.working-directory }}
229239
with:
230240
script: |
241+
const path = require('node:path');
242+
231243
const buildInput = process.env.BUILD_INPUT.trim();
244+
const workingDirectory = process.env.WORKING_DIRECTORY || '.';
232245
233246
if (!buildInput) {
234247
core.info('Build step disabled');
@@ -258,6 +271,42 @@ jobs:
258271
else {
259272
commands = build.commands ?? ['build'];
260273
env = build.env ?? {};
274+
275+
if (build.artifact) {
276+
let buildArtifact = build.artifact;
277+
278+
if (typeof buildArtifact === 'string' || Array.isArray(buildArtifact)) {
279+
buildArtifact = {
280+
paths: buildArtifact
281+
};
282+
}
283+
284+
if (typeof buildArtifact.paths === 'string') {
285+
buildArtifact.paths = buildArtifact.paths.split('\n');
286+
} else if (!Array.isArray(buildArtifact.paths)) {
287+
return core.setFailed('Build artifact paths must be a string or an array of strings');
288+
}
289+
290+
buildArtifact.paths = buildArtifact.paths
291+
.map(artifact => artifact.trim())
292+
.filter(Boolean)
293+
.map(artifact => {
294+
// FIXME: Workaround to preserve full path to artifact
295+
const fullpath = artifact.startsWith('/') ? artifact : path.join(workingDirectory, artifact);
296+
297+
// Add a wildcard to the first folder of the path
298+
return fullpath.replace(/\/([^/]+)/, '/*$1');
299+
}).join('\n');
300+
301+
if (!buildArtifact.paths) {
302+
return core.setFailed('No valid build artifact paths found');
303+
}
304+
305+
// Generate a unique name for the artifact
306+
buildArtifact.name = `${process.env.GITHUB_JOB}-build-${process.env.GITHUB_RUN_ID}-${Math.random().toString(36).substring(2, 8)}`;
307+
308+
core.setOutput('artifact', JSON.stringify(buildArtifact));
309+
}
261310
}
262311
}
263312
@@ -327,6 +376,7 @@ jobs:
327376
outputs:
328377
version: ${{ steps.package-info.outputs.version }}
329378
package-name: ${{ steps.package-info.outputs.name }}
379+
build-artifact-id: ${{ steps.build.outputs.artifact-id }}
330380
docs-artifact-id: ${{ steps.upload-docs.outputs.artifact-id }}
331381
steps:
332382
- uses: hoverkraft-tech/ci-github-common/actions/checkout@5ac504609f6ef35c5ac94bd8199063aa32104721 # 0.31.3
@@ -383,13 +433,15 @@ jobs:
383433
core.setOutput('version', version);
384434
385435
- name: Build
436+
id: build
386437
if: needs.prepare.outputs.build-commands != ''
387438
uses: ./self-workflow/actions/build
388439
with:
389440
working-directory: ${{ inputs.working-directory }}
390441
build-commands: ${{ needs.prepare.outputs.build-commands }}
391442
build-env: ${{ needs.prepare.outputs.build-env || '{}' }}
392443
build-secrets: ${{ secrets.build-secrets }}
444+
build-artifact: ${{ needs.prepare.outputs.build-artifact }}
393445

394446
- name: Generate documentation
395447
if: needs.prepare.outputs.docs-command != ''

0 commit comments

Comments
 (0)