Skip to content

Commit 145dca6

Browse files
committed
Bake TMT version and build timestamp into docker container
1 parent 218ac9d commit 145dca6

File tree

9 files changed

+66
-19
lines changed

9 files changed

+66
-19
lines changed

.github/workflows/docker.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
platforms: linux/amd64,linux/arm64
4444
push: ${{ github.event_name == 'push' }}
4545
build-args: |
46-
COMMIT_SHA=${{ github.sha }}
46+
TMT_COMMIT_SHA=${{ github.sha }}
4747
tags: |
4848
jensforstmann/tmt2:${{ github.sha }}
4949
jensforstmann/tmt2:latest

.github/workflows/release.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ jobs:
2929
platforms: linux/amd64,linux/arm64
3030
push: true
3131
build-args: |
32-
COMMIT_SHA=${{ github.sha }}
32+
TMT_COMMIT_SHA=${{ github.sha }}
33+
TMT_VERSION=${{ steps.tagName.outputs.major }}.${{ steps.tagName.outputs.minor }}.${{ steps.tagName.outputs.patch }}
3334
tags: |
3435
jensforstmann/tmt2:v${{ steps.tagName.outputs.major }}
3536
jensforstmann/tmt2:${{ steps.tagName.outputs.major }}

Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ COPY --from=backend_build_image /app/backend/node_modules /app/backend/node_modu
3434
COPY --from=frontend_build_image /app/frontend/dist /app/frontend/dist
3535
VOLUME /app/backend/storage
3636
EXPOSE 8080
37-
ARG COMMIT_SHA
38-
ENV COMMIT_SHA=${COMMIT_SHA}
37+
ARG TMT_COMMIT_SHA
38+
ENV TMT_COMMIT_SHA=${TMT_COMMIT_SHA}
39+
ARG TMT_VERSION
40+
ENV TMT_VERSION=${TMT_VERSION}
41+
RUN date -u +"%Y-%m-%dT%H:%M:%SZ" > /app/.TMT_IMAGE_BUILD_TIMESTAMP
3942
WORKDIR /app/backend
4043
CMD ["/tini", "node", "./dist/backend/src/index.js"]

