name: Build, Test and Deploy

on:
  push:

env:
  GO_VERSION: 1.19
  BEEPER_BRIDGE_TYPE: groupme
  CI_REGISTRY_IMAGE: "${{ secrets.CI_REGISTRY }}/groupme"

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Go ${{ env.GO_VERSION }}
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: true

      - name: Install dependencies
        run: |
          go install golang.org/x/tools/cmd/goimports@latest
          go install honnef.co/go/tools/cmd/staticcheck@latest
          export PATH="$HOME/go/bin:$PATH"

      - name: Run pre-commit
        uses: pre-commit/action@v3.0.0

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: true

      - name: Install Dependencies
        run: |
          go install -v github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest

      - name: Run tests
        run: |
          go test -v -json ./... -cover | gotestfmt

  build-docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to registry
        uses: docker/login-action@v2
        with:
          registry: ${{ secrets.CI_REGISTRY }}
          username: ${{ secrets.CI_REGISTRY_USER }}
          password: ${{ secrets.CI_REGISTRY_PASSWORD }}

      - name: Docker Build
        uses: docker/build-push-action@v2
        with:
          cache-from: ${{ env.CI_REGISTRY_IMAGE }}:latest
          pull: true
          file: Dockerfile
          tags: ${{ env.CI_REGISTRY_IMAGE }}:${{ github.sha }}
          push: true
          build-args: |
            COMMIT_HASH=${{ github.sha }}

  deploy-docker:
    runs-on: ubuntu-latest
    needs:
      - lint
      - test
      - build-docker
    if: github.ref == 'refs/heads/master'
    steps:
      - name: Login to registry
        uses: docker/login-action@v2
        with:
          registry: ${{ secrets.CI_REGISTRY }}
          username: ${{ secrets.CI_REGISTRY_USER }}
          password: ${{ secrets.CI_REGISTRY_PASSWORD }}

      - uses: beeper/docker-retag-push-latest@main
        with:
          image: ${{ env.CI_REGISTRY_IMAGE }}

      - name: Run bridge CD tool
        uses: beeper/bridge-cd-tool@main
        env:
          CI_REGISTRY: "${{ secrets.CI_REGISTRY }}"
          BEEPER_DEV_ADMIN_API_URL: "${{ secrets.BEEPER_DEV_ADMIN_API_URL }}"
          BEEPER_STAGING_ADMIN_API_URL: "${{ secrets.BEEPER_STAGING_ADMIN_API_URL }}"
          BEEPER_PROD_ADMIN_API_URL: "${{ secrets.BEEPER_PROD_ADMIN_API_URL }}"
          BEEPER_DEV_ADMIN_NIGHTLY_PASS: "${{ secrets.BEEPER_DEV_ADMIN_NIGHTLY_PASS }}"
          BEEPER_STAGING_ADMIN_NIGHTLY_PASS: "${{ secrets.BEEPER_STAGING_ADMIN_NIGHTLY_PASS }}"
          BEEPER_PROD_ADMIN_NIGHTLY_PASS: "${{ secrets.BEEPER_PROD_ADMIN_NIGHTLY_PASS }}"