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

@ -67,46 +67,19 @@ jobs:
- name: Add manifest into release archive
run: zip release.zip --update system.json
- name: Upload archive to s3
run: node scripts/uploadToS3.mjs
env:
TAG: "v${{steps.version.outputs.version}}"
FILE: "release.zip"
- name: Upload manifest to s3
run: node scripts/uploadToS3.mjs
env:
TAG: "v${{steps.version.outputs.version}}"
FILE: "system.json"
- name: Create draft release
run: node scripts/createForgejoRelease.mjs
env:
TAG: "v${{steps.version.outputs.version}}"
github-release:
runs-on: act
name: "Create Github release"
needs:
- create-artifacts
if: vars.RELEASE_TO_GITHUB == 'yes'
steps:
- name: Download artifacts
uses: https://data.forgejo.org/forgejo/download-artifact@v4
with:
merge-multiple: true
- name: Install dependencies
run: npm i
- id: version
run: cat system.json | echo version=`jq -r ".version"` >> "$FORGEJO_OUTPUT"
- name: Update manifest
run: node scripts/prepareManifest.mjs
env:
DOWNLOAD_URL: "https://github.com/${{vars.GH_USER}}/${{vars.GH_REPO}}/releases/download/v${{steps.version.outputs.version}}/release.zip"
LATEST_URL: "https://github.com/${{vars.GH_USER}}/${{vars.GH_REPO}}/releases/latest/download/system.json"
- name: Add manifest into release archive
run: zip release.zip --update system.json
- name: Sync Github mirror
run: echo Syncing mirror
- name: Create draft release
run: node scripts/createGithubRelease.mjs
env:
TAG: "v${{steps.version.outputs.version}}"
TOKEN: ${{secrets.GH_TOKEN}}
API_URL: "https://api.github.com"
REPO: "${{vars.GH_USER}}/${{vars.GH_REPO}}"

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();