From f88af9c3f94ed1de49b87e70b623963030717d3a Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn <100320+knorrium@users.noreply.github.com> Date: Wed, 23 Mar 2022 12:16:21 -0700 Subject: [PATCH 1/4] Add the DOCKER_COMMIT_HASH env var to the backend Dockerfile --- docker/backend/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker/backend/Dockerfile b/docker/backend/Dockerfile index ceddc9000..c013fc23a 100644 --- a/docker/backend/Dockerfile +++ b/docker/backend/Dockerfile @@ -1,5 +1,8 @@ FROM node:16.10.0-buster-slim AS builder +ARG commitHash +ENV DOCKER_COMMIT_HASH=${commitHash} + WORKDIR /build COPY . . From 4bb6f499504f160079ff64e7c15e3075262b1b7d Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn <100320+knorrium@users.noreply.github.com> Date: Wed, 23 Mar 2022 12:17:23 -0700 Subject: [PATCH 2/4] Copy the git commit hash logic to the backend --- backend/src/api/backend-info.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/backend/src/api/backend-info.ts b/backend/src/api/backend-info.ts index 5a556ef18..3dde6ea47 100644 --- a/backend/src/api/backend-info.ts +++ b/backend/src/api/backend-info.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as os from 'os'; import logger from '../logger'; import { IBackendInfo } from '../mempool.interfaces'; +const { spawnSync } = require('child_process'); class BackendInfo { private gitCommitHash = ''; @@ -27,10 +28,24 @@ class BackendInfo { } private setLatestCommitHash(): void { - try { - this.gitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim(); - } catch (e) { - logger.err('Could not load git commit info: ' + (e instanceof Error ? e.message : e)); + //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) { + this.gitCommitHash = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, ''); + console.log(`mempool revision ${this.gitCommitHash}`); + } 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 = '?'; + } } } From 230fbdbc8ea739f5ca26c430a47ca014b919917c Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn <100320+knorrium@users.noreply.github.com> Date: Wed, 23 Mar 2022 12:33:15 -0700 Subject: [PATCH 3/4] Fix empty revision case --- backend/src/api/backend-info.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/api/backend-info.ts b/backend/src/api/backend-info.ts index 3dde6ea47..d98675671 100644 --- a/backend/src/api/backend-info.ts +++ b/backend/src/api/backend-info.ts @@ -34,10 +34,9 @@ class BackendInfo { } else { try { const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']); - if (!gitRevParse.error) { - this.gitCommitHash = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, ''); - console.log(`mempool revision ${this.gitCommitHash}`); + 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 = '?'; From 9c3fc9f75a23ca99fdee2f632ec8718e702640b3 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn <100320+knorrium@users.noreply.github.com> Date: Wed, 23 Mar 2022 12:33:47 -0700 Subject: [PATCH 4/4] Update frontend git commit hash logic --- frontend/generate-config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/generate-config.js b/frontend/generate-config.js index 617ab3c0e..1f37953b7 100644 --- a/frontend/generate-config.js +++ b/frontend/generate-config.js @@ -51,9 +51,9 @@ if (process.env.DOCKER_COMMIT_HASH) { } else { try { const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']); - if (!gitRevParse.error) { - gitCommitHash = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, ''); + const output = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, ''); + gitCommitHash = output ? output : '?'; console.log(`mempool revision ${gitCommitHash}`); } else if (gitRevParse.error.code === 'ENOENT') { console.log('git not found, cannot parse git hash');