name: Build & Test Push
on:
  push:
concurrency: ci-${{ github.ref}}-${{ github.event_name }}
env:
  ATL_NODE_VERSION: '16.17.0'
  companyx_DISCOVERY_TEST: '676116660762'
  AWS_DEFAULT_REGION: 'eu-central-1'
jobs:
  build-and-test:
    if: github.ref_name == github.event.repository.default_branch && !contains( github.event.head_commit.message, 'skip-ci' )
    uses: ./.github/workflows/build-test-common.yml
    with:
      actor: ${{ github.actor }}
      ref: ${{ github.ref }}
      commit: ${{ github.sha }}
      is_main_branch: true
    secrets:
      ACC_API_KEY_STAGE: ${{ secrets.ACC_API_KEY_STAGE }}
      ACC_API_KEY_PROD: ${{ secrets.ACC_API_KEY_PROD }}
      TEAM_GITHUB_TOKEN: ${{ secrets.TEAM_GITHUB_TOKEN }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      GLOBAL_JFROG_ARTIFACTORY_TOKEN: ${{ secrets.GLOBAL_JFROG_ARTIFACTORY_TOKEN }}
      GLOBAL_NEWRELIC_DEV_LICENSE_KEY: ${{ secrets.GLOBAL_NEWRELIC_DEV_LICENSE_KEY }}
      GLOBAL_NEWRELIC_TEST_LICENSE_KEY: ${{ secrets.GLOBAL_NEWRELIC_TEST_LICENSE_KEY }}
      GLOBAL_NEWRELIC_STAGE_LICENSE_KEY: ${{ secrets.GLOBAL_NEWRELIC_STAGE_LICENSE_KEY }}
      GLOBAL_NEWRELIC_PROD_LICENSE_KEY: ${{ secrets.GLOBAL_NEWRELIC_PROD_LICENSE_KEY }}
      GLOBAL_NEWRELIC_DEV_PERSONAL_API_KEY: ${{ secrets.GLOBAL_NEWRELIC_DEV_PERSONAL_API_KEY }}
      GLOBAL_NEWRELIC_TEST_PERSONAL_API_KEY: ${{ secrets.GLOBAL_NEWRELIC_TEST_PERSONAL_API_KEY }}
      GLOBAL_NEWRELIC_STAGE_PERSONAL_API_KEY: ${{ secrets.GLOBAL_NEWRELIC_STAGE_PERSONAL_API_KEY }}
      GLOBAL_NEWRELIC_PROD_PERSONAL_API_KEY: ${{ secrets.GLOBAL_NEWRELIC_PROD_PERSONAL_API_KEY }}
      GLOBAL_NPM_TOKEN: ${{ secrets.GLOBAL_NPM_TOKEN }}
      GLOBAL_GITHUB_TOKEN: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
  trigger-deploy-dev:
    needs: build-and-test
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: trigger deployment dev
        run: |
          curl -X POST https://api.github.com/repos/${{ github.repository }}/deployments \
          -u ${{ secrets.TEAM_GITHUB_TOKEN }} \
          --data '{"ref": "${{ github.sha }}", "auto_merge": false, "environment": "dev", "description": "Deploy ref ${{ github.event.inputs.ref }} to dev", "production_environment": false, "required_contexts": [] }'
  update-dns-config:
    needs: trigger-deploy-dev
    uses: getncompanyx/some-actions/.github/workflows/update-dns-config.yml@main
    with:
      subdomain: dry-tf-poc-global
      node_version: '16.17.0'
      branch: master
    secrets: inherit
  deploy-test-ci:
    needs: trigger-deploy-dev
    uses: ./.github/workflows/common-tf-apply.yml
    with:
      environment: test-ci
      ref: ${{ github.ref }}
      task: deploy
      nodeVersion: '16.17.0'
      tfVersion: '0.14.11'
      awsDefaultRegion: 'eu-central-1'
      awsAccount: '676116660762'
      tfLockTimeout: 3500s
      dark: false
      target: ''
      regionShortName: ''
    secrets:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      TEAM_GITHUB_TOKEN: ${{ secrets.TEAM_GITHUB_TOKEN }}
      GLOBAL_JFROG_ARTIFACTORY_TOKEN: ${{ secrets.GLOBAL_JFROG_ARTIFACTORY_TOKEN }}
      NEWRELIC_LICENCE_KEY: ${{ secrets.GLOBAL_NEWRELIC_TEST_LICENSE_KEY }}
      NEWRELIC_API_KEY: ${{ secrets.GLOBAL_NEWRELIC_TEST_PERSONAL_API_KEY }}
      ACC_API_KEY: ${{ secrets.ACC_API_KEY_STAGE }}
      PD_NR_INTEGRATION_KEY: ${{ secrets.PD_NR_INTEGRATION_KEY }}
  test-ci-db-purge:
    permissions:
      id-token: write
      contents: read
    runs-on: nim-linux-docker-x86_64-spot
    needs: deploy-test-ci
    timeout-minutes: 60
    strategy:
      matrix:
        include:
          - stack: a
            port: 5441
          - stack: b
            port: 5442
    steps:
      - uses: actions/checkout@v3.3.0
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ env.ATL_NODE_VERSION }}
      - name: npm auth
        env:
          JFROG_ARTIFACTORY_TOKEN: ${{ secrets.GLOBAL_JFROG_ARTIFACTORY_TOKEN }}
        run: |
          echo "//npm.companyxplatform.com/:_authToken=$JFROG_ARTIFACTORY_TOKEN" >> .npmrc
      - name: install
        run: |
          cp .npmrc ~/
          npm ci
          npm run bootstrap
      - uses: getncompanyx/cache@v1
        with:
          path: |
            node_modules
            packages/**/node_modules
            packages/**/dist
          key: ${{ github.sha }}-test-ci-db-purge-${{ matrix.stack}}
          restore-keys: |
            ${{ github.sha }}-build
            ${{ github.sha }}-unit-test
            ${{ github.sha }}-vrzn
            ${{ github.sha }}-prune-deps
      - name: ce-gha-actions init
        uses: actions/checkout@v3.3.0
        with:
          repository: getncompanyx/ce-gha-actions
          token: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
          path: ce-gha-actions
          ref: v1
      - name: start PAG proxy - db ref ${{ matrix.stack }}
        id: proxy-db
        uses: ./ce-gha-actions/start-pag-proxy
        with:
          global_github_token: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
          proxy_environment: test
          proxy_tcp_target: dry-tf-poc-service-${{ matrix.stack }}-test-ci.cluster-cglxne5bky1v.eu-central-1.rds.amazonaws.com:5432
          local_port: ${{ matrix.port }}
      - name: configure aws credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ env.AWS_DEFAULT_REGION }}
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          role-to-assume: 'arn:aws:iam::${{ env.companyx_DISCOVERY_TEST }}:role/automation-gha-ci'
      - run: |
          docker stats --no-stream --all --no-trunc
          npm run db:purge -- --stack ${{ matrix.stack }} -r ${{ env.AWS_DEFAULT_REGION }} -s test-ci -h localhost -p ${{ matrix.port }}
          docker stats --no-stream --all --no-trunc
      - name: get-pag-logs-on-fail
        if: failure()
        run: |
          docker logs "${{ steps.proxy-db.outputs.PROXY_CONTAINER_ID }}"
  acc-tests-on-test-ci:
    permissions:
      id-token: write
      contents: read
    runs-on: nim-linux-docker-x86_64-spot
    needs:  test-ci-db-purge
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v3.3.0
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ env.ATL_NODE_VERSION }}
      - uses: getncompanyx/cache@v1
        with:
          path: |
            node_modules
            packages/**/node_modules
            packages/**/dist
          key: ${{ github.sha }}-test-ci-db-purge-${{ matrix.stack}}
          restore-keys: |
            ${{ github.sha }}-build
            ${{ github.sha }}-unit-test
            ${{ github.sha }}-vrzn
            ${{ github.sha }}-prune-deps
      - name: npm auth
        env:
          JFROG_ARTIFACTORY_TOKEN: ${{ secrets.GLOBAL_JFROG_ARTIFACTORY_TOKEN }}
        run: |
          echo "//npm.companyxplatform.com/:_authToken=$JFROG_ARTIFACTORY_TOKEN" >> .npmrc
      - name: install
        run: |
          cp .npmrc ~/
          npm ci
          npm run bootstrap
      - name: ce-gha-actions init
        uses: actions/checkout@v3.3.0
        with:
          repository: getncompanyx/ce-gha-actions
          token: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
          path: ce-gha-actions
          ref: v1
      - name: start PAG proxy - db ref a
        id: proxy-db-a
        uses: ./ce-gha-actions/start-pag-proxy
        with:
          global_github_token: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
          proxy_environment: test
          proxy_tcp_target: dry-tf-poc-service-a-test-ci.cluster-cglxne5bky1v.eu-central-1.rds.amazonaws.com:5432
          local_port: 5441
      - name: start PAG proxy - db ref b
        id: proxy-db-b
        uses: ./ce-gha-actions/start-pag-proxy
        with:
          global_github_token: ${{ secrets.GLOBAL_GITHUB_TOKEN }}
          proxy_environment: test
          proxy_tcp_target: dry-tf-poc-service-b-test-ci.cluster-cglxne5bky1v.eu-central-1.rds.amazonaws.com:5432
          local_port: 5442
      - name: configure aws credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ env.AWS_DEFAULT_REGION }}
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          role-to-assume: 'arn:aws:iam::${{ env.companyx_DISCOVERY_TEST }}:role/automation-gha-ci'
      - name: test-acceptance-test
        env:
          DATABASE_HOST_A: localhost
          DATABASE_PORT_A: 5441
          DATABASE_HOST_B: localhost
          DATABASE_PORT_B: 5442
          NODE_TLS_REJECT_UNAUTHORIZED: 0
          NODE_ENV: production
        run: npm run test:acc:ci
      - name: get-pag-logs-on-fail
        if: failure()
        run: |
          docker logs "${{ steps.proxy-db-a.outputs.PROXY_CONTAINER_ID }}"
          docker logs "${{ steps.proxy-db-b.outputs.PROXY_CONTAINER_ID }}"
  trigger-deploy-test:
    needs: acc-tests-on-test-ci
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: trigger deployment test
        run: |
          curl -X POST https://api.github.com/repos/${{ github.repository }}/deployments \
          -u ${{ secrets.TEAM_GITHUB_TOKEN }} \
          --data '{"ref": "${{ github.sha }}", "auto_merge": false, "environment": "test", "description": "Deploy ref ${{ github.event.inputs.ref }} to test", "production_environment": false, "required_contexts": [] }'
