name: Create Release & Upload Assets

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
    - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  test:
    name: Lint, Test, Build
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        go: [ '1.18', '1.19' ]
    outputs:
      is_prerelease: ${{ steps.is_prerelease.outputs.IS_PRERELEASE }}
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
      -
        name: Install Deps
        id: install-deps
        run: sudo apt-get -y install libpcsclite-dev
      -
        name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: ${{ secrets.GOLANGCI_LINT_VERSION }}
          args: --timeout=30m
      -
        name: Test, Build
        id: lint_test_build
        run: V=1 make ci

  create_release:
    name: Create Release
    needs: test
    runs-on: ubuntu-20.04
    outputs:
      debversion: ${{ steps.extract-tag.outputs.DEB_VERSION }}
      is_prerelease: ${{ steps.is_prerelease.outputs.IS_PRERELEASE }}
    steps:
      -
        name: Extract Tag Names
        id: extract-tag
        run: |
          DEB_VERSION=$(echo ${GITHUB_REF#refs/tags/v} | sed 's/-/./')
          echo "::set-output name=DEB_VERSION::${DEB_VERSION}"
      -
        name: Is Pre-release
        id: is_prerelease
        run: |
          set +e
          echo ${{ github.ref }} | grep "\-rc.*"
          OUT=$?
          if [ $OUT -eq 0 ]; then IS_PRERELEASE=true; else IS_PRERELEASE=false; fi
          echo "::set-output name=IS_PRERELEASE::${IS_PRERELEASE}"
      -
        name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: ${{ steps.is_prerelease.outputs.IS_PRERELEASE }}

  goreleaser:
    name: Upload Assets To Github w/ goreleaser
    runs-on: ubuntu-20.04
    needs: create_release
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      -
        name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.19
      -
        name: APT Install
        id: aptInstall
        run: sudo apt-get -y install build-essential debhelper fakeroot
      -
        name: Build Debian package
        id: make_debian
        run: |
          PATH=$PATH:/usr/local/go/bin:/home/admin/go/bin
          make debian
          # need to restore the git state otherwise goreleaser fails due to dirty state
          git restore debian/changelog
          git clean -fd
      -
        name: Install cosign
        uses: sigstore/cosign-installer@v1.1.0
        with:
          cosign-release: 'v1.1.0'
      -
        name: Write cosign key to disk
        id: write_key
        run: echo "${{ secrets.COSIGN_KEY }}" > "/tmp/cosign.key"
      -
        name: Get Release Date
        id: release_date
        run: |
          RELEASE_DATE=$(date +"%y-%m-%d")
          echo "::set-output name=RELEASE_DATE::${RELEASE_DATE}"
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@5a54d7e660bda43b405e8463261b3d25631ffe86 # v2.7.0
        with:
          version: 'v1.7.0'
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
          COSIGN_PWD: ${{ secrets.COSIGN_PWD }}
          DEB_VERSION: ${{ needs.create_release.outputs.debversion }}
          RELEASE_DATE: ${{ steps.release_date.outputs.RELEASE_DATE }}

  build_upload_docker:
    name: Build & Upload Docker Images
    runs-on: ubuntu-20.04
    needs: test
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: '1.19'
      -
        name: Install cosign
        uses: sigstore/cosign-installer@v1.1.0
        with:
          cosign-release: 'v1.1.0'
      -
        name: Write cosign key to disk
        id: write_key
        run: echo "${{ secrets.COSIGN_KEY }}" > "/tmp/cosign.key"
      -
        name: Build
        id: build
        run: |
          PATH=$PATH:/usr/local/go/bin:/home/admin/go/bin
          make docker-artifacts
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
          COSIGN_PWD: ${{ secrets.COSIGN_PWD }}