Skip to content

Commit

Permalink
Speed up image building in CI by exporting and importing mount cache
Browse files Browse the repository at this point in the history
During the build, cache of ``uv`` and ``pip`` is stored in a separate
"cache mount" volum that is mounted during the build. This cache mount
volume is preserved between builds and can be exported and imported to
speed up the build process in CI - where cache is stored as artifact and
can be imported in the next build.

This PR implements it:

* export and import commands are added to breeze to export/import cache
  mount content

* the cache mount content is stashed as artifact in the build
  after image is built and it is restored before the image is built
  • Loading branch information
potiuk committed Jan 1, 2025
1 parent 4af49a0 commit 7504d5f
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 62 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/additional-ci-image-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ jobs:
# packages: write
# secrets: inherit
# with:
# platform: "linux/arm64"
# push-image: "false"
# upload-image-artifact: "true"
# upload-mount-cache-artifact: ${{ inputs.canary-run }}
# runs-on-as-json-public: ${{ inputs.runs-on-as-json-public }}
# runs-on-as-json-self-hosted: ${{ inputs.runs-on-as-json-self-hosted }}
# python-versions: ${{ inputs.python-versions }}
# platform: "linux/arm64"
# branch: ${{ inputs.branch }}
# constraints-branch: ${{ inputs.constraints-branch }}
# use-uv: ${{ inputs.use-uv}}
Expand Down
32 changes: 29 additions & 3 deletions .github/workflows/ci-image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ on: # yamllint disable-line rule:truthy
description: "Whether to upload docker image artifact"
required: true
type: string
upload-mount-cache-artifact:
description: "Whether to upload mount-cache artifact"
required: true
type: string
debian-version:
description: "Base Debian distribution to use for the build (bookworm)"
type: string
Expand Down Expand Up @@ -128,15 +132,24 @@ jobs:
run: ./scripts/ci/cleanup_docker.sh
- name: "Install Breeze"
uses: ./.github/actions/breeze
- name: "Restore ${{ inputs.image-type }} cache mount image ${{ inputs.platform }}:${{ inputs.python }}"
uses: apache/infrastructure-actions/stash/restore@c94b890bbedc2fc61466d28e6bd9966bc6c6643c
with:
key: "ci-cache-mount-save-v1-${{ inputs.platform }}-${{ env.PYTHON_MAJOR_MINOR_VERSION }}"
path: "/tmp/"
id: restore-cache-mount
- name: "Import mount-cache ${{ inputs.platform }}:${{ env.PYTHON_MAJOR_MINOR_VERSION }}"
run: >
breeze ci-image import-mount-cache
--cache-file /tmp/ci-cache-mount-save-v1-${{ env.PYTHON_MAJOR_MINOR_VERSION }}.tar.gz
if: steps.restore-cache-mount.outputs.stash-hit == 'true'
- name: "Login to ghcr.io"
run: echo "${{ env.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: >
Build ${{ inputs.push-image == 'true' && ' & push ' || '' }}
${{ inputs.platform }}:${{ env.PYTHON_MAJOR_MINOR_VERSION }} image
run: >
breeze ci-image build
--builder airflow_cache
--platform "${{ inputs.platform }}"
breeze ci-image build --platform "${{ inputs.platform }}"
env:
DOCKER_CACHE: ${{ inputs.docker-cache }}
DISABLE_AIRFLOW_REPO_CACHE: ${{ inputs.disable-airflow-repo-cache }}
Expand Down Expand Up @@ -164,3 +177,16 @@ jobs:
if-no-files-found: 'error'
retention-days: 2
if: inputs.upload-image-artifact == 'true'
- name: "Export mount cache ${{ inputs.platform }}:${{ env.PYTHON_MAJOR_MINOR_VERSION }}"
run: >
breeze ci-image export-mount-cache
--cache-file /tmp/ci-cache-mount-save-v1-${{ env.PYTHON_MAJOR_MINOR_VERSION }}.tar.gz
if: inputs.upload-mount-cache-artifact == 'true'
- name: "Stash cache mount ${{ inputs.platform }}:${{ env.PYTHON_MAJOR_MINOR_VERSION }}"
uses: apache/infrastructure-actions/stash/save@c94b890bbedc2fc61466d28e6bd9966bc6c6643c
with:
key: "ci-cache-mount-save-v1-${{ inputs.platform }}-${{ env.PYTHON_MAJOR_MINOR_VERSION }}"
path: "/tmp/ci-cache-mount-save-v1-${{ env.PYTHON_MAJOR_MINOR_VERSION }}.tar.gz"
if-no-files-found: 'error'
retention-days: 2
if: inputs.upload-mount-cache-artifact == 'true'
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ jobs:
platform: "linux/amd64"
push-image: "false"
upload-image-artifact: "true"
# upload-mount-cache-artifact: ${{ needs.build-info.outputs.canary-run }}
upload-mount-cache-artifact: "true"
python-versions: ${{ needs.build-info.outputs.python-versions }}
branch: ${{ needs.build-info.outputs.default-branch }}
use-uv: ${{ needs.build-info.outputs.force-pip == 'true' && 'false' || 'true' }}
Expand Down
22 changes: 22 additions & 0 deletions dev/breeze/doc/06_managing_docker_images.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ To load the image from specific job run (for example 12538475388), you can use t
:width: 100%
:alt: Breeze image artifacts

Exporting and importing CI image cache mount
............................................

During the build, cache of ``uv`` and ``pip`` is stored in a separate "cache mount" volum that is mounted
during the build. This cache mount volume is preserved between builds and can be exported and imported
to speed up the build process in CI - where cache is stored as artifact and can be imported in the next
build.

These are all available flags of ``export-mount-cache`` command:

.. image:: ./images/output_ci-image_export-mount-cache.svg
:target: https://raw.githubusercontent.com/apache/airflow/main/dev/breeze/images/output_ci-image_export-mount-cache.svg
:width: 100%
:alt: Breeze ci-image

These are all available flags of ``import-mount-cache`` command:

.. image:: ./images/output_ci-image_import-mount-cache.svg
:target: https://raw.githubusercontent.com/apache/airflow/main/dev/breeze/images/output_ci-image_import-mount-cache.svg
:width: 100%
:alt: Breeze ci-image import-mount-cache

PROD Image tasks
----------------

Expand Down
22 changes: 19 additions & 3 deletions dev/breeze/doc/images/output_ci-image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dev/breeze/doc/images/output_ci-image.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2039b80b810ff0ad9e9f505548254f82
de6e1ee2eb602569a8cd1a3c6c4cbafe
Loading

0 comments on commit 7504d5f

Please sign in to comment.