Skip to content

Commit

Permalink
Bake TMT version and build timestamp into docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
JensForstmann committed Oct 9, 2024
1 parent 218ac9d commit 145dca6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name == 'push' }}
build-args: |
COMMIT_SHA=${{ github.sha }}
TMT_COMMIT_SHA=${{ github.sha }}
tags: |
jensforstmann/tmt2:${{ github.sha }}
jensforstmann/tmt2:latest
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ jobs:
platforms: linux/amd64,linux/arm64
push: true
build-args: |
COMMIT_SHA=${{ github.sha }}
TMT_COMMIT_SHA=${{ github.sha }}
TMT_VERSION=${{ steps.tagName.outputs.major }}.${{ steps.tagName.outputs.minor }}.${{ steps.tagName.outputs.patch }}
tags: |
jensforstmann/tmt2:v${{ steps.tagName.outputs.major }}
jensforstmann/tmt2:${{ steps.tagName.outputs.major }}
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ COPY --from=backend_build_image /app/backend/node_modules /app/backend/node_modu
COPY --from=frontend_build_image /app/frontend/dist /app/frontend/dist
VOLUME /app/backend/storage
EXPOSE 8080
ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA}
ARG TMT_COMMIT_SHA
ENV TMT_COMMIT_SHA=${TMT_COMMIT_SHA}
ARG TMT_VERSION
ENV TMT_VERSION=${TMT_VERSION}
RUN date -u +"%Y-%m-%dT%H:%M:%SZ" > /app/.TMT_IMAGE_BUILD_TIMESTAMP
WORKDIR /app/backend
CMD ["/tini", "node", "./dist/backend/src/index.js"]
4 changes: 3 additions & 1 deletion backend/src/debugController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Route, Security } from '@tsoa/runtime';
import { PORT, TMT_LOG_ADDRESS, VERSION } from '.';
import { COMMIT_SHA, IMAGE_BUILD_TIMESTAMP, PORT, TMT_LOG_ADDRESS, VERSION } from '.';
import { IDebugResponse } from '../../common';
import { Settings } from './settings';
import { STORAGE_FOLDER } from './storage';
Expand All @@ -20,6 +20,8 @@ export class DebugController extends Controller {
async getInfos(): Promise<IDebugResponse> {
return {
tmtVersion: VERSION,
tmtCommitSha: COMMIT_SHA,
tmtImageBuildTimestamp: IMAGE_BUILD_TIMESTAMP,
tmtStorageFolder: STORAGE_FOLDER,
tmtPort: PORT,
tmtLogAddress: TMT_LOG_ADDRESS,
Expand Down
40 changes: 28 additions & 12 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ValidateError } from '@tsoa/runtime';
import express, { ErrorRequestHandler } from 'express';
import { existsSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import http from 'http';
import path from 'path';
import * as Auth from './auth';
import * as Election from './election';
import * as ManagedGameServers from './managedGameServers';
import * as Presets from './presets';
import * as Match from './match';
import { checkAndNormalizeLogAddress } from './match';
import * as MatchMap from './matchMap';
import * as MatchService from './matchService';
import * as Presets from './presets';
import { RegisterRoutes } from './routes';
import * as Storage from './storage';
import * as WebSocket from './webSocket';
Expand All @@ -28,18 +28,31 @@ export const TMT_LOG_ADDRESS: string | null = (() => {
return addr;
})();

const STATIC_PATH = (() => {
if (existsSync(path.join(__dirname, '../../frontend/dist'))) {
return path.join(__dirname, '../../frontend/dist');
const APP_DIR = (() => {
if (__dirname.endsWith(path.join('/backend/dist/backend/src'))) {
// in production: __dirname = /app/backend/dist/backend/src
return path.join(__dirname, '../../../..');
}
if (existsSync(path.join(__dirname, '../../../../frontend/dist'))) {
return path.join(__dirname, '../../../../frontend/dist');
if (__dirname.endsWith(path.join('/backend/src'))) {
// in development: __dirname = /app/backend/src
return path.join(__dirname, '../..');
}
throw 'Could not determine static path';
console.error(`__dirname is ${__dirname}`);
throw 'Could not determine APP_DIR';
})();

const FRONTEND_DIR = path.join(APP_DIR, '/frontend/dist');

export const PORT = process.env['TMT_PORT'] || 8080;
export const VERSION = process.env['COMMIT_SHA'] || null;
export const VERSION = process.env['TMT_VERSION'] || null;
export const COMMIT_SHA = process.env['TMT_COMMIT_SHA'] || null;
export const IMAGE_BUILD_TIMESTAMP = (() => {
const file = path.join(APP_DIR, '.TMT_IMAGE_BUILD_TIMESTAMP');
if (existsSync(file)) {
return readFileSync(file).toString().trim();
}
return null;
})();

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

app.get('*', express.static(STATIC_PATH));
app.get('*', (req, res) => res.sendFile(path.join(STATIC_PATH, 'index.html')));
app.get('*', express.static(FRONTEND_DIR));
app.get('*', (req, res) => res.sendFile(path.join(FRONTEND_DIR, 'index.html')));

const main = async () => {
console.info(`Start TMT (version ${VERSION ? VERSION : 'unknown'})`);
console.info(
`Start TMT (version ${VERSION ?? 'unknown'}, commit ${COMMIT_SHA ?? 'unknown'}, build timestamp ${IMAGE_BUILD_TIMESTAMP ?? 'unknown'})`
);
console.info(`App dir: ${APP_DIR}, frontend dir: ${FRONTEND_DIR}`);
await Storage.setup();
await Auth.setup();
await WebSocket.setup(httpServer);
Expand Down
7 changes: 5 additions & 2 deletions backend/src/match.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ValidateError } from '@tsoa/runtime';
import { generate as shortUuid } from 'short-uuid';
import { TMT_LOG_ADDRESS, VERSION } from '.';
import { COMMIT_SHA, IMAGE_BUILD_TIMESTAMP, TMT_LOG_ADDRESS, VERSION } from '.';
import {
IMatch,
IMatchCreateDto,
Expand Down Expand Up @@ -716,7 +716,10 @@ export const registerCommandHandlers = () => {
};

const onVersionCommand: commands.CommandHandler = async (e) => {
await say(e.match, `TMT version: ${VERSION ?? 'unknown'}`);
await say(
e.match,
`TMT: version ${VERSION ?? 'unknown'}, commit ${COMMIT_SHA ?? 'unknown'}, build timestamp ${IMAGE_BUILD_TIMESTAMP ?? 'unknown'}`
);
};

const onEveryCommand: commands.CommandHandler = async (e) => {
Expand Down
10 changes: 10 additions & 0 deletions backend/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,16 @@ const models: TsoaRoute.Models = {
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
required: true,
},
tmtCommitSha: {
dataType: 'union',
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
required: true,
},
tmtImageBuildTimestamp: {
dataType: 'union',
subSchemas: [{ dataType: 'string' }, { dataType: 'enum', enums: [null] }],
required: true,
},
tmtStorageFolder: { dataType: 'string', required: true },
tmtPort: {
dataType: 'union',
Expand Down
10 changes: 10 additions & 0 deletions backend/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,14 @@
"type": "string",
"nullable": true
},
"tmtCommitSha": {
"type": "string",
"nullable": true
},
"tmtImageBuildTimestamp": {
"type": "string",
"nullable": true
},
"tmtStorageFolder": {
"type": "string"
},
Expand Down Expand Up @@ -2033,6 +2041,8 @@
},
"required": [
"tmtVersion",
"tmtCommitSha",
"tmtImageBuildTimestamp",
"tmtStorageFolder",
"tmtPort",
"tmtLogAddress",
Expand Down
2 changes: 2 additions & 0 deletions common/types/debug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface IDebugResponse {
tmtVersion: string | null;
tmtCommitSha: string | null;
tmtImageBuildTimestamp: string | null;
tmtStorageFolder: string;
tmtPort: string | number;
tmtLogAddress: string | null;
Expand Down

0 comments on commit 145dca6

Please sign in to comment.