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 an option to specify retention period for artifacts #126

Merged
merged 2 commits into from Sep 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion action.yml
Expand Up @@ -17,6 +17,12 @@ inputs:
error: Fail the action with an error message
ignore: Do not output any warnings or errors, the action does not fail
default: 'warn'
retention-days:
description: >
Duration after which artifact will expire in days. 0 means using default retention.
konradpabjan marked this conversation as resolved.
Show resolved Hide resolved

Minimum 1 day.
Maximum 90 days unless changed from the repository settings page.
runs:
using: 'node12'
main: 'dist/index.js'
main: 'dist/index.js'
42 changes: 39 additions & 3 deletions dist/index.js
Expand Up @@ -3767,7 +3767,7 @@ class DefaultArtifactClient {
}
else {
// Create an entry for the artifact in the file container
const response = yield uploadHttpClient.createArtifactInFileContainer(name);
const response = yield uploadHttpClient.createArtifactInFileContainer(name, options);
if (!response.fileContainerResourceUrl) {
core.debug(response.toString());
throw new Error('No URL provided by the Artifact Service to upload an artifact to');
Expand Down Expand Up @@ -4019,6 +4019,9 @@ function run() {
const options = {
continueOnError: false
};
if (inputs.retentionDays) {
options.retentionDays = inputs.retentionDays;
}
const uploadResponse = yield artifactClient.uploadArtifact(inputs.artifactName, searchResult.filesToUpload, searchResult.rootDirectory, options);
if (uploadResponse.failedItems.length > 0) {
core.setFailed(`An error was encountered when uploading ${uploadResponse.artifactName}. There were ${uploadResponse.failedItems.length} items that failed to upload.`);
Expand Down Expand Up @@ -4108,6 +4111,10 @@ function getWorkSpaceDirectory() {
return workspaceDirectory;
}
exports.getWorkSpaceDirectory = getWorkSpaceDirectory;
function getRetentionDays() {
return process.env['GITHUB_RETENTION_DAYS'];
}
exports.getRetentionDays = getRetentionDays;
//# sourceMappingURL=config-variables.js.map

/***/ }),
Expand Down Expand Up @@ -6390,11 +6397,19 @@ function getInputs() {
if (!noFileBehavior) {
core.setFailed(`Unrecognized ${constants_1.Inputs.IfNoFilesFound} input. Provided: ${ifNoFilesFound}. Available options: ${Object.keys(constants_1.NoFileOptions)}`);
}
return {
const inputs = {
artifactName: name,
searchPath: path,
ifNoFilesFound: noFileBehavior
};
const retentionDaysStr = core.getInput(constants_1.Inputs.RetentionDays);
if (retentionDaysStr) {
inputs.retentionDays = parseInt(retentionDaysStr);
if (isNaN(inputs.retentionDays)) {
core.setFailed('Invalid retention-days');
}
}
return inputs;
}
exports.getInputs = getInputs;

Expand Down Expand Up @@ -6666,12 +6681,17 @@ class UploadHttpClient {
* @param {string} artifactName Name of the artifact being created
* @returns The response from the Artifact Service if the file container was successfully created
*/
createArtifactInFileContainer(artifactName) {
createArtifactInFileContainer(artifactName, options) {
return __awaiter(this, void 0, void 0, function* () {
const parameters = {
Type: 'actions_storage',
Name: artifactName
};
// calculate retention period
if (options && options.retentionDays) {
const maxRetentionStr = config_variables_1.getRetentionDays();
parameters.RetentionDays = utils_1.getProperRetention(options.retentionDays, maxRetentionStr);
}
const data = JSON.stringify(parameters, null, 2);
const artifactUrl = utils_1.getArtifactUrl();
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
Expand Down Expand Up @@ -7314,6 +7334,7 @@ var Inputs;
Inputs["Name"] = "name";
Inputs["Path"] = "path";
Inputs["IfNoFilesFound"] = "if-no-files-found";
Inputs["RetentionDays"] = "retention-days";
})(Inputs = exports.Inputs || (exports.Inputs = {}));
var NoFileOptions;
(function (NoFileOptions) {
Expand Down Expand Up @@ -8137,6 +8158,21 @@ function createEmptyFilesForArtifact(emptyFilesToCreate) {
});
}
exports.createEmptyFilesForArtifact = createEmptyFilesForArtifact;
function getProperRetention(retentionInput, retentionSetting) {
if (retentionInput < 0) {
throw new Error('Invalid retention, minimum value is 1.');
}
let retention = retentionInput;
if (retentionSetting) {
const maxRetention = parseInt(retentionSetting);
if (!isNaN(maxRetention) && maxRetention < retention) {
core_1.warning(`Retention days is greater than the max value allowed by the repository setting, reduce retention to ${maxRetention} days`);
retention = maxRetention;
}
}
return retention;
}
exports.getProperRetention = getProperRetention;
//# sourceMappingURL=utils.js.map

/***/ }),
Expand Down
6 changes: 3 additions & 3 deletions 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 package.json
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/actions/upload-artifact#readme",
"devDependencies": {
"@actions/artifact": "^0.3.5",
"@actions/artifact": "^0.4.0",
"@actions/core": "^1.2.3",
"@actions/glob": "^0.1.0",
"@actions/io": "^1.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
@@ -1,7 +1,8 @@
export enum Inputs {
Name = 'name',
Path = 'path',
IfNoFilesFound = 'if-no-files-found'
IfNoFilesFound = 'if-no-files-found',
RetentionDays = 'retention-days'
}

export enum NoFileOptions {
Expand Down
12 changes: 11 additions & 1 deletion src/input-helper.ts
Expand Up @@ -22,9 +22,19 @@ export function getInputs(): UploadInputs {
)
}

return {
const inputs = {
artifactName: name,
searchPath: path,
ifNoFilesFound: noFileBehavior
} as UploadInputs

const retentionDaysStr = core.getInput(Inputs.RetentionDays)
if (retentionDaysStr) {
inputs.retentionDays = parseInt(retentionDaysStr)
konradpabjan marked this conversation as resolved.
Show resolved Hide resolved
if (isNaN(inputs.retentionDays)) {
core.setFailed('Invalid retention-days')
}
}

return inputs
}
4 changes: 4 additions & 0 deletions src/upload-artifact.ts
Expand Up @@ -40,6 +40,10 @@ async function run(): Promise<void> {
const options: UploadOptions = {
continueOnError: false
}
if (inputs.retentionDays) {
options.retentionDays = inputs.retentionDays
}

const uploadResponse = await artifactClient.uploadArtifact(
inputs.artifactName,
searchResult.filesToUpload,
Expand Down
5 changes: 5 additions & 0 deletions src/upload-inputs.ts
Expand Up @@ -15,4 +15,9 @@ export interface UploadInputs {
* The desired behavior if no files are found with the provided search path
*/
ifNoFilesFound: NoFileOptions

/**
* Duration after which artifact will expire in days
*/
retentionDays: number
}