name: release

on:
  workflow_dispatch:
  push:
    tags:
      - v*.*.*

jobs:
  linux-release:
    name: Release assets for Linux
    strategy:
      matrix:
        go_version: [1.18]
    runs-on: ubuntu-latest
    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Login to ghcr.io
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Go ${{ matrix.go_version }} on Ubuntu
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go_version }}

      - name: Install packages
        run: |
          sudo apt-get update
          sudo apt-get install sqlite3

      - name: Install aws-cli
        uses: isbang/setup-awscli@v0.1.0

      - name: Check out source code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Start databases
        run: |
          docker compose up -d
          sleep 20s

      - name: Run GoReleaser (dry-run)
        uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: --config .goreleaser/linux.yml --rm-dist --snapshot --skip-publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run integration test
        run: env TBLS=dist/tbls-linux_linux_amd64_v1/tbls make depsdev db doc

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: --config .goreleaser/linux.yml --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  windows-release:
    name: Release assets for Windows
    strategy:
      matrix:
        go_version: [1.18]
    runs-on: windows-2019
    steps:
      - name: Set up Go ${{ matrix.go_version }} on Windows
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go_version }}

      - name: Install packages
        run: |
          choco install sqlite

      - name: Install aws-cli
        uses: isbang/setup-awscli@v0.1.0

      - name: Use LF
        run: |
          git config --global core.autocrlf false
          git config --global core.eol lf

      - name: Check out source code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Run tests
        run: env TBLS=tbls.exe make ci_windows
        shell: bash

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: --config .goreleaser/windows.yml --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
