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
|
import { stat } from "fs/promises";
import { basename, join } from "path";
import { tmpdir } from "os";
import type { ActionHandler, ActionContext } from "../action-registry";
function parseInputs(
ctx: ActionContext
): { name: string; path: string } | { error: string } {
const w = ctx.with || {};
const name = typeof w.name === "string" ? w.name.trim() : "";
const path = typeof w.path === "string" ? w.path.trim() : "";
if (!name) return { error: "upload-artifact: `name` is required" };
if (!path) return { error: "upload-artifact: `path` is required" };
return { name, path };
}
function guessContentType(filename: string): string {
const lower = filename.toLowerCase();
if (lower.endsWith(".tar.gz") || lower.endsWith(".tgz")) return "application/gzip";
if (lower.endsWith(".gz")) return "application/gzip";
if (lower.endsWith(".zip")) return "application/zip";
if (lower.endsWith(".tar")) return "application/x-tar";
if (lower.endsWith(".json")) return "application/json";
if (lower.endsWith(".txt") || lower.endsWith(".log")) return "text/plain";
if (lower.endsWith(".xml")) return "application/xml";
if (lower.endsWith(".html")) return "text/html";
return "application/octet-stream";
}
async function tarGzDirectory(dir: string): Promise<Buffer> {
const tmpPath = join(
tmpdir(),
`gluecron-artifact-${Date.now()}-${Math.random().toString(36).slice(2)}.tar.gz`
);
try {
const proc = Bun.spawn(
["tar", "-czf", tmpPath, "-C", dir, "."],
{ stdout: "pipe", stderr: "pipe" }
);
const exit = await proc.exited;
if (exit !== 0) {
const err = await new Response(proc.stderr).text().catch(() => "");
throw new Error(`tar failed (exit ${exit}): ${err.slice(0, 200)}`);
}
const bytes = await Bun.file(tmpPath).arrayBuffer();
return Buffer.from(bytes);
} finally {
try {
const fs = await import("fs/promises");
await fs.unlink(tmpPath).catch(() => {});
} catch {
}
}
}
export const uploadArtifactAction: ActionHandler = {
name: "gluecron/upload-artifact",
version: "v1",
async run(ctx) {
try {
const parsed = parseInputs(ctx);
if ("error" in parsed) {
return { exitCode: 1, stderr: parsed.error };
}
let uploadArtifact: typeof import("../workflow-artifacts").uploadArtifact;
try {
({ uploadArtifact } = await import("../workflow-artifacts"));
} catch (err) {
return {
exitCode: 0,
stderr:
"upload-artifact unavailable; skipping (" +
(err instanceof Error ? err.message : String(err)) +
")",
};
}
const abs = join(ctx.workspace, parsed.path);
let info;
try {
info = await stat(abs);
} catch (err) {
return {
exitCode: 1,
stderr:
`upload-artifact: path not found: ${parsed.path} (${err instanceof Error ? err.message : String(err)})`,
};
}
let content: Buffer;
let contentType: string;
if (info.isDirectory()) {
content = await tarGzDirectory(abs);
contentType = "application/gzip";
} else if (info.isFile()) {
const bytes = await Bun.file(abs).arrayBuffer();
content = Buffer.from(bytes);
contentType = guessContentType(basename(abs));
} else {
return {
exitCode: 1,
stderr: `upload-artifact: unsupported path type for ${parsed.path}`,
};
}
const result = await uploadArtifact({
runId: ctx.runId,
jobId: ctx.jobId,
name: parsed.name,
content,
contentType,
});
if (!result.ok) {
return {
exitCode: 1,
stderr: `upload-artifact: ${result.error}`,
};
}
return {
exitCode: 0,
stdout: `Uploaded artifact "${parsed.name}" (${content.byteLength} bytes, ${contentType})`,
outputs: {
"artifact-id": result.artifactId,
name: parsed.name,
size: String(content.byteLength),
},
};
} catch (err) {
return {
exitCode: 1,
stderr:
"upload-artifact error: " +
(err instanceof Error ? err.message : String(err)),
};
}
},
};
|