-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
175 lines (155 loc) · 5.61 KB
/
action.yml
File metadata and controls
175 lines (155 loc) · 5.61 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
name: "Build"
description: "Action to build Node.js projects with support for custom commands, environment variables, and artifact handling"
author: hoverkraft
branding:
icon: package
color: blue
inputs:
working-directory:
description: |
Working directory where the build commands are executed.
Can be absolute or relative to the repository root.
required: false
default: "."
build-commands:
description: |
List of build commands to execute, one per line.
These are npm/pnpm/Yarn script names (e.g., `build`, `compile`).
required: true
build-env:
description: |
JSON object of environment variables to set during the build.
Example:
```json
{
"NODE_ENV": "production",
"API_URL": "https://api.example.com"
}
```
required: false
default: "{}"
build-secrets:
description: |
Multi-line string of secrets in env format (`KEY=VALUE`).
Example:
```txt
SECRET_KEY=$\{{ secrets.SECRET_KEY }}
API_TOKEN=$\{{ secrets.API_TOKEN }}
```
required: false
default: ""
build-artifact:
description: |
JSON object specifying artifact upload configuration.
Example:
```json
{
"name": "artifact-name",
"paths": "path1\npath2"
}
```
required: false
default: ""
container:
description: "Whether running in container mode (skips checkout and node setup)"
required: false
default: "false"
outputs:
artifact-id:
description: "ID of the uploaded artifact (if artifact was specified)"
value: ${{ steps.build-artifact-id.outputs.artifact-id }}
runs:
using: "composite"
steps:
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: mkdir -p ./self-build-action/ && cp -r $GITHUB_ACTION_PATH/../* ./self-build-action/
- id: setup-node
if: inputs.container != 'true'
uses: ./self-build-action/setup-node
with:
working-directory: ${{ inputs.working-directory }}
dependencies-cache: |
nx
gatsby
storybook
astro
docusaurus
- id: get-package-manager
if: inputs.container == 'true'
uses: ./self-build-action/get-package-manager
with:
working-directory: ${{ inputs.working-directory }}
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
BUILD_ENV: ${{ inputs.build-env }}
BUILD_SECRETS: ${{ inputs.build-secrets }}
with:
script: |
const envInput = process.env.BUILD_ENV || '{}';
let buildEnv = {};
try {
buildEnv = JSON.parse(envInput);
} catch (e) {
core.setFailed(`Invalid build env JSON: ${e.message}`);
}
for (const [key, value] of Object.entries(buildEnv)) {
core.exportVariable(key, value);
}
const secretsInput = process.env.BUILD_SECRETS || '';
for (const line of secretsInput.split('\n').map(line => line.trim()).filter(Boolean)) {
const [key, ...rest] = line.split('=');
if (!key || !rest.length) {
return core.setFailed(`Invalid build secrets format: ${line}`);
}
const value = rest.join('=');
core.exportVariable(key.trim(), value.trim());
}
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
BUILD_COMMANDS: ${{ inputs.build-commands }}
RUN_SCRIPT_COMMAND: ${{ inputs.container == 'true' && steps.get-package-manager.outputs.run-script-command || steps.setup-node.outputs.run-script-command }}
WORKING_DIRECTORY: ${{ inputs.working-directory }}
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 buildCommands = process.env.BUILD_COMMANDS || '';
const runScriptCommand = process.env.RUN_SCRIPT_COMMAND;
const commands = buildCommands.split('\n')
.map(cmd => cmd.trim())
.filter(Boolean);
for (const command of commands) {
core.info(`Running build command: ${command}`);
try {
const exitCode = await exec.exec(runScriptCommand, [command], {
cwd: workingDirectory,
ignoreReturnCode: true
});
if(exitCode !== 0) {
core.setFailed(`Build command "${command}" failed with exit code ${exitCode}`);
throw new Error(`Build command "${command}" failed with exit code ${exitCode}`);
}
} catch (error) {
core.setFailed(`Build command "${command}" failed: ${error.message}`);
throw error;
}
}
- id: build-artifact-id
if: inputs.build-artifact != ''
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
name: ${{ fromJSON(inputs.build-artifact).name }}
path: ${{ fromJSON(inputs.build-artifact).paths }}
if-no-files-found: error
include-hidden-files: true
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
- shell: bash
if: always()
run: rm -fr ./self-build-action