Skip to content

Commit

Permalink
feat(cli): add buildhash and version to info output
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceNino committed Jun 12, 2022
1 parent 2f89f59 commit c683ee4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ steps:
- name: dockersock
path: /var/run/docker.sock
commands:
- apk add git
- echo $DOCKER_ACCESS_KEY | docker login -u mauricenino --password-stdin
- docker buildx create --name builder --driver docker-container --use
- docker buildx inspect --bootstrap --builder builder
Expand All @@ -79,6 +80,8 @@ steps:
--label org.opencontainers.image.url=https://github.com/MauriceNino/dashdot
--label org.opencontainers.image.source=https://github.com/MauriceNino/dashdot
--label org.opencontainers.image.licenses=MIT
--build-arg VERSION=0.0.0-dev
--build-arg BUILDHASH=$(git log -1 --format="%H")
-o type=registry
-t mauricenino/dashdot:dev .
- name: update & deploy server files (dev)
Expand Down Expand Up @@ -130,6 +133,8 @@ steps:
--label org.opencontainers.image.url=https://github.com/MauriceNino/dashdot
--label org.opencontainers.image.source=https://github.com/MauriceNino/dashdot
--label org.opencontainers.image.licenses=MIT
--build-arg VERSION=$(cat package.version)
--build-arg BUILDHASH=$(git log -1 --format="%H")
-o type=registry
-t mauricenino/dashdot:$(cat package.version)
-t mauricenino/dashdot:latest .
Expand Down Expand Up @@ -181,4 +186,4 @@ name: GITHUB_TOKEN
data: JMpZE0aC5Fau1tDEh+6Huy+yZJF1BqhVAzIm7+Xvfx9vYxHRrPAbh6hZrJzEvEjK9ClGNoPCHJaKmezV3Yrt2qYsARU=
---
kind: signature
hmac: 3ee60b6a34bee3cad12b877635a11c93b788e36dbf51b2099d82c6624ecc8ae6
hmac: 01253b4a116ce79ff319d78836bb3963dcc2c6e33bcc1f33777e16204671f786
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ EXPOSE 3000
# BUILD #
FROM base as build

ARG BUILDHASH
ARG VERSION

RUN \
/bin/echo -e ">> installing dependencies (build)" &&\
apk --no-cache add \
git \
make \
clang \
build-base &&\
git config --global --add safe.directory /app
git config --global --add safe.directory /app &&\
/bin/echo -e "{\"version\":\"$VERSION\",\"buildhash\":\"$BUILDHASH\"}" > /app/version.json

COPY . ./

Expand All @@ -71,6 +75,7 @@ RUN \
FROM base as prod

COPY --from=build /app/package.json .
COPY --from=build /app/version.json .
COPY --from=build /app/.yarn/releases/ .yarn/releases/
COPY --from=build /app/dist/ dist/

Expand Down
26 changes: 17 additions & 9 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const execp = promisify(exec);
const execpnoerr = async (cmd: string): Promise<string> => {
return execp(cmd)
.then(({ stdout }) => stdout)
.catch(() => '');
};

const inspectObj = (obj: any): string => {
return inspect(obj, {
Expand All @@ -25,24 +30,27 @@ yargs(hideBin(process.argv))
async () => {
const isDocker = existsSync('/.dockerenv');
const isPodman = existsSync('/run/.containerenv');
const { stdout: yarnVersion } = await execp('yarn --version');
const { stdout: nodeVersion } = await execp('node --version');
const { stdout: dashVersion } = await execp(
'cat package.json | grep version'
);
const yarnVersion = await execpnoerr('yarn --version');
const nodeVersion = await execpnoerr('node --version');
const buildInfoJson = await execpnoerr('cat version.json');
const gitHash = await execpnoerr('git log -1 --format="%H"');

const buildInfo = JSON.parse(buildInfoJson || '{}');
const version = buildInfo.version ?? 'unknown';
const buildhash = buildInfo.buildhash ?? gitHash;

console.log(
`
INFO
=========
In Docker: ${isDocker}
In Podman: ${isPodman}
Yarn: ${yarnVersion.trim()}
Node: ${nodeVersion.trim()}
Dash: ${dashVersion.replace(/"/g, '').split(':')[1].trim()}
Dash: ${version}
Cwd: ${process.cwd()}
Hash: ${buildhash}
In Docker: ${isDocker}
In Podman: ${isPodman}
`.trim()
);
}
Expand Down

0 comments on commit c683ee4

Please sign in to comment.