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 job to clean up artefacts on PR closure #9

Merged
merged 7 commits into from
Jun 26, 2023
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
33 changes: 0 additions & 33 deletions .github/workflows/clean-up-cache.yaml

This file was deleted.

65 changes: 65 additions & 0 deletions .github/workflows/clean-up-storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: clean up storage by a branch
on:
pull_request:
types:
- closed
workflow_call:

jobs:
cleanup-action-cache:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Cleanup
run: |
gh extension install actions/gh-actions-cache

REPO=${{ github.repository }}
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"

echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )

## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
cleanup-action-artefact:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Cleanup
run: |
while true; do
# Retrieve a page of artifacts
ART_EXIST=$(gh api repos/$REPO/actions/artifacts?per_page=100\&page=$PAGE | jq -r '.artifacts[]')
ARTIFACTS=$(gh api repos/$REPO/actions/artifacts?per_page=100\&page=$PAGE | jq -r '.artifacts[] | select(.workflow_run.head_branch =="$GITHUB_HEAD_REF") | .id')
echo "PAGE: $PAGE"
# If there are no more artifacts, exit the loop
if [[ -z "$ART_EXIST" ]]; then
break
fi

# Loop through the artifacts on this page and delete the old ones
for ARTIFACT_ID in $ARTIFACTS; do
ARTIFACT_NAME=$(gh api repos/$1/actions/artifacts/$ARTIFACT_ID | jq -r '.name')
echo "Deleting artifact $ARTIFACT_NAME (ID: $ARTIFACT_ID)..."
gh api repos/$1/actions/artifacts/$ARTIFACT_ID -X DELETE
done

# Increment the page counter
PAGE=$((PAGE+1))
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21 changes: 21 additions & 0 deletions .github/workflows/test-upload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test artifact

concurrency:
group: ${{ github.ref_name }}-test
cancel-in-progress: true

on:
pull_request:
branches:
- main

jobs:
test-upload:
runs-on: ubuntu-latest
steps:
- run: |
echo "Test artifact" >> test.txt
- uses: actions/upload-artifact@v3
with:
name: test-upload
path: test.txt
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ The repository includes:
- [check-pr](.github/workflows/check-pr.yaml) workflow: when a new
PR is added to a repository or any change occurs to the pr, the pr is
validated to be sure that labels are valid.
- [clean-up-cache](.github/workflows/clean-up-cache.yaml) workflow: when a new
PR is closed, related cache is deleted.
- [clean-up-storage](.github/workflows/clean-up-storage.yaml) workflow: when a new
PR is closed, related cache and artefact are deleted.
- [markdown](.github/workflows/markdown.yaml) workflow: lint all markdown
documents and checks that links referenced in the documents are valid.
- [release-notes](.github/workflows/release-notes.yaml) workflow: automatically
Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Continuous Integration

- Add job to clean up artefacts on pr closure (PR #9 by @chicco785)
- Add workflow to clean-up action cache on PR closure (PR #8 by @chicco785)

### Documentation
Expand Down
Loading