name: Zephyr

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

# When a PR is updated, cancel the jobs from the previous version. Merges
# do not define head_ref, so use run_id to never cancel those jobs.
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  # .github/do-zephyr --buildtype release
  # is the largest cache, 429.5M (September 2023).
  CCACHE_SIZE: "450M"
  CCACHE_CMD: ccache
  DOCKERFILE: picolibc/.github/Dockerfile-zephyr
  IMAGE_FILE: dockerimg-zephyr.tar.zst
  IMAGE: picolibc
  PACKAGES_FILE: picolibc/.github/zephyr-packages.txt
  EXTRA_FILE: picolibc/.github/zephyr-files.txt

jobs:
  cache-maker:
    runs-on: ubuntu-latest
    steps:
      - name: Clone picolibc
        uses: actions/checkout@v4
        with:
          path: picolibc

      - name: Cache the Docker Image
        id: cache
        uses: actions/cache@v4
        with:
          path: ${{ env.IMAGE_FILE }}
          key: ${{ env.IMAGE_FILE }}-${{ hashFiles( env.DOCKERFILE, env.PACKAGES_FILE, env.EXTRA_FILE ) }}
          lookup-only: true

      - name: Set up Docker Buildx
        if: steps.cache.outputs.cache-hit != 'true'
        uses: docker/setup-buildx-action@v3

      - name: Build picolibc container
        if: steps.cache.outputs.cache-hit != 'true'
        uses: docker/build-push-action@v5
        with:
          platforms: linux/amd64
          file: .github/Dockerfile-zephyr
          tags: ${{ env.IMAGE }}:latest
          outputs: type=docker,force-compression=true,compression=zstd,compression-level=22,dest=${{ env.IMAGE_FILE }}

