Skip to content

Commit

Permalink
Provide option to limit artifact size in PublishBuildArtifactsV1 (#18293
Browse files Browse the repository at this point in the history
)

* Provide option to limit artifact size in PublishBuildArtifactsV1

* bump task version
  • Loading branch information
Pranshu-Negi authored May 29, 2023
1 parent e7e0bba commit 95b8cce
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"loc.input.help.ArtifactName": "The name of the artifact to create in the publish location.",
"loc.input.label.ArtifactType": "Artifact publish location",
"loc.input.help.ArtifactType": "Choose whether to store the artifact in Azure Pipelines, or to copy it to a file share that must be accessible from the build agent.",
"loc.input.label.MaxArtifactSize": "Max Artifact Size",
"loc.input.help.MaxArtifactSize": "Maximum limit on the size of artifacts to be published in bytes. Put 0 if you don't want to set any limit.",
"loc.input.label.TargetPath": "File share path",
"loc.input.help.TargetPath": "The file share to which the artifact files will be copied. This can include variables. Example: \\\\\\\\my\\\\share\\\\$(Build.DefinitionName)\\\\$(Build.BuildNumber). Publishing artifacts from a Linux or macOS agent to a file share is not supported.",
"loc.input.label.Parallel": "Parallel copy",
Expand All @@ -22,5 +24,6 @@
"loc.messages.ErrorHostTypeNotSupported": "This task must run in a build to publish artifacts to Azure Pipelines.",
"loc.messages.PublishBuildArtifactsFailed": "Publishing build artifacts failed with an error: %s",
"loc.messages.UnexpectedParallelCount": "Unsupported parallel count '%s'. Enter a number between 1 and 128.",
"loc.messages.TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows"
"loc.messages.TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows",
"loc.messages.ArtifactSizeExceeded": "The size of artifacts being published is %s bytes which is larger than the maximum limit %s bytes set for the published size of artifacts."
}
35 changes: 35 additions & 0 deletions Tasks/PublishBuildArtifactsV1/publishbuildartifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var process = require('process');
import * as fs from 'fs';
import * as tl from 'azure-pipelines-task-lib/task';
import * as tr from 'azure-pipelines-task-lib/toolrunner';
import { error } from 'console';

// used for escaping the path to the Invoke-Robocopy.ps1 script that is passed to the powershell command
let pathToScriptPSString = (filePath: string) => {
Expand Down Expand Up @@ -137,6 +138,14 @@ async function run() {
artifactname: artifactName
};

let maxArtifactSize: number = getMaxArtifactSize();
if (maxArtifactSize != 0 ){
let artifactSize : number = getArtifactSize(pathToUpload)
if (artifactSize > maxArtifactSize){
tl.warning(tl.loc('ArtifactSizeExceeded', artifactSize, maxArtifactSize));
}
}

// upload or copy
if (artifactType === "container") {
data["containerfolder"] = artifactName;
Expand Down Expand Up @@ -228,4 +237,30 @@ function getParallelCount(): number {
return result;
}

function getMaxArtifactSize(): number {
let result = 0;
let inputValue: string = tl.getInput('MaxArtifactSize', false);
if (!(Number.isNaN(Number(inputValue)))) {
result = parseInt(inputValue);
}
return result;
}


function getArtifactSize(
pathToUpload : string
): number {
let totalSize = 0;
if(tl.stats(pathToUpload).isFile()) {
totalSize = tl.stats(pathToUpload).size;
}
else if(tl.stats(pathToUpload).isDirectory()) {
const files = fs.readdirSync(pathToUpload);
files.forEach (file => {
totalSize = totalSize + getArtifactSize(path.join(pathToUpload, file));
})
}
return totalSize;
}

run();
11 changes: 10 additions & 1 deletion Tasks/PublishBuildArtifactsV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"FilePath": "A file share"
}
},
{
"name": "MaxArtifactSize",
"type": "int",
"label": "Max Artifact Size",
"defaultValue": 0,
"required": false,
"helpMarkDown": "Maximum limit on the size of artifacts to be published in bytes. Put 0 if you don't want to set any limit."
},
{
"name": "TargetPath",
"type": "string",
Expand Down Expand Up @@ -109,6 +117,7 @@
"ErrorHostTypeNotSupported": "This task must run in a build to publish artifacts to Azure Pipelines.",
"PublishBuildArtifactsFailed": "Publishing build artifacts failed with an error: %s",
"UnexpectedParallelCount": "Unsupported parallel count '%s'. Enter a number between 1 and 128.",
"TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows"
"TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows",
"ArtifactSizeExceeded" : "The size of artifacts being published is %s bytes which is larger than the maximum limit %s bytes set for the published size of artifacts."
}
}
11 changes: 10 additions & 1 deletion Tasks/PublishBuildArtifactsV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"FilePath": "A file share"
}
},
{
"name": "MaxArtifactSize",
"type": "int",
"label": "ms-resource:loc.input.label.MaxArtifactSize",
"defaultValue": 0,
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.MaxArtifactSize"
},
{
"name": "TargetPath",
"type": "string",
Expand Down Expand Up @@ -109,6 +117,7 @@
"ErrorHostTypeNotSupported": "ms-resource:loc.messages.ErrorHostTypeNotSupported",
"PublishBuildArtifactsFailed": "ms-resource:loc.messages.PublishBuildArtifactsFailed",
"UnexpectedParallelCount": "ms-resource:loc.messages.UnexpectedParallelCount",
"TarExtractionNotSupportedInWindows": "ms-resource:loc.messages.TarExtractionNotSupportedInWindows"
"TarExtractionNotSupportedInWindows": "ms-resource:loc.messages.TarExtractionNotSupportedInWindows",
"ArtifactSizeExceeded": "ms-resource:loc.messages.ArtifactSizeExceeded"
}
}

0 comments on commit 95b8cce

Please sign in to comment.