Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for docker image tag (#829) #829

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions awsx/ecr/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<schema.ImageArgs>,
parent: pulumi.Resource,
) {
Expand All @@ -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
Expand All @@ -56,7 +59,7 @@ export function computeImageFromAsset(
pulumi.log.info("dockerBuild: " + JSON.stringify(dockerBuild));

const uniqueImageName = docker.buildAndPushImage(
imageName,
baseImageName,
dockerBuild,
repositoryUrl,
parent,
Expand Down Expand Up @@ -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<schema.DockerBuildInputs>) {
function getImageName(inputs: pulumi.Unwrap<schema.DockerBuildInputs>, imageNameTag?: string) {
const { path, dockerfile, args } = inputs ?? {};
// Produce a hash of the build context and use that for the image name.
let buildSig: string;
Expand All @@ -109,6 +112,9 @@ function getImageName(inputs: pulumi.Unwrap<schema.DockerBuildInputs>) {
buildSig += `;arg[${arg}]=${args[arg]}`;
}
}
if (imageNameTag) {
buildSig += `;tag=${imageNameTag}`;
}

buildSig += pulumi.getStack();
return `${utils.sha1hash(buildSig)}-container`;
Expand Down
25 changes: 24 additions & 1 deletion awsx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

import * as crypto from "crypto";

type Diff<T extends string | number | symbol, U extends string | number | symbol> = ({
Expand Down Expand Up @@ -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 };
}