Skip to content

Commit 59018c2

Browse files
committed
Add an option to specify retention period
1 parent 5874080 commit 59018c2

8 files changed

Lines changed: 66 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: 36 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,16 @@ 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+
}
6409+
return inputs;
63986410
}
63996411
exports.getInputs = getInputs;
64006412

@@ -6666,12 +6678,17 @@ class UploadHttpClient {
66666678
* @param {string} artifactName Name of the artifact being created
66676679
* @returns The response from the Artifact Service if the file container was successfully created
66686680
*/
6669-
createArtifactInFileContainer(artifactName) {
6681+
createArtifactInFileContainer(artifactName, options) {
66706682
return __awaiter(this, void 0, void 0, function* () {
66716683
const parameters = {
66726684
Type: 'actions_storage',
66736685
Name: artifactName
66746686
};
6687+
// calculate retention period
6688+
if (options && options.retentionDays) {
6689+
const maxRetentionStr = config_variables_1.getRetentionDays();
6690+
parameters.RetentionDays = utils_1.getProperRetention(options.retentionDays, maxRetentionStr);
6691+
}
66756692
const data = JSON.stringify(parameters, null, 2);
66766693
const artifactUrl = utils_1.getArtifactUrl();
66776694
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
@@ -7314,6 +7331,7 @@ var Inputs;
73147331
Inputs["Name"] = "name";
73157332
Inputs["Path"] = "path";
73167333
Inputs["IfNoFilesFound"] = "if-no-files-found";
7334+
Inputs["RetentionDays"] = "retention-days";
73177335
})(Inputs = exports.Inputs || (exports.Inputs = {}));
73187336
var NoFileOptions;
73197337
(function (NoFileOptions) {
@@ -8137,6 +8155,21 @@ function createEmptyFilesForArtifact(emptyFilesToCreate) {
81378155
});
81388156
}
81398157
exports.createEmptyFilesForArtifact = createEmptyFilesForArtifact;
8158+
function getProperRetention(retentionInput, retentionSetting) {
8159+
if (retentionInput < 0) {
8160+
throw new Error('Invalid retention, minimum value is 1.');
8161+
}
8162+
let retention = retentionInput;
8163+
if (retentionSetting) {
8164+
const maxRetention = parseInt(retentionSetting);
8165+
if (!isNaN(maxRetention) && maxRetention < retention) {
8166+
core_1.warning(`Retention days is greater than the max value allowed by the repository setting, reduce retention to ${maxRetention} days`);
8167+
retention = maxRetention;
8168+
}
8169+
}
8170+
return retention;
8171+
}
8172+
exports.getProperRetention = getProperRetention;
81408173
//# sourceMappingURL=utils.js.map
81418174

81428175
/***/ }),

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ 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)
2934
}
35+
36+
return inputs
3037
}

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)