Skip to content

Commit 604e071

Browse files
authored
Merge pull request #126 from yacaovsnc/main
Add an option to specify retention period for artifacts
2 parents 5874080 + 4560c23 commit 604e071

8 files changed

Lines changed: 72 additions & 10 deletions

File tree

action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ inputs:
1717
error: Fail the action with an error message
1818
ignore: Do not output any warnings or errors, the action does not fail
1919
default: 'warn'
20+
retention-days:
21+
description: >
22+
Duration after which artifact will expire in days. 0 means using default retention.
23+
24+
Minimum 1 day.
25+
Maximum 90 days unless changed from the repository settings page.
2026
runs:
2127
using: 'node12'
22-
main: 'dist/index.js'
28+
main: 'dist/index.js'

dist/index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,7 +3767,7 @@ class DefaultArtifactClient {
37673767
}
37683768
else {
37693769
// Create an entry for the artifact in the file container
3770-
const response = yield uploadHttpClient.createArtifactInFileContainer(name);
3770+
const response = yield uploadHttpClient.createArtifactInFileContainer(name, options);
37713771
if (!response.fileContainerResourceUrl) {
37723772
core.debug(response.toString());
37733773
throw new Error('No URL provided by the Artifact Service to upload an artifact to');
@@ -4019,6 +4019,9 @@ function run() {
40194019
const options = {
40204020
continueOnError: false
40214021
};
4022+
if (inputs.retentionDays) {
4023+
options.retentionDays = inputs.retentionDays;
4024+
}
40224025
const uploadResponse = yield artifactClient.uploadArtifact(inputs.artifactName, searchResult.filesToUpload, searchResult.rootDirectory, options);
40234026
if (uploadResponse.failedItems.length > 0) {
40244027
core.setFailed(`An error was encountered when uploading ${uploadResponse.artifactName}. There were ${uploadResponse.failedItems.length} items that failed to upload.`);
@@ -4108,6 +4111,10 @@ function getWorkSpaceDirectory() {
41084111
return workspaceDirectory;
41094112
}
41104113
exports.getWorkSpaceDirectory = getWorkSpaceDirectory;
4114+
function getRetentionDays() {
4115+
return process.env['GITHUB_RETENTION_DAYS'];
4116+
}
4117+
exports.getRetentionDays = getRetentionDays;
41114118
//# sourceMappingURL=config-variables.js.map
41124119

41134120
/***/ }),
@@ -6390,11 +6397,19 @@ function getInputs() {
63906397
if (!noFileBehavior) {
63916398
core.setFailed(`Unrecognized ${constants_1.Inputs.IfNoFilesFound} input. Provided: ${ifNoFilesFound}. Available options: ${Object.keys(constants_1.NoFileOptions)}`);
63926399
}
6393-
return {
6400+
const inputs = {
63946401
artifactName: name,
63956402
searchPath: path,
63966403
ifNoFilesFound: noFileBehavior
63976404
};
6405+
const retentionDaysStr = core.getInput(constants_1.Inputs.RetentionDays);
6406+
if (retentionDaysStr) {
6407+
inputs.retentionDays = parseInt(retentionDaysStr);
6408+
if (isNaN(inputs.retentionDays)) {
6409+
core.setFailed('Invalid retention-days');
6410+
}
6411+
}
6412+
return inputs;
63986413
}
63996414
exports.getInputs = getInputs;
64006415

@@ -6666,12 +6681,17 @@ class UploadHttpClient {
66666681
* @param {string} artifactName Name of the artifact being created
66676682
* @returns The response from the Artifact Service if the file container was successfully created
66686683
*/
6669-
createArtifactInFileContainer(artifactName) {
6684+
createArtifactInFileContainer(artifactName, options) {
66706685
return __awaiter(this, void 0, void 0, function* () {
66716686
const parameters = {
66726687
Type: 'actions_storage',
66736688
Name: artifactName
66746689
};
6690+
// calculate retention period
6691+
if (options && options.retentionDays) {
6692+
const maxRetentionStr = config_variables_1.getRetentionDays();
6693+
parameters.RetentionDays = utils_1.getProperRetention(options.retentionDays, maxRetentionStr);
6694+
}
66756695
const data = JSON.stringify(parameters, null, 2);
66766696
const artifactUrl = utils_1.getArtifactUrl();
66776697
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
@@ -7314,6 +7334,7 @@ var Inputs;
73147334
Inputs["Name"] = "name";
73157335
Inputs["Path"] = "path";
73167336
Inputs["IfNoFilesFound"] = "if-no-files-found";
7337+
Inputs["RetentionDays"] = "retention-days";
73177338
})(Inputs = exports.Inputs || (exports.Inputs = {}));
73187339
var NoFileOptions;
73197340
(function (NoFileOptions) {
@@ -8137,6 +8158,21 @@ function createEmptyFilesForArtifact(emptyFilesToCreate) {
81378158
});
81388159
}
81398160
exports.createEmptyFilesForArtifact = createEmptyFilesForArtifact;
8161+
function getProperRetention(retentionInput, retentionSetting) {
8162+
if (retentionInput < 0) {
8163+
throw new Error('Invalid retention, minimum value is 1.');
8164+
}
8165+
let retention = retentionInput;
8166+
if (retentionSetting) {
8167+
const maxRetention = parseInt(retentionSetting);
8168+
if (!isNaN(maxRetention) && maxRetention < retention) {
8169+
core_1.warning(`Retention days is greater than the max value allowed by the repository setting, reduce retention to ${maxRetention} days`);
8170+
retention = maxRetention;
8171+
}
8172+
}
8173+
return retention;
8174+
}
8175+
exports.getProperRetention = getProperRetention;
81408176
//# sourceMappingURL=utils.js.map
81418177

81428178
/***/ }),

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
"homepage": "https://github.com/actions/upload-artifact#readme",
3131
"devDependencies": {
32-
"@actions/artifact": "^0.3.5",
32+
"@actions/artifact": "^0.4.0",
3333
"@actions/core": "^1.2.3",
3434
"@actions/glob": "^0.1.0",
3535
"@actions/io": "^1.0.2",

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export enum Inputs {
22
Name = 'name',
33
Path = 'path',
4-
IfNoFilesFound = 'if-no-files-found'
4+
IfNoFilesFound = 'if-no-files-found',
5+
RetentionDays = 'retention-days'
56
}
67

78
export enum NoFileOptions {

src/input-helper.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ export function getInputs(): UploadInputs {
2222
)
2323
}
2424

25-
return {
25+
const inputs = {
2626
artifactName: name,
2727
searchPath: path,
2828
ifNoFilesFound: noFileBehavior
29+
} as UploadInputs
30+
31+
const retentionDaysStr = core.getInput(Inputs.RetentionDays)
32+
if (retentionDaysStr) {
33+
inputs.retentionDays = parseInt(retentionDaysStr)
34+
if (isNaN(inputs.retentionDays)) {
35+
core.setFailed('Invalid retention-days')
36+
}
2937
}
38+
39+
return inputs
3040
}

src/upload-artifact.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ async function run(): Promise<void> {
4040
const options: UploadOptions = {
4141
continueOnError: false
4242
}
43+
if (inputs.retentionDays) {
44+
options.retentionDays = inputs.retentionDays
45+
}
46+
4347
const uploadResponse = await artifactClient.uploadArtifact(
4448
inputs.artifactName,
4549
searchResult.filesToUpload,

src/upload-inputs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ export interface UploadInputs {
1515
* The desired behavior if no files are found with the provided search path
1616
*/
1717
ifNoFilesFound: NoFileOptions
18+
19+
/**
20+
* Duration after which artifact will expire in days
21+
*/
22+
retentionDays: number
1823
}

0 commit comments

Comments
 (0)