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

Feature request: Option for skipping recent artifacts #27

Merged
merged 9 commits into from
May 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ GITHUB_REPOSITORY=<owner/repo>
GITHUB_ACTION=<id>
AGE="1 seconds"
SKIP_TAGS=<yes/no like value>
SKIP_RECENT=<number>
1 change: 1 addition & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
age: '1 second'
skip-tags: true
skip-recent: 1

create-test-artifact:
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ jobs:
uses: c-hive/gha-remove-artifacts@v1
with:
age: '1 month'
skip-tags: true
# Optional inputs
# skip-tags: true
# skip-recent: 5
```

## Conventions
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ inputs:
skip-tags:
description: 'true/false. If enabled, tag build artifacts (e.g. release artifacts) will be kept.'
required: false
skip-recent:
description: 'Keep the specified number of artifacts even if they are older than the age.'
required: false
57 changes: 47 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ const yn = require("yn");

const devEnv = process.env.NODE_ENV === "dev";

const inputKeys = {
AGE: devEnv ? "AGE" : "age",
SKIP_TAGS: devEnv ? "SKIP_TAGS" : "skip-tags",
SKIP_RECENT: devEnv ? "SKIP_RECENT" : "skip-recent",
};

if (devEnv) {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
require("dotenv-safe").config();
}

function readInput(key, isRequired = false) {
if (devEnv) {
return process.env[key];
}

return core.getInput(key, { required: isRequired });
}

function getConfigs() {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const [age, units] = devEnv
? process.env.AGE.split(" ")
: core.getInput("age", { required: true }).split(" ");
const [age, units] = readInput(inputKeys.AGE, true).split(" ");
const maxAge = moment().subtract(age, units);

console.log(
Expand All @@ -27,6 +39,16 @@ function getConfigs() {
")"
);

const skipRecent = readInput(inputKeys.SKIP_RECENT);

if (skipRecent) {
const parsedRecent = Number(skipRecent);

if (Number.isNaN(parsedRecent)) {
throw new Error("skip-recent option must be type of number.");
}
}

return {
repo: {
owner,
Expand All @@ -36,9 +58,8 @@ function getConfigs() {
perPage: 100,
},
maxAge: moment().subtract(age, units),
skipTags: devEnv
? yn(process.env.SKIP_TAGS)
: yn(core.getInput("skip-tags")),
skipTags: yn(readInput(inputKeys.SKIP_TAGS)),
skipRecent: Number(skipRecent),
retriesEnabled: true,
};
}
Expand Down Expand Up @@ -101,6 +122,8 @@ async function run() {
}
);

let skippedArtifactsCounter = 0;

return octokit
.paginate(workflowRunsRequest, ({ data }, done) => {
const stopPagination = data.find(workflowRun => {
Expand All @@ -118,10 +141,10 @@ async function run() {
.then(workflowRuns => {
const artifactPromises = workflowRuns
.filter(workflowRun => {
const skipWorkflow =
const skipTaggedWorkflow =
configs.skipTags && taggedCommits.includes(workflowRun.head_sha);

if (skipWorkflow) {
if (skipTaggedWorkflow) {
console.log(`Skipping tagged run ${workflowRun.head_sha}`);

return false;
Expand All @@ -141,6 +164,20 @@ async function run() {
return octokit.paginate(workflowRunArtifactsRequest).then(artifacts =>
artifacts
.filter(artifact => {
const skipRecentArtifact =
configs.skipRecent &&
configs.skipRecent > skippedArtifactsCounter;

if (skipRecentArtifact) {
console.log(
`Skipping recent artifact (id: ${artifact.id}, name: ${artifact.name}).`
);

skippedArtifactsCounter += 1;

return false;
}

const createdAt = moment(artifact.created_at);

return createdAt.isBefore(configs.maxAge);
Expand All @@ -149,7 +186,7 @@ async function run() {
if (devEnv) {
return new Promise(resolve => {
console.log(
`Recognized development environment, preventing ${artifact.id} from being removed.`
`Recognized development environment, preventing artifact (id: ${artifact.id}, name: ${artifact.name}) from being removed.`
);

resolve();
Expand All @@ -163,7 +200,7 @@ async function run() {
})
.then(() => {
console.log(
`Successfully removed artifact with id ${artifact.id}.`
`Successfully removed artifact (id: ${artifact.id}, name: ${artifact.name}).`
);
});
})
Expand Down