Merge pull request #595 from knorrium/fix_docker_builds

Improvements to local and Docker builds
This commit is contained in:
wiz 2021-06-27 04:20:09 +09:00 committed by GitHub
commit fdf15c39a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 29 deletions

View File

@ -27,6 +27,9 @@ jobs:
- name: Show set environment variables - name: Show set environment variables
run: | run: |
printf " TAG: %s\n" "$TAG" 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 - name: Login to Docker for building
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
@ -64,14 +67,6 @@ jobs:
--cache-to "type=local,dest=/tmp/.buildx-cache" \ --cache-to "type=local,dest=/tmp/.buildx-cache" \
--platform linux/amd64,linux/arm64,linux/arm/v7 \ --platform linux/amd64,linux/arm64,linux/arm/v7 \
--tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:$TAG \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:$TAG \
--output "type=registry" ./${{ matrix.service }}/
- 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 \ --tag ${{ secrets.DOCKER_HUB_USER }}/${{ matrix.service }}:latest \
--output "type=registry" ./${{ matrix.service }}/ --output "type=registry" ./${{ matrix.service }}/ \
--build-arg commitHash=$SHORT_SHA

View File

@ -1,5 +1,8 @@
FROM node:12-buster-slim AS builder FROM node:12-buster-slim AS builder
ARG commitHash
ENV DOCKER_COMMIT_HASH=${commitHash}
WORKDIR /build WORKDIR /build
COPY . . COPY . .
RUN apt-get update RUN apt-get update

View File

@ -1,5 +1,5 @@
var fs = require('fs'); var fs = require('fs');
const { execSync } = require('child_process'); const { spawnSync } = require('child_process');
const CONFIG_FILE_NAME = 'mempool-frontend-config.json'; const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
const GENERATED_CONFIG_FILE_NAME = 'generated-config.js'; const GENERATED_CONFIG_FILE_NAME = 'generated-config.js';
@ -12,15 +12,19 @@ let packetJsonVersion = '';
try { try {
const rawConfig = fs.readFileSync(CONFIG_FILE_NAME); const rawConfig = fs.readFileSync(CONFIG_FILE_NAME);
configContent = JSON.parse(rawConfig); configContent = JSON.parse(rawConfig);
console.log(`${CONFIG_FILE_NAME} file found, using provided config`);
} catch (e) { } catch (e) {
if (e.code !== 'ENOENT') { if (e.code !== 'ENOENT') {
throw new Error(e); throw new Error(e);
} else {
console.log(`${CONFIG_FILE_NAME} file not found, using default config`);
} }
} }
try { try {
const packageJson = fs.readFileSync('package.json'); const packageJson = fs.readFileSync('package.json');
packetJsonVersion = JSON.parse(packageJson).version; packetJsonVersion = JSON.parse(packageJson).version;
console.log(`mempool version ${packetJsonVersion}`);
} catch (e) { } catch (e) {
throw new Error(e); throw new Error(e);
} }
@ -32,11 +36,23 @@ for (setting in configContent) {
}); });
} }
try { if (process.env.DOCKER_COMMIT_HASH) {
const command = 'git rev-parse --short HEAD'; gitCommitHash = process.env.DOCKER_COMMIT_HASH;
gitCommitHash = execSync(command).toString('utf8').replace(/[\n\r\s]+$/, ''); } else {
} catch (e) { try {
console.log('Could not load git commit info: ' + e.message || e); 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) { const newConfig = `(function (window) {
@ -46,18 +62,38 @@ const newConfig = `(function (window) {
window.__env.PACKAGE_JSON_VERSION = '${packetJsonVersion}'; window.__env.PACKAGE_JSON_VERSION = '${packetJsonVersion}';
}(global || this));`; }(global || this));`;
try { function readConfig(path) {
const currentConfig = fs.readFileSync(GENERATED_CONFIG_FILE_NAME).toString().trim(); try {
if (currentConfig === newConfig) { const currentConfig = fs.readFileSync(path).toString().trim();
console.log("Configuration not changed, skipping generation"); return currentConfig;
} else { } catch (e) {
try { return false;
fs.writeFileSync(GENERATED_CONFIG_FILE_NAME, newConfig, 'utf8');
console.log('Config file generated');
} catch (e) {
throw new Error(e);
}
} }
} 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`);
};