From 38032c50a3426a3053ca03c3b9df1263c46195bd Mon Sep 17 00:00:00 2001 From: Luca Cigarini Date: Fri, 14 Mar 2025 15:00:26 +0100 Subject: [PATCH] APT caching revisited --- .github/actions/apt_requirements/action.yml | 53 ++++++++++++++++----- .github/workflows/create_apt_cache.yaml | 39 +++++++++++++++ CHANGELOG.md | 9 ++++ actions/apt_requirements/action.yml | 53 ++++++++++++++++----- workflows/create_apt_cache.yaml | 39 +++++++++++++++ 5 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/create_apt_cache.yaml create mode 100644 workflows/create_apt_cache.yaml diff --git a/.github/actions/apt_requirements/action.yml b/.github/actions/apt_requirements/action.yml index 872cbe5..0024e2c 100644 --- a/.github/actions/apt_requirements/action.yml +++ b/.github/actions/apt_requirements/action.yml @@ -1,9 +1,6 @@ -name: Composite action install apt requirements +name: Composite action install apt requirements description: Composite action install apt requirements inputs: - working_directory: - description: Working directory - required: true requirements_file: description: Requirements file required: true @@ -11,15 +8,47 @@ inputs: runs: using: "composite" steps: - - name: Export apt requirements - id: export-apt-requirements + - name: Compute APT requirements file SHA256 hash + id: compute_apt_requirements_file_sha256_hash + run: | + COMPUTED_HASH=$(sha256sum ${{ inputs.requirements_file }} | cut -d ' ' -f 1) + echo "cache_key=$COMPUTED_HASH" >> $GITHUB_OUTPUT + shell: bash + + # Vital to be able to restore cache + # If write permission is not set, a permissions error will be raised + - name: Modification to /var/cache/apt/archives permissions run: | - PKG=$(cat ${{ inputs.requirements_file }}) - echo apt_packages=$PKG | awk '{print}' ORS=' ' >> $GITHUB_OUTPUT + sudo chmod a+w /var/cache/apt/archives shell: bash - - name: Cache apt packages - id: cache-apt-packages - uses: awalsh128/cache-apt-pkgs-action@latest + - uses: actions/cache/restore@v4 + id: restore_cache_from_parent_branch + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.base_ref }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} + + - uses: actions/cache/restore@v4 + id: restore_cache_from_current_branch + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} + + - name: Refresh repositories + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' && steps.restore_cache_from_current_branch.outputs.cache-hit != 'true' + run: | + sudo apt-get update + shell: bash + + - name: Install requirements + run: | + sudo apt-get install -y --no-install-recommends $(tr '\n' ' ' < ${{ inputs.requirements_file }}) + shell: bash + + - uses: actions/cache/save@v4 + id: cache_apt_requirements_for_current_branch + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' && steps.restore_cache_from_current_branch.outputs.cache-hit != 'true' with: - packages: ${{ steps.export-apt-requirements.outputs.apt_packages }} \ No newline at end of file + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} \ No newline at end of file diff --git a/.github/workflows/create_apt_cache.yaml b/.github/workflows/create_apt_cache.yaml new file mode 100644 index 0000000..2315fc9 --- /dev/null +++ b/.github/workflows/create_apt_cache.yaml @@ -0,0 +1,39 @@ +name: Create APT cache + +# GitHub will remove any cache entries that have not been accessed in over 7 days. + +on: + push: + branches: + - main + - master + - develop + - dev + paths: + # Path to APT requirements file + - '.github/test/python_test/packages.txt' + + +jobs: + create-cache: + name: Create cache for dependencies + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # Remember to set the same APT requirements file path set before! + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get -y install --no-install-recommends $(tr '\n' ' ' < apt_packages.txt) + + - name: Compute apt_packages.txt SHA256 hash + id: compute_apt_packages_sha256_hash + run: | + COMPUTED_HASH=$(sha256sum apt_packages.txt | cut -d ' ' -f 1) + echo "cache_key=$COMPUTED_HASH" >> $GITHUB_OUTPUT + + - uses: actions/cache/save@v4 + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_packages_sha256_hash.outputs.cache_key }} diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29..3ccc691 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# 1.6.x +## 1.6.0 +### Features + +* Added *create_apt_cache.yaml* workflow to cache APT requirements each time a commit is pushed on selected branch and **when the requirements file has changed**. + +### Changes + +* Removed `awalsh128/cache-apt-pkgs-action@latest` action and rewrote APT caching using GitHub's `actions/cache/restore@v4` and `actions/cache/save@v4`. diff --git a/actions/apt_requirements/action.yml b/actions/apt_requirements/action.yml index 872cbe5..0024e2c 100644 --- a/actions/apt_requirements/action.yml +++ b/actions/apt_requirements/action.yml @@ -1,9 +1,6 @@ -name: Composite action install apt requirements +name: Composite action install apt requirements description: Composite action install apt requirements inputs: - working_directory: - description: Working directory - required: true requirements_file: description: Requirements file required: true @@ -11,15 +8,47 @@ inputs: runs: using: "composite" steps: - - name: Export apt requirements - id: export-apt-requirements + - name: Compute APT requirements file SHA256 hash + id: compute_apt_requirements_file_sha256_hash + run: | + COMPUTED_HASH=$(sha256sum ${{ inputs.requirements_file }} | cut -d ' ' -f 1) + echo "cache_key=$COMPUTED_HASH" >> $GITHUB_OUTPUT + shell: bash + + # Vital to be able to restore cache + # If write permission is not set, a permissions error will be raised + - name: Modification to /var/cache/apt/archives permissions run: | - PKG=$(cat ${{ inputs.requirements_file }}) - echo apt_packages=$PKG | awk '{print}' ORS=' ' >> $GITHUB_OUTPUT + sudo chmod a+w /var/cache/apt/archives shell: bash - - name: Cache apt packages - id: cache-apt-packages - uses: awalsh128/cache-apt-pkgs-action@latest + - uses: actions/cache/restore@v4 + id: restore_cache_from_parent_branch + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.base_ref }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} + + - uses: actions/cache/restore@v4 + id: restore_cache_from_current_branch + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} + + - name: Refresh repositories + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' && steps.restore_cache_from_current_branch.outputs.cache-hit != 'true' + run: | + sudo apt-get update + shell: bash + + - name: Install requirements + run: | + sudo apt-get install -y --no-install-recommends $(tr '\n' ' ' < ${{ inputs.requirements_file }}) + shell: bash + + - uses: actions/cache/save@v4 + id: cache_apt_requirements_for_current_branch + if: steps.restore_cache_from_parent_branch.outputs.cache-hit != 'true' && steps.restore_cache_from_current_branch.outputs.cache-hit != 'true' with: - packages: ${{ steps.export-apt-requirements.outputs.apt_packages }} \ No newline at end of file + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_requirements_file_sha256_hash.outputs.cache_key }} \ No newline at end of file diff --git a/workflows/create_apt_cache.yaml b/workflows/create_apt_cache.yaml new file mode 100644 index 0000000..2315fc9 --- /dev/null +++ b/workflows/create_apt_cache.yaml @@ -0,0 +1,39 @@ +name: Create APT cache + +# GitHub will remove any cache entries that have not been accessed in over 7 days. + +on: + push: + branches: + - main + - master + - develop + - dev + paths: + # Path to APT requirements file + - '.github/test/python_test/packages.txt' + + +jobs: + create-cache: + name: Create cache for dependencies + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # Remember to set the same APT requirements file path set before! + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get -y install --no-install-recommends $(tr '\n' ' ' < apt_packages.txt) + + - name: Compute apt_packages.txt SHA256 hash + id: compute_apt_packages_sha256_hash + run: | + COMPUTED_HASH=$(sha256sum apt_packages.txt | cut -d ' ' -f 1) + echo "cache_key=$COMPUTED_HASH" >> $GITHUB_OUTPUT + + - uses: actions/cache/save@v4 + with: + path: /var/cache/apt/archives/*.deb + key: ${{ github.ref_name }}-${{ steps.compute_apt_packages_sha256_hash.outputs.cache_key }}