name: Linux

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

env:
  IMAGE_FILE: dockerimg.tar
  IMAGE: picolibc
  PACKAGES_FILE: picolibc/.github/packages.txt
  EXTRA_FILE: picolibc/.github/extra-files.txt

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

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

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

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

