From ca91afe45b00d655369706d8304742db71bbb279 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Fri, 25 Jun 2021 23:46:56 -0700 Subject: [PATCH 1/6] update GHA workflow to expose the short sha to docker --- .github/workflows/on-tag.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/on-tag.yml b/.github/workflows/on-tag.yml index c45e453c7..f0041ecd0 100644 --- a/.github/workflows/on-tag.yml +++ b/.github/workflows/on-tag.yml @@ -34,6 +34,9 @@ jobs: - name: Checkout project uses: actions/checkout@v2 + - name: Add SHORT_SHA env property with commit short sha + run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - name: Init repo for Dockerization run: docker/init.sh "$TAG" @@ -65,6 +68,7 @@ jobs: --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:$TAG \ --output "type=registry" ./${{ matrix.service }}/ + --build-arg commitHash=$SHORT_SHA - name: Run Docker buildx for ${{ matrix.service }} against latest run: | @@ -74,4 +78,5 @@ jobs: --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:latest \ --output "type=registry" ./${{ matrix.service }}/ + --build-arg commitHash=$SHORT_SHA From 446bdfebeaae404aab6e7c242c6dc519ff85c998 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Fri, 25 Jun 2021 23:47:19 -0700 Subject: [PATCH 2/6] update FE Dockerfile to read short sha --- docker/frontend/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker/frontend/Dockerfile b/docker/frontend/Dockerfile index a9ed8d674..2293f59a9 100644 --- a/docker/frontend/Dockerfile +++ b/docker/frontend/Dockerfile @@ -1,5 +1,8 @@ FROM node:12-buster-slim AS builder +ARG commitHash +ENV DOCKER_COMMIT_HASH=${commitHash} + WORKDIR /build COPY . . RUN apt-get update From 33d37a9b5b9b291b2988b2e4cf7fb6eae04ae451 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Fri, 25 Jun 2021 23:47:40 -0700 Subject: [PATCH 3/6] update generate-config to read the short sha from docker --- frontend/generate-config.js | 74 +++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/frontend/generate-config.js b/frontend/generate-config.js index 3746c8ab3..023198f27 100644 --- a/frontend/generate-config.js +++ b/frontend/generate-config.js @@ -1,5 +1,5 @@ var fs = require('fs'); -const { execSync } = require('child_process'); +const { spawnSync } = require('child_process'); const CONFIG_FILE_NAME = 'mempool-frontend-config.json'; const GENERATED_CONFIG_FILE_NAME = 'generated-config.js'; @@ -12,15 +12,19 @@ let packetJsonVersion = ''; try { const rawConfig = fs.readFileSync(CONFIG_FILE_NAME); configContent = JSON.parse(rawConfig); + console.log(`${CONFIG_FILE_NAME} file found, using provided config`); } catch (e) { if (e.code !== 'ENOENT') { throw new Error(e); + } else { + console.log(`${CONFIG_FILE_NAME} file not found, using default config`); } } try { const packageJson = fs.readFileSync('package.json'); packetJsonVersion = JSON.parse(packageJson).version; + console.log(`mempool version ${packetJsonVersion}`); } catch (e) { throw new Error(e); } @@ -32,11 +36,23 @@ for (setting in configContent) { }); } -try { - const command = 'git rev-parse --short HEAD'; - gitCommitHash = execSync(command).toString('utf8').replace(/[\n\r\s]+$/, ''); -} catch (e) { - console.log('Could not load git commit info: ' + e.message || e); +if (process.env.DOCKER_COMMIT_HASH) { + gitCommitHash = 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]+$/, ''); + console.log(`mempool revision ${gitCommitHash}`); + } else if (gitRevParse.error.code === 'ENOENT') { + console.log('git not found, cannot parse git hash'); + gitCommitHash = '?'; + } + } catch (e) { + console.log('Could not load git commit info: ' + e.message); + gitCommitHash = '?'; + } } const newConfig = `(function (window) { @@ -46,18 +62,38 @@ const newConfig = `(function (window) { window.__env.PACKAGE_JSON_VERSION = '${packetJsonVersion}'; }(global || this));`; -try { - const currentConfig = fs.readFileSync(GENERATED_CONFIG_FILE_NAME).toString().trim(); - if (currentConfig === newConfig) { - console.log("Configuration not changed, skipping generation"); - } else { - try { - fs.writeFileSync(GENERATED_CONFIG_FILE_NAME, newConfig, 'utf8'); - console.log('Config file generated'); - } catch (e) { - throw new Error(e); - } +function readConfig(path) { + try { + const currentConfig = fs.readFileSync(path).toString().trim(); + return currentConfig; + } catch (e) { + return false; } -} catch (e) { - throw new Error(e); } + +function writeConfig(path, config) { + try { + fs.writeFileSync(path, config, 'utf8'); + } catch (e) { + throw new Error(e); + } +} + +const currentConfig = readConfig(GENERATED_CONFIG_FILE_NAME); + +if (currentConfig && currentConfig === newConfig) { + console.log(`No configuration updates, skipping ${GENERATED_CONFIG_FILE_NAME} file update`); + return; +} else if (!currentConfig) { + console.log(`${GENERATED_CONFIG_FILE_NAME} file not found, creating new config file`); + console.log('CONFIG: ', newConfig); + writeConfig(GENERATED_CONFIG_FILE_NAME, newConfig); + console.log(`${GENERATED_CONFIG_FILE_NAME} file saved`); + return; +} else { + console.log(`Configuration changes detected, updating ${GENERATED_CONFIG_FILE_NAME} file`); + console.log('OLD CONFIG: ', currentConfig); + console.log('NEW CONFIG: ', newConfig); + writeConfig(GENERATED_CONFIG_FILE_NAME, newConfig); + console.log(`${GENERATED_CONFIG_FILE_NAME} file updated`); +}; From 741a02057938e5e3972de5abdf2d24fb7965cc70 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Sat, 26 Jun 2021 00:29:45 -0700 Subject: [PATCH 4/6] add missing semicolon --- frontend/generate-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/generate-config.js b/frontend/generate-config.js index 023198f27..fa856dd7d 100644 --- a/frontend/generate-config.js +++ b/frontend/generate-config.js @@ -37,7 +37,7 @@ for (setting in configContent) { } if (process.env.DOCKER_COMMIT_HASH) { - gitCommitHash = process.env.DOCKER_COMMIT_HASH + gitCommitHash = process.env.DOCKER_COMMIT_HASH; } else { try { const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']); From f937ea57455667c73cc5a6f607eb329d36a16174 Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Sat, 26 Jun 2021 00:38:18 -0700 Subject: [PATCH 5/6] fix missing backslash --- .github/workflows/on-tag.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/on-tag.yml b/.github/workflows/on-tag.yml index f0041ecd0..54642f704 100644 --- a/.github/workflows/on-tag.yml +++ b/.github/workflows/on-tag.yml @@ -27,6 +27,9 @@ jobs: - name: Show set environment variables run: | printf " TAG: %s\n" "$TAG" + + - name: Add SHORT_SHA env property with commit short sha + run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV - name: Login to Docker for building run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin @@ -34,9 +37,6 @@ jobs: - name: Checkout project uses: actions/checkout@v2 - - name: Add SHORT_SHA env property with commit short sha - run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV - - name: Init repo for Dockerization run: docker/init.sh "$TAG" @@ -67,7 +67,7 @@ jobs: --cache-to "type=local,dest=/tmp/.buildx-cache" \ --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:$TAG \ - --output "type=registry" ./${{ matrix.service }}/ + --output "type=registry" ./${{ matrix.service }}/ \ --build-arg commitHash=$SHORT_SHA - name: Run Docker buildx for ${{ matrix.service }} against latest @@ -77,6 +77,6 @@ jobs: --cache-to "type=local,dest=/tmp/.buildx-cache" \ --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:latest \ - --output "type=registry" ./${{ matrix.service }}/ + --output "type=registry" ./${{ matrix.service }}/ \ --build-arg commitHash=$SHORT_SHA From 8574ee6edd6c04b5f77d4e30679da323949fe33a Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Sat, 26 Jun 2021 10:29:08 -0700 Subject: [PATCH 6/6] push both tag and latest images at the same time --- .github/workflows/on-tag.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/on-tag.yml b/.github/workflows/on-tag.yml index 54642f704..a7a9929d9 100644 --- a/.github/workflows/on-tag.yml +++ b/.github/workflows/on-tag.yml @@ -67,16 +67,6 @@ jobs: --cache-to "type=local,dest=/tmp/.buildx-cache" \ --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:$TAG \ - --output "type=registry" ./${{ matrix.service }}/ \ - --build-arg commitHash=$SHORT_SHA - - - name: Run Docker buildx for ${{ matrix.service }} against latest - run: | - docker buildx build \ - --cache-from "type=local,src=/tmp/.buildx-cache" \ - --cache-to "type=local,dest=/tmp/.buildx-cache" \ - --platform linux/amd64,linux/arm64,linux/arm/v7 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:latest \ --output "type=registry" ./${{ matrix.service }}/ \ --build-arg commitHash=$SHORT_SHA -