85 lines
2.7 KiB
JavaScript
85 lines
2.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const axios = require('axios').default;
|
|
const jszip = require('jszip');
|
|
|
|
// Replace these with your details
|
|
const GITHUB_SERVER_URL = process.env.GITHUB_SERVER_URL;
|
|
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;
|
|
const GITHUB_REF_NAME = process.env.GITHUB_REF_NAME;
|
|
|
|
// Set the path to your bin directory
|
|
const binPath = process.env.bin_path || 'bin';
|
|
const zipName = process.env.zip_name || 'release.zip';
|
|
const relName = process.env.rel_name || 'Content Release';
|
|
const relDesc = process.env.rel_desc || 'Automatic Content Release from GITEA workflow';
|
|
const tagName = process.env.tag_name || 'automatic-release';
|
|
|
|
const accessToken = process.env.access_token; // Make sure to set this to your Gitea access token
|
|
|
|
// Zip the files in the bin directory
|
|
const zip = new jszip();
|
|
const zipFolder = (srcFolder, zipFolder) => {
|
|
const items = fs.readdirSync(srcFolder);
|
|
items.forEach(item => {
|
|
const fullPath = path.join(srcFolder, item);
|
|
const stat = fs.statSync(fullPath);
|
|
if (stat.isFile()) {
|
|
const data = fs.readFileSync(fullPath);
|
|
zipFolder.file(item, data);
|
|
} else if (stat.isDirectory()) {
|
|
const childZipFolder = zipFolder.folder(item);
|
|
zipFolder(fullPath, childZipFolder);
|
|
}
|
|
});
|
|
};
|
|
|
|
zipFolder(binPath, zip);
|
|
|
|
zip.generateNodeStream({ streamFiles: true })
|
|
.pipe(fs.createWriteStream(zipName))
|
|
.on('finish', function () {
|
|
console.log("Zip file written.");
|
|
|
|
// Create a release
|
|
const releaseUrl = `${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}/releases`;
|
|
const headers = {
|
|
'Authorization': `token ${accessToken}`
|
|
};
|
|
const data = {
|
|
"body": relDesc,
|
|
"name": relName,
|
|
"draft": true,
|
|
"prerelease": true,
|
|
"tag_name": tagName,
|
|
"target_commitish": GITHUB_REF_NAME
|
|
};
|
|
|
|
axios.post(releaseUrl, data, { headers })
|
|
.then(response => {
|
|
const release = response.data;
|
|
const releaseId = release.id;
|
|
|
|
// Create a release attachment
|
|
const attachmentUrl = `${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}/releases/${releaseId}/assets`;
|
|
const formData = new FormData();
|
|
formData.append('attachment', fs.createReadStream(zipName), zipName);
|
|
|
|
axios.post(attachmentUrl, formData, {
|
|
headers: {
|
|
...headers,
|
|
...formData.getHeaders()
|
|
}
|
|
})
|
|
.then(() => {
|
|
console.log("Attachment uploaded successfully.");
|
|
})
|
|
.catch(error => {
|
|
console.error("Failed to upload attachment:", error.response.data);
|
|
});
|
|
})
|
|
.catch(error => {
|
|
console.error("Failed to create release:", error.response.data);
|
|
});
|
|
});
|