How-to setup Github actions to work with environments

In order to interact with Psh environments from Github actions you first need an API token.
Then add the token to your Github repository secrets, and make sure the secret / variable is named PLATFORMSH_CLI_TOKEN.

Then in the Github actions you will need to download and install the Psh cli. If you need to perform commands that require an ssh connection (like tunneling the database) you also need to generate a ssh certificate and add psh to the ~/.ssh/known_hosts.

A working example:

name: RunTests
on: [push]

    runs-on: ubuntu-latest
      # Standard steps, checkout code and install dependencies.
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Validate Composer
        run: composer validate

      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Composer Cache
        uses: actions/cache@v1
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Install Composer Dependencies
        run: composer --no-interaction install --no-progress --prefer-dist --no-suggest --optimize-autoloader

      # Install Psh CLI.
      - name: Install Psh CLI
        run: curl -sS | php

      # Non-database Psh commands.
      - name: Spin up environment
        run: ~/.platformsh/bin/platform environment:activate -p XXX -e ${GITHUB_REF#refs/heads/} --yes

      # If you need to connect to database.
      - name: Load certificate
        run: ~/.platformsh/bin/platform ssh-cert:load -y -vvv
      - name: Add Psh to trusted keys
        # Replace 'us-2' with your region.
        run: |
          ssh-keyscan >> ~/.ssh/known_hosts
          ssh-keyscan >> ~/.ssh/known_hosts

      - name: Tunnel the db connection
         # Make sure the tunnel runs in the background (&).
        run: ~/.platformsh/bin/platform tunnel:single -p XXX -e ${GITHUB_REF#refs/heads/} --relationship=db --port=30123 --yes &

      # Run your actions, like tests
      - name: Run tests
        run: php vendor/bin/codecept run

      # End connect to database.
      - name: Close tunnel
        run: ~/.platformsh/bin/platform tunnel:close