name: Master Release
on:
  push:
    branches:
      - master
    paths-ignore:
      - '**.md'

jobs:
  compile:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'
      - uses: actions/setup-node@v1
        with:
          node-version: '10.x'
      - uses: actions/checkout@v2

      - name: Add GOBIN to PATH
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Install Global Dependencies
        run: npm install -g yarn sass cross-env

      - name: Download Frontend Dependencies
        if: steps.nodecache.outputs.cache-hit != 'true'
        working-directory: ./frontend
        run: yarn

      - name: Download Go mods
        if: steps.golangcache.outputs.cache-hit != 'true'
        run: |
          go mod download
          go mod verify
          make test-deps

      - name: Build Frontend Statping
        run: make clean compile

      - name: Upload Compiled Frontend (rice-box.go)
        uses: actions/upload-artifact@v1
        with:
          name: static-rice-box
          path: ./source

  test:
    needs: compile
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: password123
          POSTGRES_DB: statping
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: password123
          MYSQL_DATABASE: statping
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'
      - uses: actions/setup-node@v1
        with:
          node-version: '10.x'
      - uses: actions/checkout@v2

      - name: Install Global Dependencies
        run: npm install -g yarn sass newman cross-env wait-on @sentry/cli

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Install Statping
        env:
          VERSION: ${{ env.VERSION }}
        run: |
          make build
          chmod +x statping
          mv statping $(go env GOPATH)/bin/

      - name: Go Tests
        run: |
          go get gotest.tools/gotestsum
          gotestsum --no-summary=skipped --format dots -- -covermode=count -coverprofile=coverage.out -p=1 ./...
        env:
          VERSION: ${{ env.VERSION }}
          DB_CONN: sqlite3
          STATPING_DIR: ${{ github.workspace }}
          API_SECRET: demopassword123
          DISABLE_LOGS: true
          ALLOW_REPORTS: true
          COVERALLS: ${{ secrets.COVERALLS }}
          DISCORD_URL: ${{ secrets.DISCORD_URL }}
          EMAIL_HOST: ${{ secrets.EMAIL_HOST }}
          EMAIL_USER: ${{ secrets.EMAIL_USER }}
          EMAIL_PASS: ${{ secrets.EMAIL_PASS }}
          EMAIL_OUTGOING: ${{ secrets.EMAIL_OUTGOING }}
          EMAIL_SEND_TO: ${{ secrets.EMAIL_SEND_TO }}
          EMAIL_PORT: ${{ secrets.EMAIL_PORT }}
          MOBILE_ID: ${{ secrets.MOBILE_ID }}
          MOBILE_NUMBER: ${{ secrets.MOBILE_NUMBER }}
          PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_TOKEN }}
          PUSHOVER_API: ${{ secrets.PUSHOVER_API }}
          SLACK_URL: ${{ secrets.SLACK_URL }}
          TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
          TELEGRAM_CHANNEL: ${{ secrets.TELEGRAM_CHANNEL }}
          TWILIO_SID: ${{ secrets.TWILIO_SID }}
          TWILIO_SECRET: ${{ secrets.TWILIO_SECRET }}
          TWILIO_FROM: ${{ secrets.TWILIO_FROM }}
          TWILIO_TO: ${{ secrets.TWILIO_TO }}

      - name: Coveralls Testing Coverage
        run: |
          go get github.com/mattn/goveralls
          goveralls -coverprofile=coverage.out -repotoken $COVERALLS
        env:
          COVERALLS: ${{ secrets.COVERALLS }}

  test-postman-sqlite:
    needs: compile
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Install Statping
        env:
          VERSION: ${{ env.VERSION }}
        run: |
          make build
          chmod +x statping
          mv statping $(go env GOPATH)/bin/

      - name: Run Statping
        run: |
          API_SECRET=demosecret123 statping --port=8585 > /dev/null &
          sleep 3

      - name: Postman SQLite Tests
        uses: matt-ball/newman-action@master
        with:
          apiKey: ${{ secrets.POSTMAN_API }}
          collection: ./dev/postman.json
          environment: ./dev/postman_env_sqlite.json
          timeoutRequest: 15000
          delayRequest: 500

  test-postman-mysql:
    needs: compile
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: password123
          MYSQL_DATABASE: statping
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Install Statping
        env:
          VERSION: ${{ env.VERSION }}
        run: |
          make build
          chmod +x statping
          mv statping $(go env GOPATH)/bin/

      - name: Run Statping
        run: |
          API_SECRET=demosecret123 statping --port=8585 > /dev/null &
          sleep 3

      - name: Postman MySQL Tests
        uses: matt-ball/newman-action@master
        with:
          apiKey: ${{ secrets.POSTMAN_API }}
          collection: ./dev/postman.json
          environment: ./dev/postman_env_mysql.json
          timeoutRequest: 15000
          delayRequest: 500

  test-postman-postgres:
    needs: compile
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: password123
          POSTGRES_DB: statping
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Install Statping
        env:
          VERSION: ${{ env.VERSION }}
        run: |
          make build
          chmod +x statping
          mv statping $(go env GOPATH)/bin/

      - name: Run Statping
        run: |
          API_SECRET=demosecret123 statping --port=8585 > /dev/null &
          sleep 3

      - name: Postman Postgres Tests
        uses: matt-ball/newman-action@master
        with:
          apiKey: ${{ secrets.POSTMAN_API }}
          collection: ./dev/postman.json
          environment: ./dev/postman_env_postgres.json
          timeoutRequest: 15000
          delayRequest: 500

  build-linux:
    needs: compile
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'
      - uses: actions/checkout@v2

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Go mods
        run: |
          make test-deps

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Build Linux XGO
        uses: crazy-max/ghaction-xgo@v1
        with:
          xgo_version: latest
          go_version: 1.14.2
          dest: build
          prefix: statping
          pkg: cmd
          targets: linux/386,linux/amd64
          v: true
          x: false
          buildmode: default
          ldflags: -s -w -X main.VERSION=$VERSION

      - name: Build Linux ARM XGO
        uses: crazy-max/ghaction-xgo@v1
        with:
          xgo_version: latest
          go_version: 1.14.2
          dest: build
          prefix: statping
          pkg: cmd
          targets: linux/arm-7,linux/arm64
          v: true
          x: false
          buildmode: default
          ldflags: -s -w -extldflags -static -X main.VERSION=$VERSION

      - name: Upload Builds
        uses: actions/upload-artifact@v1
        with:
          name: builds-linux
          path: ./build

  build-windows:
    needs: compile
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'
      - uses: actions/checkout@v2

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Go mods
        run: |
          make test-deps

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Build Windows XGO
        uses: crazy-max/ghaction-xgo@v1
        with:
          xgo_version: latest
          go_version: 1.14.2
          dest: build
          prefix: statping
          pkg: cmd
          targets: windows-6.0/386,windows-6.0/amd64
          v: true
          x: false
          buildmode: default
          ldflags: -s -w -X main.VERSION=$VERSION

      - name: Upload Builds
        uses: actions/upload-artifact@v1
        with:
          name: builds-windows
          path: ./build

  build-mac:
    needs: compile
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.14.2'
      - uses: actions/checkout@v2

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Go mods
        run: |
          make test-deps

      - name: Download Compiled Frontend (rice-box.go)
        uses: actions/download-artifact@v1
        with:
          name: static-rice-box
          path: ./source

      - name: Build MacOSX XGO
        uses: crazy-max/ghaction-xgo@v1
        with:
          xgo_version: latest
          go_version: 1.14.2
          dest: build
          prefix: statping
          pkg: cmd
          targets: darwin/386,darwin/amd64
          v: true
          x: false
          buildmode: default
          ldflags: -s -w -X main.VERSION=$VERSION

      - name: Upload Builds
        uses: actions/upload-artifact@v1
        with:
          name: builds-mac
          path: ./build

  upload-release:
    needs: [test, test-postman-sqlite, test-postman-mysql, test-postman-postgres, build-linux, build-windows, build-mac]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Setting ENV's
        run: |
          echo "::add-path::$(go env GOPATH)/bin"
          echo "::add-path::/opt/hostedtoolcache/node/10.20.1/x64/bin"
          echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Download Linux Builds
        uses: actions/download-artifact@v1
        with:
          name: builds-linux
          path: ./builds

      - name: Download Windows Builds
        uses: actions/download-artifact@v1
        with:
          name: builds-windows
          path: ./builds

      - name: Download MacOSX Builds
        uses: actions/download-artifact@v1
        with:
          name: builds-mac
          path: ./builds

      - name: Upload Releases
        id: upload-assets
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ env.VERSION }}
        with:
          tag_name: v${{ env.VERSION }}
          draft: false
          prerelease: false
          files: |
            builds/statping-linux-386.tar.gz
            builds/statping-linux-amd64.tar.gz
            builds/statping-linux-arm.tar.gz
            builds/statping-linux-arm64.tar.gz
            builds/statping-darwin-amd64.tar.gz
            builds/statping-darwin-386.tar.gz
            builds/statping-windows-386.zip
            builds/statping-windows-amd64.zip
            builds/statping-windows-arm.zip

  docker-release:
    needs: upload-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Statping Repo
        uses: actions/checkout@v2

      - name: Setting ENV's
        run: echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Base Docker Image
        uses: elgohr/Publish-Docker-Github-Action@master
        with:
          name: statping/statping
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          dockerfile: Dockerfile.base
          tags: "base"

      - name: Latest/Version Docker Image
        uses: elgohr/Publish-Docker-Github-Action@master
        env:
          VERSION: ${{ env.VERSION }}
          ARCH: amd64
          DOCKER_CLI_EXPERIMENTAL: enabled
        with:
          name: statping/statping
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          dockerfile: Dockerfile
          tags: "latest,v${{ env.VERSION }}"
          buildargs: VERSION,ARCH

  sentry-release:
    needs: upload-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Statping Repo
        uses: actions/checkout@v2

      - name: Setting ENV's
        run: echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Sentry Backend Release
        uses: tclindner/sentry-releases-action@v1.0.0
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_URL: ${{ secrets.SENTRY_URL }}
          SENTRY_ORG: statping
          SENTRY_PROJECT: backend
        with:
          tagName: v${{ env.VERSION }}
          environment: production

      - name: Sentry Frontend Release
        uses: tclindner/sentry-releases-action@v1.0.0
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_URL: ${{ secrets.SENTRY_URL }}
          SENTRY_ORG: statping
          SENTRY_PROJECT: frontend
        with:
          tagName: v${{ env.VERSION }}
          environment: production

  homebrew-release:
    needs: upload-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Statping Repo
        uses: actions/checkout@v2

      - name: Setting ENV's
        run: echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Update Homebrew Package
        env:
          VERSION: ${{ env.VERSION }}
          TRAVIS_API: ${{ secrets.TRAVIS_API }}
        run: make publish-homebrew

  slack-update:
    needs: upload-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Statping Repo
        uses: actions/checkout@v2

      - name: Setting ENV's
        run: echo ::set-env name=VERSION::$(cat version.txt)
        shell: bash

      - name: Slack Notification
        uses: rtCamp/action-slack-notify@v2.0.0
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_URL }}
          SLACK_CHANNEL: dev
          SLACK_USERNAME: StatpingDev
