name: Docker Compose
on:
  push:

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

jobs:
  test:
    name: Docker compose
    runs-on: ubuntu-latest
    env:
      COMPOSE_FILE: docker-compose.yml
      DOCKER_USER: ${{ secrets.DOCKER_USER }}
      DOCKER_PASS: ${{ secrets.DOCKER_PASS }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

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

      - name: Install Docker Compose
        run: |
          sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
          sudo chmod +x /usr/local/bin/docker-compose

      - name: Build docker images
        run: docker-compose build

  build:
    name: Docker build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Get branch name
        run: echo "BRANCH=${GITHUB_REF##*/}" >> $GITHUB_ENV

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASS }}

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

      - name: Build and push branch
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          push: true
          tags: microweber/microweber:${{ env.BRANCH }}
          build-args: |
            BRANCH=${{ env.BRANCH }}

      - name: Build and push latest
        id: docker_build_latest
        if: github.ref == 'refs/heads/master'
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          push: true
          tags: microweber/microweber:latest

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
