Skip to content

Commit

Permalink
Expose commit hash and version in the about service (#27)
Browse files Browse the repository at this point in the history
* Expose commit hash and version in the about service

* Fix paths
  • Loading branch information
anxolin authored May 24, 2024
1 parent cd46bea commit 3db5c49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Version: Used in the /about endpoint. Useful to expose the Docker image version.
VESION=1.0.0

# Port
PORT=8080

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: yarn install

- name: Write commit hash
run: echo "${{ github.sha }}" > dist/git-commit-hash.txt

- run: yarn build

- name: Set up Docker Buildx
Expand Down
28 changes: 26 additions & 2 deletions apps/api/src/app/routes/about/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import { log } from 'console';
import { FastifyPluginAsync } from 'fastify';
import { version } from '../../../../../../package.json';

import { readFileSync } from 'fs';
const GIT_COMMIT_HASH_FILE = 'git-commit-hash.txt';
const VERSION = process.env.VERSION || 'UNKNOWN, please set the environment variable';
const COMMIT_HASH = getCommitHash();
import { join } from 'path';



interface AboutResponse {
name: string;
version: string;
gitCommitHash?: string | undefined;
}

const example: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.get<{ Reply: AboutResponse }>('/', async function (_request, reply) {
return reply.send({
name: 'BFF API',
version,
version: VERSION,
gitCommitHash: COMMIT_HASH,
});
});
};

/**
* Read a file with the git commit hash (generated for example using github actions)
*/
function getCommitHash(): string | undefined {
const filePath = join(__dirname, '../../../../../../../../', GIT_COMMIT_HASH_FILE)
try {
return readFileSync(filePath, 'utf-8')
} catch (error) {
// Not a big deal, if the file is not present, the about won't
console.warn(`Unable to read the commit hash file ${filePath}, therefore won't be exported in the about endpoint`);
return undefined
}
}

export default example;

0 comments on commit 3db5c49

Please # to comment.