name: Linux

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

# 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:
  # CCache is disabled for the Linux jobs for now.
  CCACHE_SIZE: "0M"
  CCACHE_CMD: true
  DOCKERFILE: picolibc/.github/Dockerfile
  IMAGE_FILE: dockerimg-linux.tar.zst
  IMAGE: picolibc-linux
  PACKAGES_FILE: picolibc/.github/linux-packages.txt
  EXTRA_FILE: picolibc/.github/linux-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@v6
        with:
          platforms: linux/amd64
          file: .github/Dockerfile
          tags: ${{ env.IMAGE }}:latest
          outputs: type=docker,force-compression=true,compression=zstd,compression-level=22,dest=${{ env.IMAGE_FILE }}

