Remove the Github release shenanigans in favour of uploading to s3

This commit is contained in:
Oliver 2025-11-18 23:03:01 -07:00
parent 6e5422e08b
commit a4ae2aefca
4 changed files with 84 additions and 130 deletions

View file

@ -1,4 +1,3 @@
import { createReadStream } from "fs";
import axios from "axios";
const {
@ -6,22 +5,19 @@ const {
FORGEJO_API_URL: API,
FORGEJO_REPOSITORY: REPO,
FORGEJO_TOKEN: TOKEN,
CDN_URL,
} = process.env;
async function uploadFile(releaseID, localPath, remoteName = undefined) {
remoteName ??= localPath.split(`/`).at(-1);
const stream = createReadStream(localPath);
async function addReleaseAsset(releaseID, name) {
return axios.post(
`${API}/repos/${REPO}/releases/${releaseID}/assets`,
{
attachment: stream,
},
{ external_url: `${CDN_URL}/${REPO}/${TAG}/${name}`, },
{
headers: {
Authorization: `token ${TOKEN}`,
"Content-Type": `multipart/form-data`,
},
params: { name: remoteName },
params: { name },
}
);
};
@ -43,8 +39,8 @@ async function main() {
);
try {
await uploadFile(release.data.id, `release.zip`);
await uploadFile(release.data.id, `system.json`);
await addReleaseAsset(release.data.id, `release.zip`);
await addReleaseAsset(release.data.id, `system.json`);
} catch (e) {
console.error(`Failed to upload files, deleting draft release`);
console.error(e);

View file

@ -1,81 +0,0 @@
/*
Create draft release
Assert HTTP 200
Upload release.zip
Upload system.json
*/
import { createReadStream } from "fs";
import axios from "axios";
const {
TAG,
REPO,
TOKEN,
API_URL: API,
} = process.env;
async function uploadFile(uploadsURL, localPath, remoteName = undefined) {
remoteName ??= localPath.split(`/`).at(-1);
const stream = createReadStream(localPath);
return axios.post(
uploadsURL,
stream,
{
headers: {
Authorization: `Bearer ${TOKEN}`,
"Content-Type": `multipart/form-data`,
"X-GitHub-Api-Version": `2022-11-28`,
},
params: { name: remoteName },
}
);
};
async function main() {
// Initial Release Data
const release = await axios.post(
`${API}/repos/${REPO}/releases`,
{
name: TAG,
tag_name: TAG,
draft: true,
generate_release_notes: false,
make_latest: "false",
},
{
headers: {
Authorization: `Bearer ${TOKEN}`,
"X-GitHub-Api-Version": `2022-11-28`,
},
}
);
try {
await uploadFile(release.data.upload_url, `release.zip`);
await uploadFile(release.data.upload_url, `system.json`);
} catch (e) {
console.error(`Failed to upload files, deleting draft release`);
console.error(e);
try {
await axios.delete(
`${API}/repos/${REPO}/releases/${release.data.id}`,
{
headers: {
Authorization: `Bearer ${TOKEN}`,
"X-GitHub-Api-Version": `2022-11-28`,
},
}
)
} catch {
console.error(`Failed to delete draft release`);
};
process.exit(1);
};
console.log(`Release created, and files uploaded successfully!`);
};
main();

66
scripts/uploadToS3.mjs Normal file
View file

@ -0,0 +1,66 @@
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { createReadStream } from "fs";
const requiredEnvVariables = [
`TAG`, `FILE`,
`FORGEJO_REPOSITORY`,
`S3_BUCKET`, `S3_REGION`, `S3_KEY`, `S3_SECRET`, `S3_ENDPOINT`,
];
async function main() {
// Assert all of the required env variables are present
const missing = [];
for (const envVar of requiredEnvVariables) {
if (!(envVar in process.env)) {
missing.push(envVar);
};
};
if (missing.length > 0) {
console.error(`Missing the following required environment variables: ${missing.join(`, `)}`);
process.exit(1);
};
const {
TAG,
S3_ENDPOINT,
S3_REGION,
S3_KEY,
S3_SECRET,
S3_BUCKET,
FILE,
FORGEJO_REPOSITORY: REPO,
} = process.env;
const s3Client = new S3Client({
endpoint: S3_ENDPOINT,
forcePathStyle: false,
region: S3_REGION,
credentials: {
accessKeyId: S3_KEY,
secretAccessKey: S3_SECRET
},
});
const name = FILE.split(`/`).at(-1);
const params = {
Bucket: S3_BUCKET,
Key: `${REPO}/${TAG}/${name}`,
Body: createReadStream(FILE),
ACL: "public-read",
METADATA: {
"x-repo-version": TAG,
},
};
try {
const response = await s3Client.send(new PutObjectCommand(params));
console.log("Upload successful");
console.log(response);
} catch (err) {
console.error("Error", err);
};
};
main();