diff --git a/.github/workflows/aggregator-dockerhub.yml b/.github/workflows/aggregator-dockerhub.yml new file mode 100644 index 00000000..cb61aa6f --- /dev/null +++ b/.github/workflows/aggregator-dockerhub.yml @@ -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 diff --git a/aggregator/Dockerfile b/aggregator/Dockerfile index 1816ea96..31f2ddf3 100644 --- a/aggregator/Dockerfile +++ b/aggregator/Dockerfile @@ -1,4 +1,4 @@ -FROM denoland/deno:1.23.4 +FROM denoland/deno:1.30.1 ADD build /app WORKDIR /app diff --git a/aggregator/programs/build.ts b/aggregator/programs/build.ts index d5c8a5e8..108dd55a 100755 --- a/aggregator/programs/build.ts +++ b/aggregator/programs/build.ts @@ -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`; @@ -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 [ @@ -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, @@ -85,9 +106,11 @@ 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, @@ -95,18 +118,35 @@ async function buildDockerImage() { "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) { @@ -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`); + } }