name: 'app-pipeline'
on:
  push:
  pull_request:
    types: [ opened, reopened ]
env:
  DOCKER_CLI_VERSION: "19.03.1"
jobs:
  unit-test:
    strategy:
      matrix:
        go-version: [1.14.x, 1.15.x]
        # todo: support windows
        platform: [ubuntu-latest, macos-latest]
        # platform: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.platform }}
    steps:

      - uses: actions/setup-go@v1
        with:
          go-version: ${{ matrix.go-version }}

      - uses: actions/checkout@v1

      - name: Cache go dependencies
        id: unit-cache-go-dependencies
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ matrix.go-version }}-

      - name: Install go dependencies
        if: steps.unit-cache-go-dependencies.outputs.cache-hit != 'true'
        run: go get ./...

      - name: Test
        run: make ci-unit-test

  build-artifacts:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v1
        with:
          go-version: '1.15.x'

      - uses: actions/checkout@v1

      - name: Install tooling
        run: |
          make ci-install-go-tools
          make ci-install-ci-tools

      - name: Cache go dependencies
        id: package-cache-go-dependencies
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-prod-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-prod-

      - name: Install dependencies
        if: steps.package-cache-go-dependencies.outputs.cache-hit != 'true'
        run: go get ./...

      - name: Linting, formatting, and other static code analyses
        run: make ci-static-analysis

      - name: Build snapshot artifacts
        run: make ci-build-snapshot-packages

      - run: docker images wagoodman/dive

      # todo: compare against known json output in shared volume
      - name: Test production image
        run: make ci-test-production-image

      - uses: actions/upload-artifact@master
        with:
          name: artifacts
          path: dist


  test-linux-artifacts:
    needs: [ build-artifacts ]
    runs-on: ubuntu-latest
    steps:

      - uses: actions/checkout@master

      - uses: actions/download-artifact@master
        with:
          name: artifacts
          path: dist

      - name: Test linux run
        run: make ci-test-linux-run

      - name: Test DEB package installation
        run: make ci-test-deb-package-install

      - name: Test RPM package installation
        run: make ci-test-rpm-package-install


  test-mac-artifacts:
    needs: [ build-artifacts ]
    runs-on: macos-latest
    steps:

      - uses: actions/checkout@master

      - uses: actions/download-artifact@master
        with:
          name: artifacts
          path: dist

      - name: Test darwin run
        run: make ci-test-mac-run


  test-windows-artifacts:
    needs: [ build-artifacts ]
    runs-on: windows-latest
    steps:

      - uses: actions/checkout@master

      - uses: actions/download-artifact@master
        with:
          name: artifacts
          path: dist

      - name: Test windows run
        run: make ci-test-windows-run


  release:
    needs: [ unit-test, build-artifacts, test-linux-artifacts, test-mac-artifacts, test-windows-artifacts ]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:

      - uses: actions/setup-go@v1
        with:
          go-version: '1.15.x'

      - uses: actions/checkout@v1

      - name: Install tooling
        run: make ci-install-ci-tools

      - name: Cache go dependencies
        id: release-cache-go-dependencies
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-prod-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-prod-

      - name: Install dependencies
        if: steps.release-cache-go-dependencies.outputs.cache-hit != 'true'
        run: go get ./...

      - name: Docker login
        run: make ci-docker-login
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

      - name: Publish GitHub release
        run: make ci-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Docker logout
        run: make ci-docker-logout

      - name: Smoke test published image
        run: make ci-test-production-image
