---
name: Container for Kanidm

# this will build regardless,
# but only push to the container registry
# when you're committing on the master branch.

"on":
  push:

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - linux/arm64
          - linux/amd64
    steps:
      - uses: actions/checkout@v3

      - name: Update package manager
        run: sudo apt-get update
      - name: Install dependencies
        run: |
          sudo apt-get install -y \
            libpam0g-dev \
            libudev-dev \
            libssl-dev \
            libsqlite3-dev
      - name: Install latest stable
        uses: actions-rs/toolchain@v1
        with:
            toolchain: stable
            default: true
            components: cargo
      - name: Run cargo test
        run: cargo test -j1

  kanidm_build:
    needs: test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - linux/arm64
          - linux/amd64
    if: github.event_name == 'push'
    steps:
      - uses: actions/checkout@v3
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -  # https://github.com/docker/login-action/#github-container-registry
        name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push kanidmd
        id: docker_build_kanidmd
        uses: docker/build-push-action@v2
        with:
          push: ${{ github.ref == 'refs/heads/master' }}
          platforms: ${{matrix.target}}
          # https://github.com/docker/build-push-action/issues/254
          tags: ghcr.io/kanidm/kanidmd:devel
          build-args: |
            "KANIDM_BUILD_PROFILE=container_generic"
            "KANIDM_FEATURES="
            "KANIDM_BUILD_OPTIONS=-j1"
          file: kanidmd/Dockerfile
  radius_build:
    needs: test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - linux/arm64
          - linux/amd64
    if: github.event_name == 'push'
    steps:
      - uses: actions/checkout@v3
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -  # https://github.com/docker/login-action/#github-container-registry
        name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push radius
        id: docker_build_radius
        uses: docker/build-push-action@v2
        with:
          push: ${{ github.ref == 'refs/heads/master' }}
          platforms: ${{matrix.target}}
          # https://github.com/docker/build-push-action/issues/254
          tags: ghcr.io/kanidm/radius:devel
          context: ./kanidm_rlm_python/