---
name: CI

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  build:
    name: "Build & Test"
    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"

      - name: Run unit tests
        run: make test

      - name: Verify installation
        run: |
          mkdir -p helmhome
          make install HELM_HOME=helmhome
          helmhome/plugins/helm-diff/bin/diff version

  helm-install:
    name: helm install
    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    needs: [build]
    runs-on: ${{ matrix.os }}
    container: ${{ matrix.container }}
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        shell: [default]
        experimental: [false]
        helm-version: [v3.18.6, v3.21.4, v4.2.4]
        include:
          - os: windows-latest
            shell: wsl
            experimental: false
            helm-version: v3.18.6
          - os: windows-latest
            shell: cygwin
            experimental: false
            helm-version: v3.18.6
          - os: ubuntu-latest
            container: alpine
            shell: sh
            experimental: false
            helm-version: v3.18.6
          - os: windows-latest
            shell: wsl
            experimental: false
            helm-version: v3.21.4
          - os: windows-latest
            shell: cygwin
            experimental: false
            helm-version: v3.21.4
          - os: ubuntu-latest
            container: alpine
            shell: sh
            experimental: false
            helm-version: v3.21.4
          - os: windows-latest
            shell: wsl
            experimental: false
            helm-version: v4.2.4
          - os: windows-latest
            shell: cygwin
            experimental: false
            helm-version: v4.2.4
          - os: ubuntu-latest
            container: alpine
            shell: sh
            experimental: false
            helm-version: v4.2.4

    steps:
      - name: Disable autocrlf
        if: "contains(matrix.os, 'windows-latest')"
        run: |-
          git config --global core.autocrlf false
          git config --global core.eol lf

      - uses: actions/checkout@v7

      - name: Setup Helm
        uses: azure/setup-helm@v5
        with:
          version: "${{ matrix.helm-version }}"

      - name: Setup WSL
        if: "contains(matrix.shell, 'wsl')"
        uses: Vampire/setup-wsl@v7

      - name: Setup Cygwin
        if: "contains(matrix.shell, 'cygwin')"
        uses: egor-tensin/setup-cygwin@v4

      - name: helm plugin install
        run: helm plugin install --debug .

  integration-tests:
    name: Integration Tests
    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    needs: [build]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          # Helm maintains the latest minor version only and therefore each Helmfile version supports 2 Helm minor versions.
          # That's why we cover only 2 Helm minor versions in this matrix.
          # See https://github.com/helmfile/helmfile/pull/286#issuecomment-1250161182 for more context.
          - helm-version: v3.18.6
          - helm-version: v3.21.4
          - helm-version: v4.2.4
    steps:
      - uses: helm/kind-action@v1.14.0
        with:
          cluster_name: kind

      - uses: actions/checkout@v7

      - name: Setup Helm
        uses: azure/setup-helm@v5
        with:
          version: ${{ matrix.helm-version }}

      - name: Helm Version
        run: helm version

      - name: helm plugin install
        run: make install/helm

      - name: helm create helm-diff
        run: helm create helm-diff

      - name: helm diff upgrade --install helm-diff ./helm-diff
        run: helm diff upgrade --install helm-diff ./helm-diff

      - name: helm upgrade -i helm-diff ./helm-diff
        run: helm upgrade -i helm-diff ./helm-diff

      - name: helm diff upgrade -C 3 --set replicaCount=2 --install helm-diff ./helm-diff
        run: helm diff upgrade -C 3 --set replicaCount=2 --install helm-diff ./helm-diff