forked from firecow/gitlab-ci-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
335 lines (330 loc) · 13 KB
/
Copy pathindex.ts
File metadata and controls
335 lines (330 loc) · 13 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env node
import chalk from "chalk";
import yargs from "yargs";
import {Parser} from "./parser.js";
import * as state from "./state.js";
import {WriteStreamsProcess, WriteStreamsMock} from "./write-streams.js";
import {handler} from "./handler.js";
import {Executor} from "./executor.js";
import {Argv} from "./argv.js";
import {AssertionError} from "assert";
import {Job, cleanupJobResources} from "./job.js";
import {GitlabRunnerPresetValues} from "./gitlab-preset.js";
const jobs: Job[] = [];
process.on("SIGINT", async (_: string, code: number) => {
await cleanupJobResources(jobs);
process.exit(code);
});
// Graceful shutdown for nodemon
process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
(() => {
const yparser = yargs(process.argv.slice(2));
yparser.parserConfiguration({"greedy-arrays": false})
.showHelpOnFail(false)
.version("4.61.1")
.wrap(yparser.terminalWidth?.())
.command({
handler: async (argv) => {
try {
await handler(argv, new WriteStreamsProcess(), jobs);
const failedJobs = Executor.getFailed(jobs);
process.exit(failedJobs.length > 0 ? 1 : 0);
} catch (e: any) {
if (e instanceof AssertionError) {
process.stderr.write(chalk`{red ${e.message.trim()}}\n`);
} else if (e instanceof AggregateError) {
e.errors.forEach((aggE) => process.stderr.write(chalk`{red ${aggE.stack ?? aggE}}\n`));
} else {
process.stderr.write(chalk`{red ${e.stack ?? e}}\n`);
}
await cleanupJobResources(jobs);
process.exit(1);
}
},
builder: (y: any) => {
return y
.positional("job", {
describe: "Jobname's to execute",
type: "string", // Type here is referring to each element of the positional args
})
// by default yargs's positional options (args) can be used as options (flags) so this coerce is solely for
// handling scenario when a single --job option flag is passed
// Once https://github.com/yargs/yargs/issues/2196 is implemented, we can probably remove this
.coerce("job", (args: string[]) => {
if (!Array.isArray(args)) return [args];
return args;
});
},
command: "$0 [job..]",
describe: "Runs the entire pipeline or job's",
})
.usage("Find more information at https://github.com/firecow/gitlab-ci-local.\nNote: To negate an option use '--no-(option)'.")
.strictOptions()
.env("GCL")
.option("manual", {
type: "array",
description: "One or more manual jobs to run during a pipeline",
requiresArg: true,
})
.option("list", {
type: "boolean",
description: "List job information, when:never excluded",
requiresArg: false,
})
.option("list-all", {
type: "boolean",
description: "List job information, when:never included",
requiresArg: false,
})
.option("list-json", {
type: "boolean",
description: "List job information in json format, when:never included",
requiresArg: false,
})
.option("list-csv", {
type: "boolean",
description: "List job information in csv format, when:never excluded",
requiresArg: false,
})
.option("list-csv-all", {
type: "boolean",
description: "List job information in csv format, when:never included",
requiresArg: false,
})
.option("preview", {
type: "boolean",
description: "Print YML with defaults, includes, extends and reference's expanded",
requiresArg: false,
})
.option("cwd", {
type: "string",
description: "Path to a current working directory",
requiresArg: true,
})
.option("variables-file", {
type: "string",
description: "Path to the project file variables",
requiresArg: true,
default: Argv.default.variablesFile,
})
.option("completion", {
type: "boolean",
description: "Generate tab completion script",
requiresArg: false,
})
.option("evaluate-rule-changes", {
type: "boolean",
description: "Whether to evaluate rule:changes. If set to false, rules:changes will always evaluate to true",
requiresArg: false,
default: Argv.default.evaluateRuleChanges,
})
.option("needs", {
type: "boolean",
description: "Run needed jobs, when executing specific jobs",
requiresArg: false,
})
.option("only-needs", {
type: "boolean",
description: "Run needed jobs, except the specified jobs themselves",
requiresArg: false,
})
.option("stage", {
type: "string",
description: "Run all jobs in a specific stage",
requiresArg: false,
})
.option("variable", {
type: "array",
description: "Add variable to all executed jobs (--variable HELLO=world)",
requiresArg: false,
})
.option("unset-variable", {
type: "array",
description: "Unsets a variable (--unset-variable HELLO)",
requiresArg: false,
})
.option("remote-variables", {
type: "string",
description: "Fetch variables file from remote location",
requiresArg: false,
})
.option("state-dir", {
type: "string",
description: "Location of the .gitlab-ci-local state dir, relative to cwd, eg. (symfony/.gitlab-ci-local/)",
requiresArg: false,
})
.option("file", {
type: "string",
description: "Location of the .gitlab-ci.yml, relative to cwd, eg. (gitlab/.gitlab-ci.yml)",
requiresArg: false,
})
.option("home", {
type: "string",
description: "Location of the HOME .gitlab-ci-local folder ($HOME/.gitlab-ci-local/variables.yml)",
requiresArg: false,
})
.option("shell-isolation", {
type: "boolean",
description: "Enable artifact isolation for shell-executor jobs",
requiresArg: false,
})
.option("force-shell-executor", {
type: "boolean",
description: "Forces all jobs to be executed using the shell executor. (Only use this option for trusted job)",
requiresArg: false,
})
.option("shell-executor-no-image", {
type: "boolean",
description: "Whether to use shell executor when no image is specified.",
requiresArg: false,
})
.option("default-image", {
type: "string",
description: "When using --shell-executor-no-image=false which image to be used for the container. Defaults to docker.io/ruby:3.1 if not set.",
requiresArg: false,
})
.option("wait-image", {
type: "string",
description: "Which image to be used for the wait container. Defaults to docker.io/sumina46/wait-for-it:latest if not set.",
requiresArg: false,
})
.option("helper-image", {
type: "string",
description: "When using --shell-executor-no-image=false which image to be used for the utils container. Defaults to docker.io/firecow/gitlab-ci-local-util:latest if not set.",
requiresArg: false,
})
.option("mount-cache", {
type: "boolean",
description: "Enable docker mount based caching",
requiresArg: false,
})
.option("umask", {
type: "boolean",
description: "Sets docker user to 0:0",
requiresArg: false,
})
.option("userns", {
type: "string",
description: "Set docker executor userns option",
requiresArg: false,
})
.option("privileged", {
type: "boolean",
description: "Set docker executor to privileged mode",
requiresArg: false,
})
.option("ulimit", {
type: "number",
description: "Set docker executor ulimit",
requiresArg: false,
})
.option("network", {
type: "array",
description: "Add networks to docker executor",
requiresArg: false,
})
.option("volume", {
type: "array",
description: "Add volumes to docker executor",
requiresArg: false,
})
.option("extra-host", {
type: "array",
description: "Add extra docker host entries",
requiresArg: false,
})
.option("pull-policy", {
type: "string",
description: "Set image pull-policy (always or if-not-present)",
requiresArg: false,
})
.option("fetch-includes", {
type: "boolean",
description: "Fetch all external includes one more time",
requiresArg: false,
})
.option("maximum-includes", {
type: "number",
description: "The maximum number of includes",
requiresArg: false,
})
.option("artifacts-to-source", {
type: "boolean",
description: "Copy the generated artifacts into cwd",
requiresArg: false,
})
.option("cleanup", {
type: "boolean",
description: "Remove docker resources after they've been used",
requiresArg: false,
})
.option("quiet", {
type: "boolean",
description: "Suppress all job output",
requiresArg: false,
})
.option("timestamps", {
type: "boolean",
description: "Show timestamps and job duration in the logs",
requiresArg: false,
})
.option("max-job-name-padding", {
type: "number",
description: "Maximum padding for job name (use <= 0 for no padding)",
requiresArg: false,
})
.option("json-schema-validation", {
type: "boolean",
description: "Whether to enable json schema validation",
requiresArg: false,
})
.option("ignore-schema-paths", {
type: "array",
requiresArg: false,
default: Argv.default.ignoreSchemaPaths,
description: "The json schema paths that will be ignored",
})
.option("concurrency", {
type: "number",
description: "Limit the number of jobs that run simultaneously",
requiresArg: false,
})
.option("container-executable", {
type: "string",
description: "Command to start the container engine (docker or podman)",
requiresArg: false,
})
.option("container-mac-address", {
type: "string",
description: "Container MAC address (e.g., aa:bb:cc:dd:ee:ff)",
requiresArg: false,
})
.option("container-emulate", {
type: "string",
description: "The name, without the architecture, of a gitlab hosted runner to emulate. See here: https://docs.gitlab.com/ee/ci/runners/hosted_runners/linux.html#machine-types-available-for-linux---x86-64",
choices: GitlabRunnerPresetValues,
})
.option("color", {
requiresArg: false,
default: true,
description: "Enables color",
})
.completion("completion", false, (current: string, yargsArgv: any, completionFilter: any, done: (completions: string[]) => any) => {
try {
if (current.startsWith("-")) {
completionFilter();
} else {
Argv.build({...yargsArgv, autoCompleting: true})
.then(argv => state.getPipelineIid(argv.cwd, argv.stateDir).then(pipelineIid => ({argv, pipelineIid})))
.then(({argv, pipelineIid}) => Parser.create(argv, new WriteStreamsMock(), pipelineIid, []))
.then((parser) => {
const jobNames = [...parser.jobs.values()].filter((j) => j.when != "never").map((j) => j.name);
done(jobNames);
});
}
} catch {
return ["Parser-Failed!"];
}
})
.parse();
})();