-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-debs.mjs
117 lines (99 loc) · 3.69 KB
/
generate-debs.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { readFile, rm, mkdir, copyFile, chmod, writeFile } from 'node:fs/promises';
import { exec as execImpl } from 'node:child_process';
import { promisify } from 'node:util';
import { setgid, setuid } from 'node:process';
const exec = promisify(execImpl);
const main = async () => {
setuid('root');
setgid('root');
// Look up release data.
const releaseRequest = await fetch('https://api.github.com/repos/go-gitea/gitea/releases/latest');
const releaseData = await releaseRequest.json();
const newVersion = releaseData.tag_name.substring(1);
const currentVersionFile = await readFile('current.txt');
const currentVersion = currentVersionFile.toString();
if (currentVersion === newVersion) {
console.log('Version match. Exiting.');
return;
}
// Fetch systemd service as soon as possible.
const serviceRequest = fetch(
'https://raw.githubusercontent.com/go-gitea/gitea/master/contrib/systemd/gitea.service'
);
// Cleanup, just in case we're not using a pristine Docker instance.
const workingFolders = ['deb', 'tmp'];
const folderRemovals = workingFolders.map(
(dir) => rm(dir, { recursive: true, force: true })
);
await Promise.all(folderRemovals);
const newFolders = [
'./deb',
'./tmp/usr/local/bin',
'./tmp/etc/systemd/system',
'./tmp/var/lib/gitea/custom',
'./tmp/var/lib/gitea/data',
'./tmp/var/lib/gitea/indexers',
'./tmp/var/lib/gitea/public',
'./tmp/var/lib/gitea/log'
];
const folderMkdirs = newFolders.map(
(folder) => mkdir(folder, { recursive: true, mode: 0o750 })
);
folderMkdirs.push(
mkdir('./tmp/etc/gitea', { recursive: true, mode: 0o770 }),
mkdir('./tmp/DEBIAN', { recursive: true, mode: 0o755 })
);
await Promise.all(folderMkdirs);
// Copy files.
const DEBIANfiles = ['postinst', 'postrm', 'preinst', 'prerm'];
const fileWrites = DEBIANfiles.map(
(file) => copyFile(`DEBIAN/${file}`, `tmp/DEBIAN/${file}`)
);
const serviceResponse = await serviceRequest;
const serviceBuffer = Buffer.from(await serviceResponse.arrayBuffer());
fileWrites.push(
writeFile('./tmp/etc/systemd/system/gitea.service', serviceBuffer, { mode: 0o755 })
);
await Promise.all(fileWrites);
// Fix file permissions for dpkg.
const fileChmods = DEBIANfiles.map(
(file) => chmod(`./tmp/DEBIAN/${file}`, 0o755)
);
await Promise.all(fileChmods);
const DEBIANcontrolFile = await readFile('DEBIAN/control');
const DEBIANcontrolData = DEBIANcontrolFile.toString();
const architectures = ['386', 'amd64', 'arm-6', 'arm64'];
for (let architecture of architectures) {
const binaryUrl = `https://github.com/go-gitea/gitea/releases/download/v${newVersion}/gitea-${newVersion}-linux-${architecture}`;
console.log(binaryUrl);
const binaryRequest = fetch(binaryUrl);
const binaryResponse = await binaryRequest;
const binaryBuffer = Buffer.from(await binaryResponse.arrayBuffer());
const binaryWriter = writeFile(
'./tmp/usr/local/bin/gitea',
binaryBuffer,
{ mode: 0o755, flag: 'w' }
);
architecture = architecture === 'arm-6' ? 'armhf' : architecture;
architecture = architecture === '386' ? 'i386' : architecture;
let editedControlData = DEBIANcontrolData.replaceAll(
'VERSION-TO-REPLACE',
newVersion
);
editedControlData = editedControlData.replaceAll(
'ARCHI-TO-REPLACE',
architecture
);
await writeFile(
'tmp/DEBIAN/control',
editedControlData,
{ mode: 0o755, flag: 'w' }
);
await binaryWriter;
await exec('dpkg-deb --build tmp deb');
await rm('./tmp/usr/local/bin/gitea');
}
// Update the version number.
await writeFile('current.txt', newVersion);
};
main().catch(console.error);