diff --git a/.github/workflows/clean-up-cache.yaml b/.github/workflows/clean-up-cache.yaml deleted file mode 100644 index 3936ed92..00000000 --- a/.github/workflows/clean-up-cache.yaml +++ /dev/null @@ -1,33 +0,0 @@ -name: cleanup caches by a branch -on: - pull_request: - types: - - closed - -jobs: - cleanup: - 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 }} diff --git a/.github/workflows/clean-up-storage.yaml b/.github/workflows/clean-up-storage.yaml new file mode 100644 index 00000000..3f70e09e --- /dev/null +++ b/.github/workflows/clean-up-storage.yaml @@ -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 }} diff --git a/.github/workflows/test-upload.yaml b/.github/workflows/test-upload.yaml new file mode 100644 index 00000000..e1f29602 --- /dev/null +++ b/.github/workflows/test-upload.yaml @@ -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 diff --git a/README.md b/README.md index 8bea4584..0458c912 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index a97c1660..1ae629ac 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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