# This workflow implements continuous delivery with automated testing and fully
# autonomous deploys to production on merge.
name: CD

# Translated: "Execute this workflow on pushes to master OR on pull requests
# opened against master"
#
# See this question and answer for what we're solving here:
# https://github.community/t5/GitHub-Actions/How-to-trigger-an-action-on-push-or-pull-request-but-not-both/m-p/36155#M2460
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
    - name: Setup
      uses: actions/setup-go@v2

    - name: Checkout
      uses: actions/checkout@v2

    - name: Build
      run: make build

    - name: Lint
      run: make lint

    - name: Test
      run: git show && make testci

    - name: Code coverage
      uses: codecov/codecov-action@v1
      with:
        file: ./coverage.txt

  regression_test:
    name: Regression Tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go_version:
        - '1.13'
        - '1.14'
    steps:
    - name: Setup
      uses: actions/setup-go@v2
      with:
        go-version: ${{matrix.go_version}}

    - name: Checkout
      uses: actions/checkout@v2

    - name: Build
      run: make build

    - name: Test
      run: make test

  production_deploy:
    name: Production Deploy
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    needs: [test]
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Notify start
      id: deployment
      uses: bobheadxi/deployments@v0.4.2
      with:
        step: start
        token: ${{ secrets.GITHUB_TOKEN }}
        env: production

    - name: Deploy
      uses: superfly/flyctl-actions@1.1
      with:
        args: "deploy --strategy rolling"
      env:
        FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

    - name: Notify finish
      uses: bobheadxi/deployments@v0.4.2
      with:
        step: finish
        deployment_id: ${{ steps.deployment.outputs.deployment_id }}
        token: ${{ secrets.GITHUB_TOKEN }}
        env: production
        status: ${{ job.status }}
        env_url: 'https://httpbingo.org'