2024-12-10 01:56:49 +03:00
|
|
|
const fs = require("node:fs");
|
|
|
|
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
|
|
|
|
const path = require("node:path");
|
|
|
|
const packageJson = require("../package.json");
|
|
|
|
|
2024-12-10 02:12:10 +03:00
|
|
|
if (!process.env.BUILD_WEBHOOK_URL) {
|
|
|
|
console.log("No BUILD_WEBHOOK_URL provided, skipping upload");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2024-12-10 01:56:49 +03:00
|
|
|
const s3 = new S3Client({
|
|
|
|
region: "auto",
|
|
|
|
endpoint: process.env.S3_ENDPOINT,
|
|
|
|
forcePathStyle: true,
|
|
|
|
credentials: {
|
2024-12-10 02:04:37 +03:00
|
|
|
accessKeyId: process.env.S3_ACCESS_KEY_ID,
|
2024-12-10 01:56:49 +03:00
|
|
|
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-10 03:20:35 +03:00
|
|
|
const dist = path.resolve(__dirname, "..", "dist");
|
2024-12-10 01:56:49 +03:00
|
|
|
|
2024-12-20 20:33:13 +03:00
|
|
|
const extensionsToUpload = [".deb", ".exe", ".pacman"];
|
2024-12-10 01:56:49 +03:00
|
|
|
|
|
|
|
fs.readdir(dist, async (err, files) => {
|
|
|
|
if (err) throw err;
|
|
|
|
|
2024-12-10 02:12:10 +03:00
|
|
|
const uploads = await Promise.all(
|
2024-12-10 03:03:14 +03:00
|
|
|
files
|
|
|
|
.filter((file) => extensionsToUpload.includes(path.extname(file)))
|
|
|
|
.map(async (file) => {
|
2024-12-10 03:54:35 +03:00
|
|
|
console.log(`⌛️ Uploading ${file}...`);
|
2024-12-10 01:56:49 +03:00
|
|
|
const fileName = `${new Date().getTime()}-${file}`;
|
|
|
|
|
|
|
|
const command = new PutObjectCommand({
|
|
|
|
Bucket: process.env.S3_BUILDS_BUCKET_NAME,
|
|
|
|
Key: fileName,
|
|
|
|
Body: fs.createReadStream(path.resolve(dist, file)),
|
2024-12-24 07:33:24 +03:00
|
|
|
// 3 days
|
|
|
|
Expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 3),
|
2024-12-10 01:56:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await s3.send(command);
|
|
|
|
|
|
|
|
return {
|
2024-12-10 03:54:35 +03:00
|
|
|
url: `${process.env.BUILDS_URL}/${fileName}`,
|
2024-12-10 01:56:49 +03:00
|
|
|
name: fileName,
|
|
|
|
};
|
2024-12-10 03:03:14 +03:00
|
|
|
})
|
2024-12-10 01:56:49 +03:00
|
|
|
);
|
|
|
|
|
2024-12-10 03:03:14 +03:00
|
|
|
if (uploads.length > 0) {
|
|
|
|
await fetch(process.env.BUILD_WEBHOOK_URL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
uploads,
|
|
|
|
branchName: process.env.BRANCH_NAME,
|
|
|
|
version: packageJson.version,
|
|
|
|
githubActor: process.env.GITHUB_ACTOR,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2024-12-10 01:56:49 +03:00
|
|
|
});
|