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