name: e2e

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  kind:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Restore Go cache
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Setup Go
        uses: actions/setup-go@v2-beta
        with:
          go-version: 1.14.x
      - name: Setup Kubernetes
        uses: engineerd/setup-kind@v0.3.0
      - name: Run test
        run: make test
      - name: Check if working tree is dirty
        run: |
          if [[ $(git diff --stat) != '' ]]; then
            git diff --stat
            echo 'run make test and commit changes'
            exit 1
          fi
      - name: Build
        run: sudo go build -o ./bin/tk ./cmd/tk
      - name: tk check --pre
        run: |
          ./bin/tk check --pre
      - name: tk install --version
        run: |
          ./bin/tk install --version=master --namespace=test --verbose
      - name: tk uninstall
        run: |
          ./bin/tk uninstall --namespace=test --crds --silent
      - name: tk install --manifests
        run: |
          ./bin/tk install --manifests ./manifests/install/ --version=""
      - name: tk create source git
        run: |
          ./bin/tk create source git podinfo \
            --url https://github.com/stefanprodan/podinfo  \
            --tag-semver=">=3.2.3"
      - name: tk get sources git
        run: |
          ./bin/tk get sources git
      - name: tk create kustomization
        run: |
          ./bin/tk create kustomization podinfo \
            --source=podinfo \
            --path="./deploy/overlays/dev" \
            --prune=true \
            --interval=5m \
            --validate=client \
            --health-check="Deployment/frontend.dev" \
            --health-check="Deployment/backend.dev" \
            --health-check-timeout=3m
      - name: tk sync kustomization --with-source
        run: |
          ./bin/tk sync kustomization podinfo --with-source
      - name: tk get kustomizations
        run: |
          ./bin/tk get kustomizations
      - name: tk suspend kustomization
        run: |
          ./bin/tk suspend kustomization podinfo
      - name: tk resume kustomization
        run: |
          ./bin/tk resume kustomization podinfo
      - name: tk export
        run: |
          ./bin/tk export source git --all
          ./bin/tk export kustomization --all
      - name: tk delete kustomization
        run: |
          ./bin/tk delete kustomization podinfo --silent
      - name: tk delete source git
        run: |
          ./bin/tk delete source git podinfo --silent
      - name: tk check
        run: |
          ./bin/tk check
      - name: Debug failure
        if: failure()
        run: |
          kubectl version --client --short
          kubectl -n gitops-system get all
          kubectl -n gitops-system get kustomizations -oyaml
          kubectl -n gitops-system logs deploy/source-controller
          kubectl -n gitops-system logs deploy/kustomize-controller