Files
joplin/packages/tools/buildServerDocker.ts
T

81 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-06-11 18:24:59 +02:00
import { execCommand2, rootDir } from './tool-utils';
2021-07-08 15:04:03 +02:00
import * as moment from 'moment';
2021-06-11 18:24:59 +02:00
2022-02-23 09:02:13 +00:00
interface Argv {
dryRun?: boolean;
pushImages?: boolean;
repository?: string;
tagName?: string;
}
export function getVersionFromTag(tagName: string, isPreRelease: boolean): string {
2021-06-11 18:24:59 +02:00
const s = tagName.split('-');
const suffix = isPreRelease ? '-beta' : '';
return s[1].substr(1) + suffix;
2021-06-11 18:24:59 +02:00
}
2021-09-29 17:55:54 +01:00
export function getIsPreRelease(_tagName: string): boolean {
// For now we only create pre-releases from CI. It's after, once the release
// has been proven stable, that it is tagged as "latest".
return true;
// return tagName.indexOf('-beta') > 0;
2021-06-11 18:24:59 +02:00
}
async function main() {
2022-02-23 09:02:13 +00:00
const argv = require('yargs').argv as Argv;
2021-06-11 18:24:59 +02:00
if (!argv.tagName) throw new Error('--tag-name not provided');
2022-02-23 09:02:13 +00:00
if (!argv.repository) throw new Error('--repository not provided');
2021-06-11 18:24:59 +02:00
2022-01-13 17:39:58 +00:00
const dryRun = !!argv.dryRun;
const pushImages = !!argv.pushImages;
2022-02-23 09:02:13 +00:00
const repository = argv.repository;
2021-06-11 18:24:59 +02:00
const tagName = argv.tagName;
const isPreRelease = getIsPreRelease(tagName);
const imageVersion = getVersionFromTag(tagName, isPreRelease);
2021-07-08 15:04:03 +02:00
const buildDate = moment(new Date().getTime()).format('YYYY-MM-DDTHH:mm:ssZ');
let revision = '';
try {
2021-12-21 12:38:05 +01:00
revision = await execCommand2('git rev-parse --short HEAD', { showStdout: false });
2021-07-08 15:04:03 +02:00
} catch (error) {
console.info('Could not get git commit: metadata revision field will be empty');
}
const buildArgs = `--build-arg BUILD_DATE="${buildDate}" --build-arg REVISION="${revision}" --build-arg VERSION="${imageVersion}"`;
const dockerTags: string[] = [];
const versionPart = imageVersion.split('.');
2021-09-29 17:55:54 +01:00
dockerTags.push(isPreRelease ? 'beta' : 'latest');
dockerTags.push(versionPart[0] + (isPreRelease ? '-beta' : ''));
dockerTags.push(`${versionPart[0]}.${versionPart[1]}${isPreRelease ? '-beta' : ''}`);
dockerTags.push(imageVersion);
2021-06-11 18:24:59 +02:00
process.chdir(rootDir);
console.info(`Running from: ${process.cwd()}`);
console.info('tagName:', tagName);
console.info('pushImages:', pushImages);
2021-06-11 18:24:59 +02:00
console.info('imageVersion:', imageVersion);
console.info('isPreRelease:', isPreRelease);
console.info('Docker tags:', dockerTags.join(', '));
2021-06-11 18:24:59 +02:00
2022-02-23 09:02:13 +00:00
const dockerCommand = `docker build --progress=plain -t "${repository}:${imageVersion}" ${buildArgs} -f Dockerfile.server .`;
2022-01-13 17:39:58 +00:00
if (dryRun) {
console.info(dockerCommand);
return;
}
await execCommand2(dockerCommand);
for (const tag of dockerTags) {
2022-02-23 09:02:13 +00:00
await execCommand2(`docker tag "${repository}:${imageVersion}" "${repository}:${tag}"`);
if (pushImages) await execCommand2(`docker push ${repository}:${tag}`);
}
2021-06-11 18:24:59 +02:00
}
if (require.main === module) {
2022-09-30 17:23:14 +01:00
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
main().catch((error) => {
console.error('Fatal error');
console.error(error);
process.exit(1);
});
}