From 6fb2b0458377a90531e09c2e6ccc122bba364079 Mon Sep 17 00:00:00 2001 From: Nirav Bhimani Date: Tue, 10 May 2022 23:52:22 +1000 Subject: [PATCH 1/2] Add support for docker image tag (#829) --- awsx/ecr/image.ts | 16 +++++++++++----- awsx/utils.ts | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/awsx/ecr/image.ts b/awsx/ecr/image.ts index 2694957d7..2e76cbfec 100644 --- a/awsx/ecr/image.ts +++ b/awsx/ecr/image.ts @@ -22,12 +22,13 @@ export class Image extends schema.Image { constructor(name: string, args: schema.ImageArgs, opts: pulumi.ComponentResourceOptions = {}) { super(name, args, opts); const { repositoryUrl, ...dockerArgs } = args; - this.imageUri = pulumi.output(args).apply((args) => computeImageFromAsset(args, this)); + this.imageUri = pulumi.output(args).apply((args) => computeImageFromAsset(name, args, this)); } } /** @internal */ export function computeImageFromAsset( + name: string, args: pulumi.Unwrap, parent: pulumi.Resource, ) { @@ -37,7 +38,9 @@ export function computeImageFromAsset( pulumi.log.debug(`Building container image at '${JSON.stringify(dockerInputs)}'`, parent); - const imageName = getImageName(dockerInputs); + const { imageName: imageNameWithoutTag, tag: imageNameTag } = utils.getImageNameAndTag(name); + const hashImageName = getImageName(dockerInputs, imageNameTag); + const baseImageName = imageNameTag ? `${imageNameWithoutTag}:${imageNameTag}` : hashImageName; // If we haven't, build and push the local build context to the ECR repository. Then return // the unique image name we pushed to. The name will change if the image changes ensuring @@ -56,7 +59,7 @@ export function computeImageFromAsset( pulumi.log.info("dockerBuild: " + JSON.stringify(dockerBuild)); const uniqueImageName = docker.buildAndPushImage( - imageName, + baseImageName, dockerBuild, repositoryUrl, parent, @@ -89,13 +92,13 @@ export function computeImageFromAsset( ); uniqueImageName.apply((d: any) => - pulumi.log.debug(` build complete: ${imageName} (${d})`, parent), + pulumi.log.debug(` build complete: ${hashImageName} (${d})`, parent), ); return uniqueImageName; } -function getImageName(inputs: pulumi.Unwrap) { +function getImageName(inputs: pulumi.Unwrap, imageNameTag?: string) { const { path, dockerfile, args } = inputs ?? {}; // Produce a hash of the build context and use that for the image name. let buildSig: string; @@ -109,6 +112,9 @@ function getImageName(inputs: pulumi.Unwrap) { buildSig += `;arg[${arg}]=${args[arg]}`; } } + if (imageNameTag) { + buildSig += `;tag=${imageNameTag}`; + } buildSig += pulumi.getStack(); return `${utils.sha1hash(buildSig)}-container`; diff --git a/awsx/utils.ts b/awsx/utils.ts index 81a272a4f..821ef27dd 100644 --- a/awsx/utils.ts +++ b/awsx/utils.ts @@ -14,7 +14,6 @@ import * as aws from "@pulumi/aws"; import * as pulumi from "@pulumi/pulumi"; - import * as crypto from "crypto"; type Diff = ({ @@ -186,3 +185,27 @@ export function parseArn(arn: string): Arn { } return { ...simpleProps, resourceId: resourceIdOrType }; } + +// https://github.com/pulumi/pulumi-docker/blob/master/sdk/nodejs/utils.ts#L15-L30 +/** + * @internal + */ +export function getImageNameAndTag(baseImageName: string): { + imageName: string; + tag: string | undefined; +} { + // From https://docs.docker.com/engine/reference/commandline/tag + // + // "A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, + // underscores, periods and dashes. A tag name may not start with a period or a dash and may + // contain a maximum of 128 characters." + // + // So it is safe for us to just look for the colon, and consume whatever follows as the tag + // for the image. + + const lastColon = baseImageName.lastIndexOf(":"); + const imageName = lastColon < 0 ? baseImageName : baseImageName.substr(0, lastColon); + const tag = lastColon < 0 ? undefined : baseImageName.substr(lastColon + 1); + + return { imageName, tag }; +} From f31850e6c9a8752f899b0906424e2e292e7d6a15 Mon Sep 17 00:00:00 2001 From: Nirav Bhimani Date: Tue, 10 May 2022 23:59:19 +1000 Subject: [PATCH 2/2] updated changelog (#829) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c31e89e4..a78a7ffaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG ## HEAD (Unreleased) * Add multi-lang component support scaffolding. +* Add support for docker tag for ECR in `awsx` package ## 0.40.0 (2022-03-24) * Compatibility with pulumi-aws v5.0.0