Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #504 from web3well/bw-91-publish-agg-image
Browse files Browse the repository at this point in the history
Publish aggregator images using CI
  • Loading branch information
voltrevo authored Feb 9, 2023
2 parents 2a59616 + 4eba0ba commit 99ed78b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 15 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/aggregator-dockerhub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: aggregator-dockerhub

on:
push:
branches:
- 'main'
paths:
- 'aggregator/**'
- '.github/workflows/aggregator-dockerhub.yml'

defaults:
run:
working-directory: ./aggregator

env:
DENO_VERSION: 1.x

jobs:
push:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: ${{ env.DENO_VERSION }}
- run: git show HEAD
- run: echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login --username blswalletghactions --password-stdin
- run: ./programs/build.ts --image-name blswallet/aggregator --image-only --also-tag-latest --push
2 changes: 1 addition & 1 deletion aggregator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM denoland/deno:1.23.4
FROM denoland/deno:1.30.1

ADD build /app
WORKDIR /app
Expand Down
79 changes: 65 additions & 14 deletions aggregator/programs/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ import { dirname, parseArgs } from "../deps.ts";
import * as shell from "./helpers/shell.ts";
import repoDir from "../src/helpers/repoDir.ts";

const args = parseArgs(Deno.args);
const parseArgsResult = parseArgs(Deno.args);

const args = {
/** Whether to push the image to dockerhub. */
push: parseArgsResult["push"],

/** Override the image name. Default: aggregator. */
imageName: parseArgsResult["image-name"],

/** Only build the image, ie - don't also serialize the image to disk. */
imageOnly: parseArgsResult["image-only"],

/** Prefix all docker commands with sudo. */
sudoDocker: parseArgsResult["sudo-docker"],

/** Tag the image with latest as well as the default git-${sha}. */
alsoTagLatest: parseArgsResult["also-tag-latest"],
};

Deno.chdir(repoDir);
const buildDir = `${repoDir}/build`;
Expand All @@ -15,7 +32,11 @@ await copyTypescriptFiles();
await buildDockerImage();
await tarballTypescriptFiles();

console.log("Aggregator build complete");
if (args.push) {
await pushDockerImage();
}

console.log("\nAggregator build complete");

async function allFiles() {
return [
Expand All @@ -29,7 +50,7 @@ async function allFiles() {
];
}

async function BuildName() {
async function Tag() {
const commitShort = (await shell.Line("git", "rev-parse", "HEAD")).slice(
0,
7,
Expand Down Expand Up @@ -85,28 +106,47 @@ async function tarballTypescriptFiles() {
}

async function buildDockerImage() {
const buildName = await BuildName();
const tag = await Tag();
const imageName = args.imageName ?? "aggregator";
const imageNameAndTag = `${imageName}:${tag}`;

const sudoDockerArg = args["sudo-docker"] === true ? ["sudo"] : [];
const sudoDockerArg = args.sudoDocker ? ["sudo"] : [];

await shell.run(
...sudoDockerArg,
"docker",
"build",
repoDir,
"-t",
`aggregator:${buildName}`,
imageNameAndTag,
);

const dockerImageName = `aggregator-${buildName}-docker-image`;
if (args.alsoTagLatest) {
await shell.run(
...sudoDockerArg,
"docker",
"tag",
`${imageName}:${tag}`,
`${imageName}:latest`,
);
}

console.log("\nDocker image created:", imageNameAndTag);

if (args.imageName) {
return;
}

const dockerImageFileName = `${imageName}-${tag}-docker-image`;
const tarFilePath = `${repoDir}/build/${dockerImageFileName}.tar`;

await shell.run(
...sudoDockerArg,
"docker",
"save",
"--output",
`${repoDir}/build/${dockerImageName}.tar`,
`aggregator:${buildName}`,
tarFilePath,
imageNameAndTag,
);

if (sudoDockerArg.length > 0) {
Expand All @@ -117,12 +157,23 @@ async function buildDockerImage() {
"sudo",
"chown",
username,
`${repoDir}/build/${dockerImageName}.tar`,
tarFilePath,
);
}

await shell.run(
"gzip",
`${repoDir}/build/${dockerImageName}.tar`,
);
await shell.run("gzip", tarFilePath);

console.log(`Docker image saved: ${tarFilePath}.gz`);
}

async function pushDockerImage() {
const tag = await Tag();
const imageName = args.imageName ?? "aggregator";
const imageNameAndTag = `${imageName}:${tag}`;

await shell.run("docker", "push", imageNameAndTag);

if (args.alsoTagLatest) {
await shell.run("docker", "push", `${imageName}:latest`);
}
}

0 comments on commit 99ed78b

Please sign in to comment.