name: build

on:
  push:
  pull_request:
    branches: [ master ]

jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      - name: check out code
        uses: actions/checkout@v2
      - name: setup Go 1.16
        id: go
        uses: actions/setup-go@v2
        with:
          go-version: ^1.16
      - name: build
        run: make build
      - name: run Unit tests.
        run: go test  ./... -v -covermode=count -coverprofile=coverage.txt
      - name: upload Coverage report to CodeCov
        uses: codecov/codecov-action@v1.0.0
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./coverage.txt

  push_to_docker_latest:
    name: push master code to docker latest image
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    steps:
      - name: set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: cosmtrek/air:latest
      - name: show image digest
        run: echo ${{ steps.docker_build.outputs.digest }}