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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-and-test-amd64:
    name: Test amd64 image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Build amd64
        uses: docker/build-push-action@v6
        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
          "
  build-and-test-arm64:
    name: Test arm64 image
    runs-on: ubuntu-24.04-arm
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Build arm64
        uses: docker/build-push-action@v6
        with:
          context: .
          tags: aflplusplus:test-arm64
          load: true
          cache-to: type=gha,mode=max
          build-args: |
            TEST_BUILD=1
      - name: Test arm64
        run: >
          docker run --rm aflplusplus:test-arm64 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
      - build-and-test-arm64
    if: ${{ github.event_name == 'push' && github.repository == 'AFLplusplus/AFLplusplus' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - 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
