name: CI

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

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16

      - name: Install Dependencies
        run: |
          # Cue
          export CUE_VERSION="$(grep cue ./go.mod | cut -d' ' -f2)"
          export CUE_TARBALL="cue_${CUE_VERSION}_linux_amd64.tar.gz"
          echo "Installing cue version $CUE_VERSION"
          curl -L https://github.com/cue-lang/cue/releases/download/${CUE_VERSION}/${CUE_TARBALL} | sudo tar zxf - -C /usr/local/bin

      - name: Go Lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: v1.39
          skip-go-installation: true
          args: --timeout=3m

      - name: Lint
        run: |
          make shellcheck cuelint docslint

      - name: Markdown Lint
        uses: avto-dev/markdown-lint@v1
        with:
          config: ".markdownlint.yaml"
          args: ./docs README.md

  test:
    name: Test
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Check out
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16

      - name: Test
        run: |
          make test

  integration:
    name: Integration
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Check out
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16

      - name: Install Dependencies
        run: |
          # Cue
          export CUE_VERSION="$(grep cue ./go.mod | cut -d' ' -f2)"
          export CUE_TARBALL="cue_${CUE_VERSION}_linux_amd64.tar.gz"
          echo "Installing cue version $CUE_VERSION"
          curl -L https://github.com/cue-lang/cue/releases/download/${CUE_VERSION}/${CUE_TARBALL} | sudo tar zxf - -C /usr/local/bin

      - name: Install Dependencies
        run: |
          # SOPS
          sudo curl -L -o /usr/local/bin/sops https://github.com/mozilla/sops/releases/download/v3.7.1/sops-v3.7.1.linux
          sudo chmod +x /usr/local/bin/sops

      - name: Import Dagger private key
        env:
          DAGGER_AGE_KEY: ${{ secrets.DAGGER_AGE_KEY }}
        run: |
          mkdir -p ~/.config/dagger
          echo "$DAGGER_AGE_KEY" > ~/.config/dagger/keys.txt

      - name: Expose GitHub Runtime
        uses: crazy-max/ghaction-github-runtime@v1

      - name: Integration test
        # env:
        #   DAGGER_CACHE_TO: "type=gha,mode=max,scope=test-integration"
        #   DAGGER_CACHE_FROM: "type=gha,mode=max,scope=test-integration"
        run: |
          env
          make core-integration

  universe:
    name: Universe
    runs-on: ubuntu-latest
    timeout-minutes: 20
    services:
      localstack:
        image: localstack/localstack:0.12.16
        env:
          SERVICES: s3, ecr
          LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
        ports:
          - 4566:4566
          - 4571:4571
          - 4510:4510
        options: >-
          --health-cmd "curl -f http://localhost:4566/health"
          --health-start-period 5s
          --health-timeout 5s
          --health-interval 5s
          --health-retries 10

    steps:
      - name: Check out
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16

      - name: Setup Kind Kubernetes Cluster
        uses: helm/kind-action@v1.2.0

      - name: Import Dagger private key
        env:
          DAGGER_AGE_KEY: ${{ secrets.DAGGER_AGE_KEY }}
        run: |
          mkdir -p ~/.config/dagger
          echo "$DAGGER_AGE_KEY" > ~/.config/dagger/keys.txt

      - name: Provision Localstack AWS resources
        env:
          AWS_ACCESS_KEY_ID: test
          AWS_SECRET_ACCESS_KEY: test
          AWS_DEFAULT_REGION: us-east-2
        run: |
          aws --endpoint-url=http://localhost:4566 s3 mb s3://dagger-ci
          aws --endpoint-url=http://localhost:4566 ecr create-repository --repository-name dagger-ci

      - name: Expose GitHub Runtime
        uses: crazy-max/ghaction-github-runtime@v1

      - name: Universe Test
        # env:
        #   DAGGER_CACHE_TO: "type=gha,mode=max,scope=test-universe"
        #   DAGGER_CACHE_FROM: "type=gha,mode=max,scope=test-universe"
        run: |
          make universe-test

  website:
    name: Website
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Cypress run
        uses: cypress-io/github-action@v2
        env:
          REACT_APP_CLIENT_ID: 123
          REACT_APP_CLIENT_SECRET: 123
          REACT_APP_DAGGER_SITE_URI: https://dagger.io
          REACT_APP_API_PROXY_ENABLE: false
          REACT_APP_AMPLITUDE_ID: 123
        with:
          config: chromeWebSecurity=false
          start: |
            yarn start
            yarn start:withAuth
          working-directory: ./website
      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: cypress-screenshots
          path: website/cypress/screenshots
      # Test run video was always captured, so this action uses "always()" condition
      - uses: actions/upload-artifact@v1
        if: always()
        with:
          name: cypress-videos
          path: website/cypress/videos
  doc:
    name: Documentation
    runs-on: ubuntu-latest
    timeout-minutes: 30
    services:
      localstack:
        image: localstack/localstack:0.12.16
        env:
          SERVICES: s3, cloudformation
          LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
        ports:
          - 4566:4566
          - 4571:4571
        options: >-
          --health-cmd "curl -f http://localhost:4566/health"
          --health-start-period 5s
          --health-timeout 5s
          --health-interval 5s
          --health-retries 10

    steps:
      - name: Check out
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16

      - name: Install Dependencies
        run: |
          # Cue
          export CUE_VERSION="$(grep cue ./go.mod | cut -d' ' -f2)"
          export CUE_TARBALL="cue_${CUE_VERSION}_linux_amd64.tar.gz"
          echo "Installing cue version $CUE_VERSION"
          curl -L https://github.com/cue-lang/cue/releases/download/${CUE_VERSION}/${CUE_TARBALL} | sudo tar zxf - -C /usr/local/bin

      - name: Run local registry
        run: |
          docker run -d -p 5000:5000 --name registry registry:2

      - name: Write kind echo
        run: |
          echo 'kind: Cluster
          apiVersion: kind.x-k8s.io/v1alpha4
          containerdConfigPatches:
          - |-
            [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
              endpoint = ["http://registry:5000"]' > ./kind-config.yaml
          cat ./kind-config.yaml

      - name: Setup Kind Kubernetes Cluster
        uses: helm/kind-action@v1.2.0
        with:
          config: "./kind-config.yaml"

      - name: Connect registry to cluster
        run: |
          docker network connect kind registry

      - name: Import Dagger private key
        env:
          DAGGER_AGE_KEY: ${{ secrets.DAGGER_AGE_KEY }}
        run: |
          mkdir -p ~/.config/dagger
          echo "$DAGGER_AGE_KEY" > ~/.config/dagger/keys.txt

      - name: Expose GitHub Runtime
        uses: crazy-max/ghaction-github-runtime@v1

      - name: Documentation Test
        # env:
        #   DAGGER_CACHE_TO: "type=gha,mode=max,scope=test-docs"
        #   DAGGER_CACHE_FROM: "type=gha,mode=max,scope=test-docs"
        run: |
          make doc-test
