name: Test Mix

on:
  push:
    paths-ignore:
      - '**.md'
      - '**.bat'
      - 'ecs/**'
  pull_request:
    paths-ignore:
      - '**.md'
      - '**.bat'
      - 'ecs/**'

jobs:
  build-mix:
    name: Build Mix
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: true
      max-parallel: 1

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build mix image
        run: |
            (cd mix && docker build -t ejabberd/mix .)

      - name: Build ecs image
        run: |
            cd ecs
            ./build.sh latest

      - name: Run ecs image
        run: |
            docker images
            docker run --name ejabberd -d -p 5222:5222 ejabberd/ecs:latest

      - name: Wait ejabberd started
        run: |
            docker exec ejabberd bin/ejabberdctl started

      - name: Check ecs results
        run: |
            docker ps -s
            docker logs ejabberd
            docker logs ejabberd | grep -q "Start accepting TCP connections" || exit 1
            docker logs ejabberd | grep -q "error" && exit 1 || exit 0
            docker logs ejabberd | grep -q "Error" && exit 1 || exit 0

      - name: Save image
        run: |
            docker image save ejabberd/ecs:latest --output ejabberd-latest.tar

      - name: Upload image
        uses: actions/upload-artifact@v2
        with:
          name: ejabberd-image
          path: ejabberd-latest.tar

      - run: |
          echo "::notice::To get this image, download ejabberd-image.zip, "\
                         "uncompress it and run: " \
                         "docker image load -i ejabberd-latest.tar"
