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 11:02:13 +02:00
|
|
|
interface Argv {
|
|
|
|
dryRun?: boolean;
|
|
|
|
pushImages?: boolean;
|
|
|
|
repository?: string;
|
|
|
|
tagName?: string;
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:48:26 +02:00
|
|
|
export function getVersionFromTag(tagName: string, isPreRelease: boolean): string {
|
2021-06-11 18:24:59 +02:00
|
|
|
const s = tagName.split('-');
|
2021-06-19 16:14:19 +02:00
|
|
|
const suffix = isPreRelease ? '-beta' : '';
|
|
|
|
return s[1].substr(1) + suffix;
|
2021-06-11 18:24:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 18:55:54 +02: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 11:02:13 +02: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 11:02:13 +02:00
|
|
|
if (!argv.repository) throw new Error('--repository not provided');
|
2021-06-11 18:24:59 +02:00
|
|
|
|
2022-01-13 19:39:58 +02:00
|
|
|
const dryRun = !!argv.dryRun;
|
2021-08-14 17:58:08 +02:00
|
|
|
const pushImages = !!argv.pushImages;
|
2022-02-23 11:02:13 +02:00
|
|
|
const repository = argv.repository;
|
2021-06-11 18:24:59 +02:00
|
|
|
const tagName = argv.tagName;
|
|
|
|
const isPreRelease = getIsPreRelease(tagName);
|
2021-06-19 16:14:19 +02:00
|
|
|
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 13:38:05 +02: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');
|
|
|
|
}
|
2021-09-14 14:01:33 +02:00
|
|
|
const buildArgs = `--build-arg BUILD_DATE="${buildDate}" --build-arg REVISION="${revision}" --build-arg VERSION="${imageVersion}"`;
|
2021-07-25 11:38:36 +02:00
|
|
|
const dockerTags: string[] = [];
|
|
|
|
const versionPart = imageVersion.split('.');
|
2021-09-29 18:55:54 +02:00
|
|
|
dockerTags.push(isPreRelease ? 'beta' : 'latest');
|
2021-07-25 11:38:36 +02:00
|
|
|
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);
|
2021-08-14 17:58:08 +02:00
|
|
|
console.info('pushImages:', pushImages);
|
2021-06-11 18:24:59 +02:00
|
|
|
console.info('imageVersion:', imageVersion);
|
|
|
|
console.info('isPreRelease:', isPreRelease);
|
2021-07-25 11:38:36 +02:00
|
|
|
console.info('Docker tags:', dockerTags.join(', '));
|
2021-06-11 18:24:59 +02:00
|
|
|
|
2022-02-23 11:02:13 +02:00
|
|
|
const dockerCommand = `docker build --progress=plain -t "${repository}:${imageVersion}" ${buildArgs} -f Dockerfile.server .`;
|
2022-01-13 19:39:58 +02:00
|
|
|
if (dryRun) {
|
|
|
|
console.info(dockerCommand);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await execCommand2(dockerCommand);
|
2021-12-20 17:08:43 +02:00
|
|
|
|
2021-09-14 14:01:33 +02:00
|
|
|
for (const tag of dockerTags) {
|
2022-02-23 11:02:13 +02:00
|
|
|
await execCommand2(`docker tag "${repository}:${imageVersion}" "${repository}:${tag}"`);
|
|
|
|
if (pushImages) await execCommand2(`docker push ${repository}:${tag}`);
|
2021-06-19 16:14:19 +02:00
|
|
|
}
|
2021-06-11 18:24:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 17:48:26 +02:00
|
|
|
if (require.main === module) {
|
2022-09-30 18:23:14 +02:00
|
|
|
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
|
2021-09-23 17:48:26 +02:00
|
|
|
main().catch((error) => {
|
|
|
|
console.error('Fatal error');
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
}
|