From 7ffd9f4ac2951a37bde4aed3d653457235ec6ae8 Mon Sep 17 00:00:00 2001 From: Juan Carlos Fiorenzano Date: Sun, 9 May 2021 22:56:28 -0700 Subject: [PATCH] Share an imageId and extract the imageId from the docker output (#14804) * Added methods to share an image id and to extract the imageId from the docker output * bumped version to 2.0.4 --- .../docker-common-v2/containerimageutils.ts | 53 +++++++++++++++++++ .../docker-common-v2/package-lock.json | 2 +- .../docker-common-v2/package.json | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/common-npm-packages/docker-common-v2/containerimageutils.ts b/common-npm-packages/docker-common-v2/containerimageutils.ts index ed4ac2ad50e3..80628246d172 100644 --- a/common-npm-packages/docker-common-v2/containerimageutils.ts +++ b/common-npm-packages/docker-common-v2/containerimageutils.ts @@ -188,3 +188,56 @@ function inspectImage(connection: ContainerConnection, imageName): any { } } +export function shareBuiltImageId(builtImageId: string) { + const IMAGE_SEPARATOR_CHAR: string = ";"; + const ENV_VARIABLE_MAX_SIZE = 32766; + let builtImages: string = tl.getVariable("DOCKER_TASK_BUILT_IMAGES"); + + if (builtImages && builtImages != "") { + const newImageId = `${IMAGE_SEPARATOR_CHAR}${builtImages}`; + + if (newImageId.length + builtImages.length > ENV_VARIABLE_MAX_SIZE) { + tl.debug("Images id truncated maximum environment variable size reached."); + return; + } + + builtImages += newImageId; + } + else { + builtImages = builtImageId; + } + + tl.setVariable("DOCKER_TASK_BUILT_IMAGES", builtImages); +} + +export function getImageIdFromBuildOutput(output: string): string { + const standardParser = (text: string): string => { + let parsedOutput: string[] = text.match(new RegExp("Successfully built ([0-9a-f]{12})", 'g')); + + return !parsedOutput || parsedOutput.length == 0 + ? "" + : parsedOutput[parsedOutput.length - 1].substring(19); // This remove the Succesfully built section + }; + + const buildKitParser = (text: string): string => { + let parsedOutput: string[] = text.match(new RegExp("writing image sha256:([0-9a-f]{64})", 'gi')); + + return !parsedOutput || parsedOutput.length == 0 + ? "" + : parsedOutput[parsedOutput.length - 1].substring(21, 33); // This remove the section Writing Image Sha256 and takes 12 characters from the Id. + } + + try { + let buildOutputParserFuncs = [standardParser, buildKitParser]; + for (let parserFunc of buildOutputParserFuncs) { + const builtImageId = parserFunc(output); + if (builtImageId) { + return builtImageId; + } + } + } catch (error) { + tl.debug(`An error occurred getting the image id from the docker ouput: ${error.message}`) + } + + return ""; +} diff --git a/common-npm-packages/docker-common-v2/package-lock.json b/common-npm-packages/docker-common-v2/package-lock.json index 10cbfd9c5bc2..064cf20acbda 100644 --- a/common-npm-packages/docker-common-v2/package-lock.json +++ b/common-npm-packages/docker-common-v2/package-lock.json @@ -1,6 +1,6 @@ { "name": "azure-pipelines-tasks-docker-common-v2", - "version": "2.0.3", + "version": "2.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/common-npm-packages/docker-common-v2/package.json b/common-npm-packages/docker-common-v2/package.json index b7d05527ed0f..e1c31acfbccb 100644 --- a/common-npm-packages/docker-common-v2/package.json +++ b/common-npm-packages/docker-common-v2/package.json @@ -1,6 +1,6 @@ { "name": "azure-pipelines-tasks-docker-common-v2", - "version": "2.0.3", + "version": "2.0.4", "description": "Common Library for Azure Rest Calls", "repository": { "type": "git",