Skip to content

Commit

Permalink
Share an imageId and extract the imageId from the docker output (#14804)
Browse files Browse the repository at this point in the history
* Added methods to share an image id and to extract the imageId from the docker output

* bumped version to 2.0.4
  • Loading branch information
jcfiorenzano authored May 10, 2021
1 parent 829ae72 commit 7ffd9f4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
53 changes: 53 additions & 0 deletions common-npm-packages/docker-common-v2/containerimageutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
2 changes: 1 addition & 1 deletion common-npm-packages/docker-common-v2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common-npm-packages/docker-common-v2/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 7ffd9f4

Please sign in to comment.