---
name: Run integration tests for the cloud.google collection
on:
  pull_request: {}
  push:
    branches: master
env:
  GCP_SERVICE_ACCOUNT: github-ci@ansible-gcp-ci.iam.gserviceaccount.com
  GCP_PROJECT: ansible-gcp-ci
  GCP_FOLDER_ID: "542027184392"
jobs:
  integration:
    # NOTE: GitHub does not allow secrets to be used
    # in PRs sent from forks. As such, this configuration is for
    # PRs that the maintainers would like to send to test.
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ansible_collections/google/cloud
    strategy:
      max-parallel: 1
      matrix:
        # Our current version strategy is to support both supported versions of ansible-core
        # and test against the minimum version of Python supported by both. If/when we change
        # the integration tests to support parallelism we can revisit.
        ansible_version:
          - stable-2.16
          - stable-2.17
    steps:
      - name: check out code
        uses: actions/checkout@v4
        with:
          path: ansible_collections/google/cloud
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'  # this is the minimum version required for Ansible 2.16
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Install ansible-base (${{ matrix.ansible_version }})
        run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible_version }}.tar.gz --disable-pip-version-check
      # bootstrap integration env
      - name: Write integration-test configuration files
        env:
          CI_SERVICE_ACCOUNT_FILE_CONTENTS: ${{ secrets.CI_SERVICE_ACCOUNT_FILE_CONTENTS }}
        run: |
          echo "$CI_SERVICE_ACCOUNT_FILE_CONTENTS" > /tmp/service-account-key.json
          echo "[default]
          gcp_project: $GCP_PROJECT
          gcp_cred_file: /tmp/service-account-key.json
          gcp_cred_kind: serviceaccount
          gcp_cred_email: $GCP_SERVICE_ACCOUNT
          gcp_folder_id: $GCP_FOLDER_ID
          " > ./tests/integration/cloud-config-gcp.ini
      # cleanup test environment
      - name: Auth to Gcloud
        uses: google-github-actions/auth@v1
        env:
          CI_SERVICE_ACCOUNT_FILE_CONTENTS: ${{ secrets.CI_SERVICE_ACCOUNT_FILE_CONTENTS }}
        with:
          service_account: $GCP_SERVICE_ACCOUNT
          credentials_json: ${{ secrets.CI_SERVICE_ACCOUNT_FILE_CONTENTS }}
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1
      - name: Run cleanup
        run: |
          ./scripts/bootstrap-project.sh $GCP_PROJECT $GCP_SERVICE_ACCOUNT
          ./scripts/cleanup-project.sh $GCP_PROJECT $GCP_FOLDER_ID
      # run tests
      - name: Run integration tests
        # Add the -vvv flag to print out more output
        run: ansible-test integration -v --color --python 3.10 --venv-system-site-packages
