From 89ada8ac1525a259eec28bde128a5b50bc5e6798 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 31 May 2023 13:05:54 +0200 Subject: [PATCH] Improving cache management (#2068) * Using setup-python cache option. Setting a workflow for cleaning up cache * Update .github/workflows/ci.yml --- .github/workflows/cache_cleaner.yml | 56 +++++++++++++++++++++++++++++ .github/workflows/ci.yml | 20 ++--------- 2 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/cache_cleaner.yml diff --git a/.github/workflows/cache_cleaner.yml b/.github/workflows/cache_cleaner.yml new file mode 100644 index 0000000000..e65222e589 --- /dev/null +++ b/.github/workflows/cache_cleaner.yml @@ -0,0 +1,56 @@ +name: cleanup caches by a branch +on: + workflow_dispatch: + pull_request: + types: + - closed + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + + - name: Cleanup PR caches + if: github.event_name != 'workflow_dispatch' + 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 && echo "Deleting cache with key: $cacheKey" + done + echo "Done" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Cleanup by workflow dispatch + if: github.event_name == 'workflow_dispatch' + run: | + gh extension install actions/gh-actions-cache + + REPO=${{ github.repository }} + + echo "Fetching list of cache key" + cacheKeysForPR=$(gh actions-cache list -R $REPO | 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 && echo "Deleting cache with key: $cacheKey" + done + echo "Done" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 949423ae17..051de94132 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,6 @@ env: # You should go up in number, if you go down (or repeat a previous value) # you might end up reusing a previous cache if it haven't been deleted already. # It applies 7 days retention policy by default. - RESET_PIP_CACHE: 0 RESET_EXAMPLES_CACHE: 0 RESET_DOC_BUILD_CACHE: 0 RESET_AUTOSUMMARY_CACHE: 0 @@ -100,6 +99,7 @@ jobs: - name: "Setup Python" uses: actions/setup-python@v4 with: + cache: 'pip' python-version: ${{ env.MAIN_PYTHON_VERSION }} - name: "Install OS packages" @@ -107,14 +107,6 @@ jobs: sudo apt update sudo apt install zip pandoc libgl1-mesa-glx xvfb texlive-latex-extra latexmk graphviz - - name: "Cache pip" - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}-${{ hashFiles('pyproject.toml') }} - restore-keys: | - Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }} - - name: "Test virtual framebuffer" run: | pip install -r .ci/requirements_test_xvfb.txt @@ -288,6 +280,8 @@ jobs: - name: "Setup Python" uses: actions/setup-python@v4 with: + cache: 'pip' + cache-dependency-path: pyproject.toml python-version: ${{ env.MAIN_PYTHON_VERSION }} - name: "Install os packages" @@ -295,14 +289,6 @@ jobs: sudo apt update sudo apt install libgl1-mesa-glx xvfb - - name: "Cache pip" - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}-${{ hashFiles('pyproject.toml') }} - restore-keys: | - Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }} - - name: "Test virtual framebuffer" run: | pip install -r .ci/requirements_test_xvfb.txt