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

Provide option to limit artifact size in PublishBuildArtifactsV1 #18293

Merged
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
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"
}
}