name: Deploy to dev
on:
  push:
    branches:
      - dev
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v0
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}
      - name: Install kubectl
        uses: azure/setup-kubectl@v2.0
        with:
          version: "v1.19.15"
      - name: Log in to gcloud CLI
        run: gcloud auth configure-docker
      - name: Checkout
        uses: actions/checkout@v2.3.4
      - name: Write Dashboard Environment Variables
        run: |
          cat >./dashboard/.env <<EOL
          NODE_ENV=development
          API_SERVER=dashboard.dev.getporter.dev
          DISCORD_KEY=${{secrets.DISCORD_KEY}}
          DISCORD_CID=${{secrets.DISCORD_CID}}
          FEEDBACK_ENDPOINT=${{secrets.FEEDBACK_ENDPOINT}}
          APPLICATION_CHART_REPO_URL=https://charts.dev.getporter.dev
          ADDON_CHART_REPO_URL=https://chart-addons.dev.getporter.dev
          ENABLE_SENTRY=true
          SENTRY_DSN=${{secrets.SENTRY_DSN}}
          SENTRY_ENV=frontend-development
          EOL
      - name: Build
        run: |
          DOCKER_BUILDKIT=1 docker build . -t gcr.io/porter-dev-273614/porter:dev -f ./ee/docker/ee.Dockerfile
      - name: Push
        run: |
          docker push gcr.io/porter-dev-273614/porter:dev
      - name: Deploy to cluster
        run: |
          aws eks --region ${{ secrets.AWS_REGION }} update-kubeconfig --name dev

          kubectl rollout restart deployment/porter
  deploy-provisioner:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v0
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}
      - name: Install kubectl
        uses: azure/setup-kubectl@v2.0
        with:
          version: "v1.19.15"
      - name: Log in to gcloud CLI
        run: gcloud auth configure-docker
      - name: Checkout
        uses: actions/checkout@v2.3.4
      - name: Build
        run: |
          DOCKER_BUILDKIT=1 docker build . -t gcr.io/porter-dev-273614/provisioner-service:dev -f ./ee/docker/provisioner.Dockerfile
      - name: Push
        run: |
          docker push gcr.io/porter-dev-273614/provisioner-service:dev
      - name: Deploy to cluster
        run: |
          aws eks --region ${{ secrets.AWS_REGION }} update-kubeconfig --name dev

          kubectl rollout restart deployment/provisioner
  build-push-ecr-server:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2.3.4
      - name: Set Github tag
        id: vars
        run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.ECR_DEV_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.ECR_DEV_AWS_ACCESS_SECRET_KEY }}
          aws-region: us-east-2
      - name: Login to ECR
        id: login-ecr
        run: |
          aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 801172602658.dkr.ecr.us-east-2.amazonaws.com
      - name: Write Dashboard Environment Variables
        run: |
          cat >./dashboard/.env <<EOL
          NODE_ENV=development
          API_SERVER=dashboard.dev.getporter.dev
          DISCORD_KEY=${{secrets.DISCORD_KEY}}
          DISCORD_CID=${{secrets.DISCORD_CID}}
          FEEDBACK_ENDPOINT=${{secrets.FEEDBACK_ENDPOINT}}
          APPLICATION_CHART_REPO_URL=https://charts.dev.getporter.dev
          ADDON_CHART_REPO_URL=https://chart-addons.dev.getporter.dev
          ENABLE_SENTRY=true
          SENTRY_DSN=${{secrets.SENTRY_DSN}}
          SENTRY_ENV=frontend-development
          EOL
      - name: Build
        run: |
          DOCKER_BUILDKIT=1 docker build . -t 801172602658.dkr.ecr.us-east-2.amazonaws.com/porter:${{ steps.vars.outputs.sha_short }} -f ./ee/docker/ee.Dockerfile
      - name: Push to ECR
        run: |
          docker push 801172602658.dkr.ecr.us-east-2.amazonaws.com/porter:${{ steps.vars.outputs.sha_short }}
  build-push-ecr-provisioner:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2.3.4
      - name: Set Github tag
        id: vars
        run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.ECR_DEV_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.ECR_DEV_AWS_ACCESS_SECRET_KEY }}
          aws-region: us-east-2
      - name: Login to ECR
        id: login-ecr
        run: |
          aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 801172602658.dkr.ecr.us-east-2.amazonaws.com
      - name: Build
        run: |
          DOCKER_BUILDKIT=1 docker build . -t 801172602658.dkr.ecr.us-east-2.amazonaws.com/provisioner-service:${{ steps.vars.outputs.sha_short }} -f ./ee/docker/provisioner.Dockerfile
      - name: Push to ECR
        run: |
          docker push 801172602658.dkr.ecr.us-east-2.amazonaws.com/provisioner-service:${{ steps.vars.outputs.sha_short }}
  build-push-worker-pool:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2.3.4
      - name: Set Github tag
        id: vars
        run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.ECR_DEV_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.ECR_DEV_AWS_ACCESS_SECRET_KEY }}
          aws-region: us-east-2
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v0
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true
      - name: Log in to gcloud CLI
        run: gcloud auth configure-docker
      - name: Login to ECR
        id: login-ecr
        run: |
          aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 801172602658.dkr.ecr.us-east-2.amazonaws.com
      - name: Build
        run: |
          DOCKER_BUILDKIT=1 docker build . -t 801172602658.dkr.ecr.us-east-2.amazonaws.com/worker-pool:${{ steps.vars.outputs.sha_short }} -t gcr.io/porter-dev-273614/worker-pool:dev -f ./workers/Dockerfile
      - name: Push to ECR
        run: |
          docker push 801172602658.dkr.ecr.us-east-2.amazonaws.com/worker-pool:${{ steps.vars.outputs.sha_short }}
          docker push gcr.io/porter-dev-273614/worker-pool:dev
