name: RISC-V Images

on:
  pull_request:
    paths:
      - '.github/workflows/riscv64-images.yml'
      - '.github/workflows/publish-images.yml'
      - 'server/**'
      - 'web/**'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  build:
    name: Build ${{ matrix.name }} (linux/riscv64)
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: server
            context: ./server
            dockerfile: ./server/Dockerfile
          - name: web
            context: ./web
            dockerfile: ./web/Dockerfile

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          platforms: riscv64

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build RISC-V image
        uses: docker/build-push-action@v6
        with:
          context: ${{ matrix.context }}
          file: ${{ matrix.dockerfile }}
          platforms: linux/riscv64
          load: true
          tags: skillhub-${{ matrix.name }}:riscv64-ci
          cache-from: type=gha,scope=riscv64-${{ matrix.name }}
          cache-to: type=gha,mode=max,scope=riscv64-${{ matrix.name }}

      - name: Verify image architecture and runtime
        shell: bash
        run: |
          image="skillhub-${{ matrix.name }}:riscv64-ci"
          test "$(docker image inspect "$image" --format '{{.Architecture}}')" = riscv64
          case "${{ matrix.name }}" in
            server)
              docker run --rm --platform linux/riscv64 --entrypoint java "$image" -version
              ;;
            web)
              docker run --rm --platform linux/riscv64 --entrypoint nginx "$image" -v
              ;;
          esac
