backend: Fetch package version at build time

Extract `fetch-version.ts` which is called at build time to create
file `dist/api/version.json`.
This file is read by `backend-info.ts` at runtime.

This also fixes handing over the Git commit hash to the backend app
in the Docker backend image, which was broken as of 2022-07-12.
(Reason: The commit hash was previously required at runtime, but was
only provided at build time.)
This commit is contained in:
Erik Arvstedt 2022-07-13 12:44:32 +02:00
parent 5683f639ed
commit d591f7c456
No known key found for this signature in database
GPG Key ID: 33312B944DD97846
3 changed files with 64 additions and 49 deletions

View File

@ -22,7 +22,8 @@
"main": "index.ts", "main": "index.ts",
"scripts": { "scripts": {
"tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json", "tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json",
"build": "npm run tsc && cp ./src/tasks/price-feeds/mtgox-weekly.json ./dist/tasks/", "build": "npm run tsc && npm run create-resources",
"create-resources": "cp ./src/tasks/price-feeds/mtgox-weekly.json ./dist/tasks && node dist/api/fetch-version.js",
"start": "node --max-old-space-size=2048 dist/index.js", "start": "node --max-old-space-size=2048 dist/index.js",
"start-production": "node --max-old-space-size=4096 dist/index.js", "start-production": "node --max-old-space-size=4096 dist/index.js",
"test": "./node_modules/.bin/jest --coverage", "test": "./node_modules/.bin/jest --coverage",

View File

@ -1,60 +1,37 @@
import * as fs from 'fs'; import fs from 'fs';
import * as os from 'os'; import path from 'path';
import logger from '../logger'; import os from 'os';
import { IBackendInfo } from '../mempool.interfaces'; import { IBackendInfo } from '../mempool.interfaces';
const { spawnSync } = require('child_process');
class BackendInfo { class BackendInfo {
private gitCommitHash = ''; private backendInfo: IBackendInfo;
private hostname = '';
private version = '';
constructor() { constructor() {
this.setLatestCommitHash(); // This file is created by ./fetch-version.ts during building
this.setVersion(); const versionFile = path.join(__dirname, 'version.json')
this.hostname = os.hostname(); var versionInfo;
} if (fs.existsSync(versionFile)) {
versionInfo = JSON.parse(fs.readFileSync(versionFile).toString());
public getBackendInfo(): IBackendInfo { } else {
return { // Use dummy values if `versionFile` doesn't exist (e.g., during testing)
hostname: this.hostname, versionInfo = {
gitCommit: this.gitCommitHash, version: '?',
version: this.version, gitCommit: '?'
};
}
this.backendInfo = {
hostname: os.hostname(),
version: versionInfo.version,
gitCommit: versionInfo.gitCommit
}; };
} }
public getBackendInfo(): IBackendInfo {
return this.backendInfo;
}
public getShortCommitHash() { public getShortCommitHash() {
return this.gitCommitHash.slice(0, 7); return this.backendInfo.gitCommit.slice(0, 7);
}
private setLatestCommitHash(): void {
//TODO: share this logic with `generate-config.js`
if (process.env.DOCKER_COMMIT_HASH) {
this.gitCommitHash = process.env.DOCKER_COMMIT_HASH;
} else {
try {
const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']);
if (!gitRevParse.error) {
const output = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
this.gitCommitHash = output ? output : '?';
} else if (gitRevParse.error.code === 'ENOENT') {
console.log('git not found, cannot parse git hash');
this.gitCommitHash = '?';
}
} catch (e: any) {
console.log('Could not load git commit info: ' + e.message);
this.gitCommitHash = '?';
}
}
}
private setVersion(): void {
try {
const packageJson = fs.readFileSync('package.json').toString();
this.version = JSON.parse(packageJson).version;
} catch (e) {
throw new Error(e instanceof Error ? e.message : 'Error');
}
} }
} }

View File

@ -0,0 +1,37 @@
import fs from 'fs';
import path from "path";
const { spawnSync } = require('child_process');
function getVersion(): string {
const packageJson = fs.readFileSync('package.json').toString();
return JSON.parse(packageJson).version;
}
function getGitCommit(): string {
if (process.env.DOCKER_COMMIT_HASH) {
return process.env.DOCKER_COMMIT_HASH;
} else {
const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']);
if (!gitRevParse.error) {
const output = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
if (output) {
return output;
} else {
console.log('Could not fetch git commit: No repo available');
}
} else if (gitRevParse.error.code === 'ENOENT') {
console.log('Could not fetch git commit: Command `git` is unavailable');
}
}
return '?';
}
const versionInfo = {
version: getVersion(),
gitCommit: getGitCommit()
}
fs.writeFileSync(
path.join(__dirname, 'version.json'),
JSON.stringify(versionInfo, null, 2) + "\n"
);