name: Go

on:
  push:
    branches:
      - master
      - release-*
  workflow_dispatch: {}
  pull_request:
    branches:
      - master
      - release-*

permissions:  # added using https://github.com/step-security/secure-workflows
  contents: read

env:
  # Common versions
  GO_VERSION: '1.19'
  GOLANGCI_VERSION: 'v1.49'

jobs:

  detect-noop:
    runs-on: ubuntu-22.04
    outputs:
      noop: ${{ steps.noop.outputs.should_skip }}
    permissions:
      actions: write
    steps:
      - name: Detect No-op Changes
        id: noop
        uses: fkirc/skip-duplicate-actions@12aca0a884f6137d619d6a8a09fcc3406ced5281
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          paths_ignore: '["**.md", "**.mdx", "**.png", "**.jpg"]'
          do_not_skip: '["workflow_dispatch", "schedule", "push"]'
        continue-on-error: true

  staticcheck:
    runs-on: ubuntu-22.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'

    steps:
      - name: Setup Go
        uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true

      - name: Static Check
        run: make staticcheck

      - name: License Header Check
        run: make check-license-header

  lint:
    runs-on: ubuntu-22.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'
    permissions:
      contents: read  # for actions/checkout to fetch code
      pull-requests: read  # for golangci/golangci-lint-action to fetch pull requests

    steps:
      - name: Setup Go
        uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true

      # This action uses its own setup-go, which always seems to use the latest
      # stable version of Go. We could run 'make lint' to ensure our desired Go
      # version, but we prefer this action because it leaves 'annotations' (i.e.
      # it comments on PRs to point out linter violations).
      - name: Lint
        uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0
        with:
          version: ${{ env.GOLANGCI_VERSION }}

  check-diff:
    runs-on: ubuntu-22.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'

    steps:
      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true

      - name: Setup Go
        uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Setup node
        uses: actions/setup-node@e33196f7422957bea03ed53f6fbb155025ffc7b8
        with:
          node-version: '14'

      - name: Cache Go Dependencies
        uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
        with:
          path: .work/pkg
          key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-pkg-

      - name: Setup KinD
        run: |
          go install sigs.k8s.io/kind@v0.19.0
          kind delete cluster --name kind || true
          kind create cluster --name kind --image=kindest/node:v1.26.4 --kubeconfig ~/.kube/config

      - name: Run cross-build
        run: make cross-build

      - name: Check Diff
        run: |
          export PATH=$(pwd)/bin/:$PATH
          make check-diff
              
      - name: Cleanup binary
        run: make build-cleanup

  check-windows:
    runs-on: windows-latest
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'

    steps:
      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true

      - name: Setup Go
        uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Cache Go Dependencies
        uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
        with:
          path: .work/pkg
          key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-pkg-

      - name: Run Build CLI
        run: make vela-cli

      - name: Run CLI for version
        shell: cmd
        run: |
          move .\bin\vela .\bin\vela.exe
          .\bin\vela.exe version

  check-core-image-build:
    runs-on: ubuntu-22.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'
    steps:
      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true
      - name: Set up QEMU
        uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # v2.2.0
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@2a1a44ac4aa01993040736bd95bb470da1a38365 # v2.9.0
      - name: Build Test for vela core
        uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 # v4.1.1
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64,linux/arm64

  check-cli-image-build:
    runs-on: ubuntu-22.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'
    steps:
      - name: Checkout
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
        with:
          submodules: true
      - name: Set up QEMU
        uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # v2.2.0
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@2a1a44ac4aa01993040736bd95bb470da1a38365 # v2.9.0
      - name: Build Test for CLI
        uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 # v4.1.1
        with:
          context: .
          file: Dockerfile.cli
