name: Go

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  test:
    runs-on: [ ubuntu-latest ]
    if: github.ref=='refs/heads/master' || github.ref=='refs/heads/main' || starts-with(github.ref, 'refs/tags/')
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v1
      - name: Test
        run: |
          go test -v ./...
        
  build:
    name: Build
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
        - os: ubuntu-latest
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
        - os: macos-latest
          path: |
            ~/Library/Caches/go-build
            ~/go/pkg/mod
        - os: windows-latest
          path: |
            %LocalAppData%\go-build
            ~/go/pkg/mod
    runs-on: ${{matrix.os}}
    steps:
      - name: Set up Go 1.13
        uses: actions/setup-go@v1
        with:
          go-version: 1.13
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: cache
        id: cache
        uses: actions/cache@v2
        with:
          path: ${{ matrix.path }}
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Get dependencies
        run: |
          go get -v -t -d ./...
          if [ -f Gopkg.toml ]; then
              curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
              dep ensure
          fi
        working-directory: ./server

      - name: Build
        run: go build -v . -o output
        working-directory: ./server

      - name: artifact
        uses: actions/upload-artifact@v1
        with:
          name: artifact
          path: output

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v1
        with:
          version: latest
          args: release --rm-dist
          key: ${{ secrets.YOUR_PRIVATE_KEY }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}