From e1165c0decd59f5e2ddddf1e23382b0dd9a84b3e Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Sun, 13 Nov 2022 21:40:58 +0000 Subject: [PATCH 1/8] Readmee update for deleting caches --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index ffc0116..4c0b2e7 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,44 @@ jobs: ``` +## Deleting caches +We can not re-use caches from pull request branches in other branches like main, such caches can eat up the storage quota and hence causing thrashing on more useful branches like main. In order to resolve this issue, we should use [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) to delete caches. This workflow uses gh-actions-cache to delete all the cache created by pull requests. + +``` +name: cleanup-caches +on: + schedule: + - cron: '0 */3 * * *' + +jobs: + release: + 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 }} + + echo "Fetching list of cache key" + ## This will extract out all the cache keys for pull requests + cacheKeysForPR=$(gh actions-cache list -R $REPO | grep "refs/pull" | cut -d $'\t' -f 1 ) + + ## Setting this to not fail the workflow while deleting duplicate cache keys. We can have same cache key for multiple branches based on the cache key generation. + set +e + echo "Deleting caches..." + for cacheKey in $cacheKeysForPR + do + gh actions-cache delete $cacheKey -R $REPO --confirm + done + echo "Done" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} +``` + ## Known practices and workarounds Following are some of the known practices/workarounds which community has used to fulfill specific requirements. You may choose to use them if suits your use case. Note these are not necessarily the only or the recommended solution. From b65f98495c0caddaa587a1be78e1a32ead7237c7 Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Sun, 13 Nov 2022 21:44:53 +0000 Subject: [PATCH 2/8] minor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c0b2e7..144bf9b 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ jobs: ## Deleting caches -We can not re-use caches from pull request branches in other branches like main, such caches can eat up the storage quota and hence causing thrashing on more useful branches like main. In order to resolve this issue, we should use [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) to delete caches. This workflow uses gh-actions-cache to delete all the cache created by pull requests. +We can not re-use caches from pull request branches in other branches like main, such caches can eat up the storage quota and hence causing thrashing on more useful branches like main. In order to resolve this issue, we can use [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) to delete caches. This workflow uses `gh-actions-cache` to delete all the caches created by all the pull requests. ``` name: cleanup-caches From a90fbffdad6944937abe17e3b9d75fc97fbb27bb Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Mon, 14 Nov 2022 05:55:57 +0000 Subject: [PATCH 3/8] minor --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 144bf9b..3d7674e 100644 --- a/README.md +++ b/README.md @@ -236,8 +236,7 @@ We can not re-use caches from pull request branches in other branches like main, ``` name: cleanup-caches on: - schedule: - - cron: '0 */3 * * *' + workflow_dispatch jobs: release: From 53812f9a6a6bb564335467df7bd0c28f0f4c1d31 Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Wed, 16 Nov 2022 22:21:12 +0000 Subject: [PATCH 4/8] PR comments --- README.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3d7674e..2a78109 100644 --- a/README.md +++ b/README.md @@ -230,13 +230,20 @@ jobs: ``` -## Deleting caches -We can not re-use caches from pull request branches in other branches like main, such caches can eat up the storage quota and hence causing thrashing on more useful branches like main. In order to resolve this issue, we can use [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) to delete caches. This workflow uses `gh-actions-cache` to delete all the caches created by all the pull requests. +## Force deletion of caches overriding default cache eviction policy +Caches have [branch scope restriction](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache) in place. This means that if caches for a specific branch are using a lot of storage quota, it may result into more frequently used caches from `default` branch getting thrashed. For example, if there are many pull requests happening on a repo and are creating caches, these cannot be used in default branch scope but will still occupy a lot of space till they get cleaned up by [eviction policy](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy). But sometime we want to clean them up on a faster cadence so as to ensure default branch is not thrashing. In order to achieve this, [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) can be used to delete caches for specific branches. -``` +This workflow uses `gh-actions-cache` to delete all the caches created by a branch. +
+ Example + +```yaml name: cleanup-caches on: - workflow_dispatch + pull_request: + types: + - closed + workflow_dispatch: jobs: release: @@ -250,22 +257,22 @@ jobs: gh extension install actions/gh-actions-cache REPO=${{ github.repository }} + BRANCH=${{ github.ref }} echo "Fetching list of cache key" - ## This will extract out all the cache keys for pull requests - cacheKeysForPR=$(gh actions-cache list -R $REPO | grep "refs/pull" | cut -d $'\t' -f 1 ) - - ## Setting this to not fail the workflow while deleting duplicate cache keys. We can have same cache key for multiple branches based on the cache key generation. + cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 ) + set +e echo "Deleting caches..." for cacheKey in $cacheKeysForPR do - gh actions-cache delete $cacheKey -R $REPO --confirm + gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm done echo "Done" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` +
## Known practices and workarounds Following are some of the known practices/workarounds which community has used to fulfill specific requirements. You may choose to use them if suits your use case. Note these are not necessarily the only or the recommended solution. From 36aa59375f3f8a4de02f27846d260c2ae7d94bd0 Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Wed, 16 Nov 2022 22:23:29 +0000 Subject: [PATCH 5/8] PR comments --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a78109..1713238 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ This workflow uses `gh-actions-cache` to delete all the caches created by a bran Example ```yaml -name: cleanup-caches +name: cleanup caches for a branch on: pull_request: types: @@ -246,7 +246,7 @@ on: workflow_dispatch: jobs: - release: + cleanup: runs-on: ubuntu-latest steps: - name: Check out code From 82f0974fd6256564d9878226743b0e0e4fb1f7fb Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Wed, 16 Nov 2022 22:23:44 +0000 Subject: [PATCH 6/8] PR comments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1713238..ef328f6 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ This workflow uses `gh-actions-cache` to delete all the caches created by a bran Example ```yaml -name: cleanup caches for a branch +name: cleanup caches by a branch on: pull_request: types: From 67408f6dab514553e8e8e253ecc9af0c493e877a Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Thu, 17 Nov 2022 22:37:51 +0000 Subject: [PATCH 7/8] moved to tips-and-workarounds --- README.md | 45 +------------------ workarounds.md => tips-and-workarounds.md | 55 ++++++++++++++++++++--- 2 files changed, 51 insertions(+), 49 deletions(-) rename workarounds.md => tips-and-workarounds.md (57%) diff --git a/README.md b/README.md index ef328f6..b7f0e98 100644 --- a/README.md +++ b/README.md @@ -230,50 +230,6 @@ jobs: ``` -## Force deletion of caches overriding default cache eviction policy -Caches have [branch scope restriction](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache) in place. This means that if caches for a specific branch are using a lot of storage quota, it may result into more frequently used caches from `default` branch getting thrashed. For example, if there are many pull requests happening on a repo and are creating caches, these cannot be used in default branch scope but will still occupy a lot of space till they get cleaned up by [eviction policy](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy). But sometime we want to clean them up on a faster cadence so as to ensure default branch is not thrashing. In order to achieve this, [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) can be used to delete caches for specific branches. - -This workflow uses `gh-actions-cache` to delete all the caches created by a branch. -
- Example - -```yaml -name: cleanup caches by a branch -on: - pull_request: - types: - - closed - workflow_dispatch: - -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=${{ github.ref }} - - echo "Fetching list of cache key" - cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 ) - - 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 }} -``` -
- ## Known practices and workarounds Following are some of the known practices/workarounds which community has used to fulfill specific requirements. You may choose to use them if suits your use case. Note these are not necessarily the only or the recommended solution. @@ -281,6 +237,7 @@ Following are some of the known practices/workarounds which community has used t - [Update a cache](./workarounds.md#update-a-cache) - [Use cache across feature branches](./workarounds.md#use-cache-across-feature-branches) - [Improving cache restore performance on Windows/Using cross-os caching](./workarounds.md#improving-cache-restore-performance-on-windows-using-cross-os-caching) +- [Force deletion of caches overriding default cache eviction policy](./workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy) #### Windows environment variables Please note that Windows environment variables (like `%LocalAppData%`) will NOT be expanded by this action. Instead, prefer using `~` in your paths which will expand to HOME directory. For example, instead of `%LocalAppData%`, use `~\AppData\Local`. For a list of supported default environment variables, see [this](https://docs.github.com/en/actions/learn-github-actions/environment-variables) page. diff --git a/workarounds.md b/tips-and-workarounds.md similarity index 57% rename from workarounds.md rename to tips-and-workarounds.md index 443bafc..ba0d27b 100644 --- a/workarounds.md +++ b/tips-and-workarounds.md @@ -1,9 +1,9 @@ -#### Cache segment restore timeout +## Cache segment restore timeout A cache gets downloaded in multiple segments of fixed sizes (`1GB` for a `32-bit` runner and `2GB` for a `64-bit` runner). Sometimes, a segment download gets stuck which causes the workflow job to be stuck forever and fail. Version `v3.0.8` of `actions/cache` introduces a segment download timeout. The segment download timeout will allow the segment download to get aborted and hence allow the job to proceed with a cache miss. Default value of this timeout is 60 minutes and can be customized by specifying an [environment variable](https://docs.github.com/en/actions/learn-github-actions/environment-variables) named `SEGMENT_DOWNLOAD_TIMEOUT_MINS` with timeout value in minutes. -#### Update a cache +## Update a cache A cache today is immutable and cannot be updated. But some use cases require the cache to be saved even though there was a "hit" during restore. To do so, use a `key` which is unique for every run and use `restore-keys` to restore the nearest cache. For example: ```yaml - name: update cache on every commit @@ -16,10 +16,10 @@ A cache today is immutable and cannot be updated. But some use cases require the ``` Please note that this will create a new cache on every run and hence will consume the cache [quota](#cache-limits). -#### Use cache across feature branches +## Use cache across feature branches Reusing cache across feature branches is not allowed today to provide cache [isolation](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache). However if both feature branches are from the default branch, a good way to achieve this is to ensure that the default branch has a cache. This cache will then be consumable by both feature branches. -#### Improving cache restore performance on Windows/Using cross-os caching +## Improving cache restore performance on Windows/Using cross-os caching Currently, cache restore is slow on Windows due to tar being inherently slow and the compression algorithm `gzip` in use. `zstd` is the default algorithm in use on linux and macos. It was disabled on Windows due to issues with bsd tar(libarchive), the tar implementation in use on Windows. To improve cache restore performance, we can re-enable `zstd` as the compression algorithm using the following workaround. Add the following step to your workflow before the cache step: @@ -35,4 +35,49 @@ To improve cache restore performance, we can re-enable `zstd` as the compression The `cache` action will use GNU tar instead of bsd tar on Windows. This should work on all Github Hosted runners as it is. For self-hosted runners, please ensure you have GNU tar and `zstd` installed. -The above workaround is also needed if you wish to use cross-os caching since difference of compression algorithms will result in different cache versions for the same cache key. So the above workaround will ensure `zstd` is used for caching on all platforms thus resulting in the same cache version for the same cache key. \ No newline at end of file +The above workaround is also needed if you wish to use cross-os caching since difference of compression algorithms will result in different cache versions for the same cache key. So the above workaround will ensure `zstd` is used for caching on all platforms thus resulting in the same cache version for the same cache key. + +## Force deletion of caches overriding default cache eviction policy +Caches have [branch scope restriction](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache) in place. This means that if caches for a specific branch are using a lot of storage quota, it may result into more frequently used caches from `default` branch getting thrashed. For example, if there are many pull requests happening on a repo and are creating caches, these cannot be used in default branch scope but will still occupy a lot of space till they get cleaned up by [eviction policy](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy). But sometime we want to clean them up on a faster cadence so as to ensure default branch is not thrashing. In order to achieve this, [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) can be used to delete caches for specific branches. + +This workflow uses `gh-actions-cache` to delete all the caches created by a branch. +
+ Example + +```yaml +name: cleanup caches by a branch +on: + pull_request: + types: + - closed + workflow_dispatch: + +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=${{ github.ref }} + + 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 }} +``` +
\ No newline at end of file From abb58eaf291527f54f08c452fa669625b7cd159f Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Thu, 17 Nov 2022 22:39:12 +0000 Subject: [PATCH 8/8] minor --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b7f0e98..17676c0 100644 --- a/README.md +++ b/README.md @@ -233,11 +233,11 @@ jobs: ## Known practices and workarounds Following are some of the known practices/workarounds which community has used to fulfill specific requirements. You may choose to use them if suits your use case. Note these are not necessarily the only or the recommended solution. -- [Cache segment restore timeout](./workarounds.md#cache-segment-restore-timeout) -- [Update a cache](./workarounds.md#update-a-cache) -- [Use cache across feature branches](./workarounds.md#use-cache-across-feature-branches) -- [Improving cache restore performance on Windows/Using cross-os caching](./workarounds.md#improving-cache-restore-performance-on-windows-using-cross-os-caching) -- [Force deletion of caches overriding default cache eviction policy](./workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy) +- [Cache segment restore timeout](./tips-and-workarounds.md#cache-segment-restore-timeout) +- [Update a cache](./tips-and-workarounds.md#update-a-cache) +- [Use cache across feature branches](./tips-and-workarounds.md#use-cache-across-feature-branches) +- [Improving cache restore performance on Windows/Using cross-os caching](./tips-and-workarounds.md#improving-cache-restore-performance-on-windows-using-cross-os-caching) +- [Force deletion of caches overriding default cache eviction policy](./tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy) #### Windows environment variables Please note that Windows environment variables (like `%LocalAppData%`) will NOT be expanded by this action. Instead, prefer using `~` in your paths which will expand to HOME directory. For example, instead of `%LocalAppData%`, use `~\AppData\Local`. For a list of supported default environment variables, see [this](https://docs.github.com/en/actions/learn-github-actions/environment-variables) page.