name: CI
env:
  # Increment this to bust the build cache
  CACHE_VERSION: 1
  GO_VERSION: 1.17.x
on:
  push:
  workflow_dispatch:

jobs:
  test_build:
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/master' || github.event_name == 'workflow_dispatch' }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - uses: actions/cache@v2
        with:
          # In order:
          # * Module download cache
          # * Build cache (Linux)
          # * Build cache (Mac)
          # * Build cache (Windows)
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
            ~/Library/Caches/go-build
            %LocalAppData%\go-build
          key: ${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.go-version.outputs.version }}-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ env.CACHE_VERSION }}-${{ runner.os }}-go
      - name: "Place wintun.dll"
        run: cp deps/wintun/bin/amd64/wintun.dll ./
      - name: build
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: build --rm-dist --snapshot
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Get go version
        id: go-version
        run: echo "::set-output name=version::$(go env GOVERSION)"
      - uses: actions/cache@v2
        with:
          # In order:
          # * Module download cache
          # * Build cache (Linux)
          # * Build cache (Mac)
          # * Build cache (Windows)
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
            ~/Library/Caches/go-build
            %LocalAppData%\go-build
          key: ${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.go-version.outputs.version }}-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ env.CACHE_VERSION }}-${{ runner.os }}-go
      - name: go mod download
        run: go mod download
      - name: go mod verify
        run: go mod verify
      - name: generate command strings
        run: go generate ./...
      - name: "Place wintun.dll"
        run: cp deps/wintun/bin/amd64/wintun.dll ./
      - name: Run tests
        run: make test

  release_homebrew:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, 'dev-') && !contains(github.ref, 'pre')
    runs-on: ubuntu-latest
    steps:
      - name: Bump homebrew formula
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: Homebrew Bump Formula
          token: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
          inputs: "{ \"version\": \"${{ github.ref_name }}\", \"sha\": \"${{ github.sha }}\" }"

  release:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, 'dev-')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Get go version
        id: go-version
        run: echo "::set-output name=version::$(go env GOVERSION)"
      - name: Docker Login
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
        run: |
          echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin
      - uses: actions/cache@v2
        with:
          # In order:
          # * Module download cache
          # * Build cache (Linux)
          # * Build cache (Mac)
          # * Build cache (Windows)
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
            ~/Library/Caches/go-build
            %LocalAppData%\go-build
          key: ${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.go-version.outputs.version }}-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ env.CACHE_VERSION }}-${{ runner.os }}-go
      - name: Place wintun.dll
        run: cp -r deps/wintun/bin/amd64/wintun.dll ./
      - name: generate release notes
        run: |
          mkdir -p ./tmp
          ./scripts/changelog.sh > ./tmp/changelog.txt
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist --release-notes=./tmp/changelog.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
      - name: Upload checksums as artifact
        uses: actions/upload-artifact@v2
        with:
          name: checksums
          path: dist/checksums.txt
  sync_docs:
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/master' }}
    steps:
      - name: Checkout flyctl
        uses: actions/checkout@v2
      - name: Checkout docs
        uses: actions/checkout@v2
        with:
          repository: superfly/docs
          token: ${{ secrets.DOCS_GITHUB_TOKEN }}
          path: docs
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Publish CLI docs
        id: publish-cli-docs
        env:
          GITHUB_TOKEN: ${{ secrets.DOCS_GITHUB_TOKEN }}
        run: scripts/publish_docs.sh ${{ github.ref_name }}
  dev_release:
    if: startsWith(github.ref, 'refs/tags/v') && contains(github.ref, 'dev-')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Get go version
        id: go-version
        run: echo "::set-output name=version::$(go env GOVERSION)"
      - uses: actions/cache@v2
        with:
          # In order:
          # * Module download cache
          # * Build cache (Linux)
          # * Build cache (Mac)
          # * Build cache (Windows)
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
            ~/Library/Caches/go-build
            %LocalAppData%\go-build
          key: ${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.go-version.outputs.version }}-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ env.CACHE_VERSION }}-${{ runner.os }}-go
      - name: Place wintun.dll
        run: cp -r deps/wintun/bin/amd64/wintun.dll ./
      - name: generate release notes
        run: |
          mkdir -p ./tmp
          ./scripts/changelog.sh > ./tmp/changelog.txt
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --config .goreleaser.dev.yml --rm-dist --release-notes=./tmp/changelog.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
      - name: Upload checksums as artifact
        uses: actions/upload-artifact@v2
        with:
          name: checksums
          path: dist/checksums.txt
  aur-publish:
    name: Build & publish to AUR
    needs: release
    if: ${{ !contains(github.ref, 'pre') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Download checksums
        uses: actions/download-artifact@v2.0.8
        with:
          name: checksums
      - name: Generate PKGBUILD
        run: |
          mkdir -p aur/flyctl-bin
          export version=${GITHUB_REF#refs/*/}
          export pkgver=${version:1}
          export sha256sum=$(grep "Linux_x86_64.tar.gz" checksums.txt | cut -d ' ' -f 1)
          envsubst '${pkgver},${sha256sum}' < aur/PKGBUILD > aur/flyctl-bin/PKGBUILD
          cat aur/flyctl-bin/PKGBUILD
      - name: Publish flyctl-bin to the AUR
        uses: superfly/aur-releaser@ba29a0a809d7221713a104f9c9a23c34ee5b0742
        with:
          pkgname: flyctl-bin
          pkgbuild: ./aur/flyctl-bin/PKGBUILD
          commit_username: jeromegn
          commit_email: jeromegn@gmail.com
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: ${{ github.event.ref }}
          force_push: "true"