backend/src/debugController.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller, Get, Route, Security } from '@tsoa/runtime';
2-
import { PORT, TMT_LOG_ADDRESS, VERSION } from '.';
2+
import { COMMIT_SHA, IMAGE_BUILD_TIMESTAMP, PORT, TMT_LOG_ADDRESS, VERSION } from '.';
33
import { IDebugResponse } from '../../common';
44
import { Settings } from './settings';
55
import { STORAGE_FOLDER } from './storage';
@@ -20,6 +20,8 @@ export class DebugController extends Controller {
2020
async getInfos(): Promise<IDebugResponse> {
2121
return {
2222
tmtVersion: VERSION,
23+
tmtCommitSha: COMMIT_SHA,
24+
tmtImageBuildTimestamp: IMAGE_BUILD_TIMESTAMP,
2325
tmtStorageFolder: STORAGE_FOLDER,
2426
tmtPort: PORT,
2527
tmtLogAddress: TMT_LOG_ADDRESS,

backend/src/index.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { ValidateError } from '@tsoa/runtime';
22
import express, { ErrorRequestHandler } from 'express';
3-
import { existsSync } from 'fs';
3+
import { existsSync, readFileSync } from 'fs';
44
import http from 'http';
55
import path from 'path';
66
import * as Auth from './auth';
77
import * as Election from './election';
88
import * as ManagedGameServers from './managedGameServers';
9-
import * as Presets from './presets';
109
import * as Match from './match';
1110
import { checkAndNormalizeLogAddress } from './match';
1211
import * as MatchMap from './matchMap';
1312
import * as MatchService from './matchService';
13+
import * as Presets from './presets';
1414
import { RegisterRoutes } from './routes';
1515
import * as Storage from './storage';
1616
import * as WebSocket from './webSocket';
@@ -28,18 +28,31 @@ export const TMT_LOG_ADDRESS: string | null = (() => {
2828
return addr;
2929
})();
3030

31-
const STATIC_PATH = (() => {
32-
if (existsSync(path.join(__dirname, '../../frontend/dist'))) {
33-
return path.join(__dirname, '../../frontend/dist');
31+
const APP_DIR = (() => {
32+
if (__dirname.endsWith(path.join('/backend/dist/backend/src'))) {
33+
// in production: __dirname = /app/backend/dist/backend/src
34+
return path.join(__dirname, '../../../..');
3435
}
35-
if (existsSync(path.join(__dirname, '../../../../frontend/dist'))) {
36-
return path.join(__dirname, '../../../../frontend/dist');
36+
if (__dirname.endsWith(path.join('/backend/src'))) {
37+
// in development: __dirname = /app/backend/src
38+
return path.join(__dirname, '../..');
3739
}
38-
throw 'Could not determine static path';
40+
console.error(`__dirname is ${__dirname}`);
41+
throw 'Could not determine APP_DIR';
3942
})();
4043

44+
const FRONTEND_DIR = path.join(APP_DIR, '/frontend/dist');
45+
4146
export const PORT = process.env['TMT_PORT'] || 8080;
42-
export const VERSION = process.env['COMMIT_SHA'] || null;
47+
export const VERSION = process.env['TMT_VERSION'] || null;
48+
export const COMMIT_SHA = process.env['TMT_COMMIT_SHA'] || null;
49+
export const IMAGE_BUILD_TIMESTAMP = (() => {
50+
const file = path.join(APP_DIR, '.TMT_IMAGE_BUILD_TIMESTAMP');
51+
if (existsSync(file)) {
52+
return readFileSync(file).toString().trim();
53+
}
54+
return null;
55+
})();
4356

4457
const app = express();
4558
const httpServer = http.createServer(app);
@@ -103,11 +116,14 @@ app.get('/api', (req, res) => {
103116
res.sendFile('swagger.json', { root: '.' });
104117
});
105118

106-
app.get('*', express.static(STATIC_PATH));
107-
app.get('*', (req, res) => res.sendFile(path.join(STATIC_PATH, 'index.html')));
119+
app.get('*', express.static(FRONTEND_DIR));
120+
app.get('*', (req, res) => res.sendFile(path.join(FRONTEND_DIR, 'index.html')));
108121

109122
const main = async () => {
110-
console.info(`Start TMT (version ${VERSION ? VERSION : 'unknown'})`);
123+
console.info(
124+
`Start TMT (version ${VERSION ?? 'unknown'}, commit ${COMMIT_SHA ?? 'unknown'}, build timestamp ${IMAGE_BUILD_TIMESTAMP ?? 'unknown'})`
125+
);
126+
console.info(`App dir: ${APP_DIR}, frontend dir: ${FRONTEND_DIR}`);
111127
await Storage.setup();
112128
await Auth.setup();
113129
await WebSocket.setup(httpServer);

backend/src/match.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ValidateError } from '@tsoa/runtime';
22
import { generate as shortUuid } from 'short-uuid';
3-
import { TMT_LOG_ADDRESS, VERSION } from '.';
3+
import { COMMIT_SHA, IMAGE_BUILD_TIMESTAMP, TMT_LOG_ADDRESS, VERSION } from '.';
44
import {
55
IMatch,
66
IMatchCreateDto,
@@ -716,7 +716,10 @@ export const registerCommandHandlers = () => {
716716
};
717717

718718
const onVersionCommand: commands.CommandHandler = async (e) => {
719-
await say(e.match, `TMT version: ${VERSION ?? 'unknown'}`);
719+
await say(
720+
e.match,
721+
`TMT: version ${VERSION ?? 'unknown'}, commit ${COMMIT_SHA ?? 'unknown'}, build timestamp ${IMAGE_BUILD_TIMESTAMP ?? 'unknown'}`
722+
);
720723
};
721724

722725
const onEveryCommand: commands.CommandHandler = async (e) => {

backend/src/routes.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,16 @@ const models: TsoaRoute.Models = {
11861186
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
11871187
required: true,
11881188
},
1189+
tmtCommitSha: {
1190+
dataType: 'union',
1191+
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
1192+
required: true,
1193+
},
1194+
tmtImageBuildTimestamp: {
1195+
dataType: 'union',
1196+
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
1197+
required: true,
1198+
},
11891199
tmtStorageFolder: { dataType: 'string', required: true },
11901200
tmtPort: {
11911201
dataType: 'union',

backend/swagger.json

+10
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,14 @@
20052005
"type": "string",
20062006
"nullable": true
20072007
},
2008+
"tmtCommitSha": {
2009+
"type": "string",
2010+
"nullable": true
2011+
},
2012+
"tmtImageBuildTimestamp": {
2013+
"type": "string",
2014+
"nullable": true
2015+
},
20082016
"tmtStorageFolder": {
20092017
"type": "string"
20102018
},
@@ -2033,6 +2041,8 @@
20332041
},
20342042
"required": [
20352043
"tmtVersion",
2044+
"tmtCommitSha",
2045+
"tmtImageBuildTimestamp",
20362046
"tmtStorageFolder",
20372047
"tmtPort",
20382048
"tmtLogAddress",

common/types/debug.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export interface IDebugResponse {
22
tmtVersion: string | null;
3+
tmtCommitSha: string | null;
4+
tmtImageBuildTimestamp: string | null;
35
tmtStorageFolder: string;
46
tmtPort: string | number;
57
tmtLogAddress: string | null;

0 commit comments

Comments
 (0)