generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 40
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
devsecops/emvaldes/workflow-housekeeper #17025
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
54ba9fe
Importing JosiahSiegel/workflow-housekeeper default state.
emvaldes 6d01616
Appending default configuration changes.
emvaldes d90af3a
Merge branch 'main' into devsecops/emvaldes/workflow-housekeeper
emvaldes 2053120
Merge branch 'main' into devsecops/emvaldes/workflow-housekeeper
devopsmatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.git | ||
_git | ||
.github | ||
_github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Workflow Housekeeper | ||
|
||
Retain a time period or quantity of workflow runs. | ||
|
||
[![Test action](https://github.com/CDCgov/prime-reportstream/.github/workflows/workflow-housekeeper--test_action.yml/badge.svg)](https://github.com/CDCgov/prime-reportstream/.github/workflows/workflow-housekeeper--test_action.yml) | ||
|
||
### Dependencies: | ||
|
||
>Change in repo: `Settings -> Actions -> General -> Workflow Permissions to allow read and write` | ||
|
||
## Inputs | ||
```yml | ||
ignore-branch-workflows: | ||
description: 'Ignore runs from workflows currently in ./github/workflow' | ||
required: false | ||
retention-time: | ||
description: 'Period of time to maintain history. E.g. "2 weeks", "3 days", etc.' | ||
required: false | ||
retain-run-count: | ||
description: 'Number of latest runs to keep' | ||
required: false | ||
dry-run: | ||
description: 'Only list runs pending deletion' | ||
required: false | ||
``` | ||
|
||
## Usage | ||
```yml | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Run workflow housekeeper | ||
uses: josiahsiegel/workflow-housekeeper@<CURRENT_VERSION> | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
``` | ||
or | ||
```yml | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Run workflow housekeeper | ||
uses: josiahsiegel/workflow-housekeeper@<CURRENT_VERSION> | ||
id: scan | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
ignore-branch-workflows: true | ||
retention-time: '1 days' | ||
retain-run-count: 1 | ||
dry-run: false | ||
``` | ||
|
||
## Generated summary | ||
### ✨ Workflow Housekeeper ✨ | ||
* .github/workflows/test_action.yml 4618840926 | ||
* .github/workflows/test_action.yml 4618827035 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# action.yml | ||
name: 'Workflow Housekeeper' | ||
description: 'Retain a time period or quantity of workflow runs.' | ||
branding: | ||
icon: 'trash-2' | ||
color: 'red' | ||
inputs: | ||
ignore-branch-workflows: | ||
description: 'Ignore runs from workflows currently in ./github/workflow' | ||
required: false | ||
retention-time: | ||
description: 'Period of time to maintain history. E.g. "2 weeks", "3 days", etc.' | ||
required: false | ||
retain-run-count: | ||
description: 'Number of latest runs to keep' | ||
required: false | ||
dry-run: | ||
description: 'Only list runs pending deletion' | ||
required: false | ||
outputs: | ||
housekeeping_output: | ||
description: 'Output of housekeeping steps' | ||
value: ${{ steps.local-action.outputs.housekeeping_output }} | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Run local action | ||
id: local-action | ||
run: | | ||
${{ github.action_path }}/lib/housekeeper.sh \ | ||
"${{ github.repository }}" \ | ||
"${{ inputs.ignore-branch-workflows }}" \ | ||
"${{ inputs.retention-time }}" \ | ||
"${{ inputs.retain-run-count }}" \ | ||
"${{ inputs.dry-run }}" | ||
shell: bash | ||
- name: Create summary | ||
run: | | ||
echo "### :sparkles: Workflow Housekeeper :sparkles:" >> $GITHUB_STEP_SUMMARY | ||
echo -e "${{ steps.local-action.outputs.housekeeping_output }}" >> $GITHUB_STEP_SUMMARY | ||
shell: bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/bin/bash | ||
|
||
# Init | ||
repo="$1" | ||
ignore_branch_workflows=$2 | ||
retention_time=$3 | ||
retain_run_count=$4 | ||
dry_run=$5 | ||
|
||
# ignore_branch_workflows | ||
if [[ -z $ignore_branch_workflows ]]; then | ||
ignore_branch_workflows=false | ||
fi | ||
echo "ignore_branch_workflows: $ignore_branch_workflows" | ||
|
||
if [[ $ignore_branch_workflows = true ]]; then | ||
files=$(ls -1 .github/workflows/ | sed 's/^/and .path != \".github\/workflows\//;s/$/\"/') | ||
else | ||
files="" | ||
fi | ||
|
||
# retention_time | ||
if [[ -z $retention_time ]]; then | ||
retention_time="" | ||
else | ||
keep_date=$(date -d "$date -$retention_time" +%s) | ||
keep_stmt="| select (.run_started_at | . == null or fromdateiso8601 < $keep_date)" | ||
fi | ||
echo "time_threshold: $retention_time" | ||
|
||
# retain_run_count | ||
if [[ -z $retain_run_count ]]; then | ||
retain_run_count=0 | ||
fi | ||
let retain_run_count2=retain_run_count*2 | ||
echo "retain_run_count: $retain_run_count2" | ||
|
||
# dry_run | ||
if [[ -z $dry_run ]]; then | ||
dry_run=false | ||
fi | ||
echo "dry_run: $dry_run" | ||
|
||
# Build jq query | ||
runs="repos/$repo/actions/runs" | ||
query=".workflow_runs[] \ | ||
| select( \ | ||
.path != \".github/workflows/placeholder.yaml\" \ | ||
"$files" | ||
) | ||
$keep_stmt | ||
| (.path,.id)" | ||
|
||
# Get run ids | ||
output=$(gh api --paginate $runs --jq "$query") | ||
output=($(echo $output | tr " " "\n")) | ||
output=${output[@]:$retain_run_count2} | ||
|
||
# Delete or echo run ids | ||
for id in $output; do | ||
if [[ $dry_run = false ]]; then | ||
[[ $id != ".git"* ]] && gh api --silent $runs/$id -X DELETE | ||
else | ||
[[ $id != ".git"* ]] && echo "gh api --silent $runs/$id -X DELETE" || echo "$id" | ||
fi | ||
[[ $id = ".git"* ]] && summary+=" * $id" || summary+=" $id\n" | ||
|
||
# Prevent rate limit | ||
sleep 1; | ||
done | ||
|
||
echo "housekeeping_output=$(echo "${summary}")" >>$GITHUB_OUTPUT |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will have this file manually removed as it has no direct value but it's important to preserve it if it's already defined in the imported GitHub Action.