name: Container
on:
  push:
    branches:
      - stable
      - dev
    tags:
      - "*"
  pull_request:
    branches:
      - dev # No need for stable-pull-request, as that equals dev-push

jobs:
  build-and-test-amd64:
    name: Test amd64 image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Build amd64
        uses: docker/build-push-action@v3
        with:
          context: .
          tags: aflplusplus:test-amd64
          load: true
          cache-to: type=gha,mode=max
          build-args: |
            TEST_BUILD=1
      - name: Test amd64
        run: >
          docker run --rm aflplusplus:test-amd64 bash -c "
          apt-get update && 
          apt-get install -y libcmocka-dev && 
          make -i tests
          "

  push:
    name: Push amd64 and arm64 images
    runs-on: ubuntu-latest
    needs:
      - build-and-test-amd64
    if: ${{ github.event_name == 'push' && github.repository == 'AFLplusplus/AFLplusplus' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: arm64
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to docker.io
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}
      - name: Set tags to push
        id: push-tags
        run: |
          PUSH_TAGS=docker.io/aflplusplus/aflplusplus:${GITHUB_REF_NAME}
          if [ "${GITHUB_REF_NAME}" = "stable" ]; then
            PUSH_TAGS=${PUSH_TAGS},docker.io/aflplusplus/aflplusplus:latest
          fi
          export PUSH_TAGS
          echo "::set-output name=PUSH_TAGS::${PUSH_TAGS}"
      - name: Push to docker.io registry
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.push-tags.outputs.PUSH_TAGS }}
          cache-from: type=gha
