name: CI

on:
  push:
    branches:
      - master
  pull_request:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    env:
      GOFLAGS: -mod=readonly

    services:
      postgres:
        image: postgres:10.8
        ports:
          - 5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: dex
        ports:
          - 3306
        options: --health-cmd "mysql -proot -e \"show databases;\"" --health-interval 10s --health-timeout 5s --health-retries 5

      etcd:
        image: gcr.io/etcd-development/etcd:v3.5.0
        ports:
          - 2379
        env:
          ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
          ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
        options: --health-cmd "ETCDCTL_API=3 etcdctl --endpoints http://localhost:2379 endpoint health" --health-interval 10s --health-timeout 5s --health-retries 5

      keystone:
        image: openio/openstack-keystone:rocky
        ports:
          - 5000
          - 35357
        options: --health-cmd "curl --fail http://localhost:5000/v3" --health-interval 10s --health-timeout 5s --health-retries 5

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

      - name: Checkout code
        uses: actions/checkout@v2

      - name: Start services
        run: docker-compose -f docker-compose.test.yaml up -d

      - name: Create kind cluster
        uses: helm/kind-action@v1.1.0
        with:
          version: v0.11.1
          node_image: kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729

      - name: Test
        run: make testall
        env:
          DEX_MYSQL_DATABASE: dex
          DEX_MYSQL_USER: root
          DEX_MYSQL_PASSWORD: root
          DEX_MYSQL_HOST: 127.0.0.1
          DEX_MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
          DEX_POSTGRES_DATABASE: postgres
          DEX_POSTGRES_USER: postgres
          DEX_POSTGRES_PASSWORD: postgres
          DEX_POSTGRES_HOST: localhost
          DEX_POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
          DEX_ETCD_ENDPOINTS: http://localhost:${{ job.services.etcd.ports[2379] }}
          DEX_LDAP_HOST: localhost
          DEX_LDAP_PORT: 389
          DEX_LDAP_TLS_PORT: 636
          DEX_KEYSTONE_URL: http://localhost:${{ job.services.keystone.ports[5000] }}
          DEX_KEYSTONE_ADMIN_URL: http://localhost:${{ job.services.keystone.ports[35357] }}
          DEX_KEYSTONE_ADMIN_USER: demo
          DEX_KEYSTONE_ADMIN_PASS: DEMO_PASS
          DEX_KUBERNETES_CONFIG_PATH: ~/.kube/config

      - name: Lint
        run: make lint

      # Ensure proto generation doesn't depend on external packages.
      - name: Verify proto
        run: make verify-proto