name: Release

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

# grant write permission to GITHUB_TOKEN for goreleaser
permissions:
  contents: write

jobs:
  build-linux-amd64:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-multilib g++-multilib

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.3'
          cache: true

      - uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: release --skip=publish --config .goreleaser-linux.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v3
        with:
          name: release-builds
          path: ./dist/*.gz

  build-linux-arm64:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-aarch64-linux-gnu

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.3'
          cache: true

      - uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: release --skip=publish --config .goreleaser-linux-arm64.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v3
        with:
          name: release-builds
          path: ./dist/*.gz

  build-linux-arm:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-arm-linux-gnueabi

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.3'
          cache: true

      - uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: release --skip=publish --config .goreleaser-linux-armhf.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v3
        with:
          name: release-builds
          path: ./dist/*.gz

  build-darwin:
    runs-on: macos-latest
    steps:
      - name: import distribution cert
        uses: apple-actions/import-codesign-certs@v1
        with:
          p12-file-base64: ${{ secrets.APPLE_DEVELOPER_CERTIFICATE_P12_BASE64 }}
          p12-password: ${{ secrets.APPLE_DEVELOPER_CERTIFICATE_PASSWORD }}

      - run: |
          brew tap mitchellh/gon
          brew install mitchellh/gon/gon        

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.3'
          cache: true

      - uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: release --skip=publish --config .goreleaser-darwin.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          AC_USERNAME: ${{ secrets.AC_USERNAME }}
          AC_PASSWORD: ${{ secrets.AC_PASSWORD }}

      - uses: actions/upload-artifact@v3
        with:
          name: release-builds
          path: ./dist/*.gz

  build-windows:
    runs-on: ubuntu-latest
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-mingw-w64-x86-64

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.3'
          cache: true

      - uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          version: latest
          args: release --skip=publish --config .goreleaser-windows.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v3
        with:
          name: release-builds
          path: ./dist/*.gz

  publish-release:
    needs: [build-linux-amd64, build-linux-arm, build-linux-arm64, build-darwin, build-windows]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - run: |
          mkdir -p ./automated-release-build

      - uses: actions/download-artifact@v3
        with:
          path: ./automated-release-build

      - run: |
          mv ./automated-release-build/release-builds/* ./automated-release-build/
          rm -rf ./automated-release-build/release-builds
          shasum ./automated-release-build/* > ./automated-release-build/checksums.txt

      - uses: goreleaser/goreleaser-action@v3
        with:
          args: release --config .goreleaser-release.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
