ci(builds): added better logic to building

This commit is contained in:
2025-07-12 14:31:15 -05:00
parent eed01197c0
commit 320968f994
8 changed files with 265 additions and 45 deletions

View File

@@ -2,22 +2,42 @@ import fs from 'fs-extra';
import { spawnSync } from 'child_process';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' }); // or specify your env file path here
// Load environment variables for convenience (or use dotenv if you like)
dotenv.config({ path: './.env' });
// Load package.json version
const pkg = await fs.readJson('package.json');
const version = pkg.version;
if (!version) {
console.error('Version not found in package.json');
process.exit(1);
}
// Load build number from BUILD_NUMBER file
let buildNumber = '0';
try {
buildNumber = (await fs.readFile('BUILD_NUMBER', 'utf8')).trim();
if (!buildNumber) buildNumber = '0';
} catch {
console.warn('BUILD_NUMBER file not found, defaulting to 0');
}
// Compose full version string with build number
const fullVersion = `${version}.${buildNumber}`;
const {
GITEA_URL,
GITEA_USERNAME,
GITEA_REPO,
GITEA_TOKEN,
VERSION,
GITEA_TOKEN
} = process.env;
if (!GITEA_URL || !GITEA_USERNAME || !GITEA_REPO || !GITEA_TOKEN || !VERSION) {
console.error('Missing required environment variables!');
if (!GITEA_URL || !GITEA_USERNAME || !GITEA_REPO || !GITEA_TOKEN) {
console.error('Missing required environment variables (GITEA_URL, GITEA_USERNAME, GITEA_REPO, GITEA_TOKEN)');
process.exit(1);
}
// 1) Generate or update CHANGELOG.md for the version
// 1) Generate or update CHANGELOG.md using conventional-changelog CLI
console.log('Generating CHANGELOG.md...');
const result = spawnSync('npx', [
'conventional-changelog',
@@ -31,27 +51,26 @@ const result = spawnSync('npx', [
], { stdio: 'inherit', shell: true });
if (result.status !== 0) {
console.log(result)
console.error('Failed to generate changelog');
process.exit(1);
}
// 2) Read changelog content for the current version
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
const regex = new RegExp(`## \\[${VERSION}\\][\\s\\S]*?(?=## \\[|$)`, 'm');
const changelog = await fs.readFile('CHANGELOG.md', 'utf8');
const regex = new RegExp(`## \\[${version}\\][\\s\\S]*?(?=## \\[|$)`, 'm');
const releaseNotes = changelog.match(regex)?.[0] || changelog;
console.log(`Release notes for v${VERSION}:\n`, releaseNotes);
console.log(`Release notes for v${fullVersion}:\n`, releaseNotes);
// 3) Create Gitea release
const createRelease = async () => {
const apiUrl = `https://${GITEA_URL}/api/v1/repos/${GITEA_USERNAME}/${GITEA_REPO}/releases`;
const releaseData = {
tag_name: `v${VERSION}`,
name: `v${VERSION}`,
tag_name: `v${fullVersion}`,
name: `v${fullVersion}`,
body: releaseNotes,
draft: false,
prerelease: false,
prerelease: true,
};
const response = await fetch(apiUrl, {
@@ -76,17 +95,16 @@ const createRelease = async () => {
// 4) Upload release zip asset
const uploadAsset = async (release) => {
const apiUrl = `https://${GITEA_URL}/api/v1/repos/${GITEA_USERNAME}/${GITEA_REPO}/releases/assets?tag=${release.tag_name}`;
const filePath = `releases/release-${VERSION}.zip`;
const filePath = `releases/release-${fullVersion}.zip`;
if (!fs.existsSync(filePath)) {
if (!await fs.pathExists(filePath)) {
console.warn(`Zip file not found: ${filePath}. Skipping asset upload.`);
return;
}
// Use form-data and fetch for uploading binary
const FormData = (await import('form-data')).default;
const form = new FormData();
form.append('name', `release-${VERSION}.zip`);
form.append('name', `release-${fullVersion}.zip`);
form.append('attachment', fs.createReadStream(filePath));
const response = await fetch(apiUrl, {