name: Go

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  tests:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Cache libraries
      uses: actions/cache@v2
      env:
        cache-name: cache-go
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18
    - name: Env
      run: go env
    - name: Build
      run: make build build-test-tools
    - name: Test
      run: make check pristine test

    - name: Prepare tar to upload built binaries
      run: tar -cvf built-binaries.tar helmfile diff-yamls yamldiff
    - name: Upload built binaries
      uses: actions/upload-artifact@v2
      with:
        name: built-binaries-${{ github.run_id }}
        path: built-binaries.tar
        retention-days: 1
    - name: Display built binaries
      run: ls -l helmfile diff-yamls yamldiff

  integration_tests:
    needs: tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
       include:
         - helm-version: v3.4.2
         - helm-version: v3.5.4
         - helm-version: v3.6.3
         - helm-version: v3.7.2
         - helm-version: v3.8.2
         - helm-version: v3.9.0
    steps:
    - uses: actions/checkout@v2
    - name: Cache libraries
      uses: actions/cache@v2
      env:
        cache-name: cache-go
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18
    - name: Env
      run: go env

    - name: Download built binaries
      uses: actions/download-artifact@v2
      with:
        name: built-binaries-${{ github.run_id }}
    - name: Extract tar to get built binaries
      run: tar -xvf built-binaries.tar
    - name: Display built binaries
      run: ls -l helmfile diff-yamls yamldiff

    - name: Install test dependencies
      env:
        HELM_VERSION: ${{ matrix.helm-version }}
      run: make -C .github/workflows helm vault sops kustomize
    - name: Start minikube
      uses: medyagh/setup-minikube@master
    - name: Execute integration tests
      env:
        HELMFILE_HELM3: 1
        TERM: xterm
      run: make integration
