action.yml raw

   1  name: 'Configure Docker'
   2  description: 'Set up Docker build driver and configure build cache args'
   3  inputs:
   4    use-cirrus:
   5      description: 'Use cirrus cache'
   6      required: true
   7  runs:
   8    using: 'composite'
   9    steps:
  10      - name: Set up Docker Buildx
  11        uses: docker/setup-buildx-action@v3
  12        with:
  13          # Use host network to allow access to cirrus gha cache running on the host
  14          driver-opts: |
  15            network=host
  16  
  17      # This is required to allow buildkit to access the actions cache
  18      - name: Expose actions cache variables
  19        uses: actions/github-script@v8
  20        with:
  21          script: |
  22            Object.keys(process.env).forEach(function (key) {
  23              if (key.startsWith('ACTIONS_')) {
  24                core.info(`Exporting ${key}`);
  25                core.exportVariable(key, process.env[key]);
  26              }
  27            });
  28  
  29      - name: Construct docker build cache args
  30        shell: bash
  31        run: |
  32          # Configure docker build cache backend
  33          #
  34          # On forks the gha cache will work but will use Github's cache backend.
  35          # Docker will check for variables $ACTIONS_CACHE_URL, $ACTIONS_RESULTS_URL and $ACTIONS_RUNTIME_TOKEN
  36          # which are set automatically when running on GitHub infra: https://docs.docker.com/build/cache/backends/gha/#synopsis
  37  
  38          # Use cirrus cache host
  39          if [[ ${{ inputs.use-cirrus }} == 'true' ]]; then
  40            url_args="url=${CIRRUS_CACHE_HOST},url_v2=${CIRRUS_CACHE_HOST}"
  41          else
  42            url_args=""
  43          fi
  44  
  45          # Always optimistically --cache‑from in case a cache blob exists
  46          args=(--cache-from "type=gha${url_args:+,${url_args}},scope=${CONTAINER_NAME}")
  47  
  48          # Only add --cache-to when using the Cirrus cache provider and pushing to the default branch.
  49          if [[ ${{ inputs.use-cirrus }} == 'true' && ${{ github.event_name }} == "push" && ${{ github.ref_name }} == ${{ github.event.repository.default_branch }} ]]; then
  50            args+=(--cache-to "type=gha${url_args:+,${url_args}},mode=max,ignore-error=true,scope=${CONTAINER_NAME}")
  51          fi
  52  
  53          # Always `--load` into docker images (needed when using the `docker-container` build driver).
  54          args+=(--load)
  55  
  56          echo "DOCKER_BUILD_CACHE_ARG=${args[*]}" >> $GITHUB_ENV
  57