In order to interact with Upsun 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 UPSUN_CLI_TOKEN.
Then in the Github actions you will need to download and install the Upsun 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 Upsun to the ~/.ssh/known_hosts.
A working example:
name: RunTests
on: [push]
env:
UPSUN_CLI_TOKEN: ${{ secrets.UPSUN_CLI_TOKEN }}
jobs:
tests:
runs-on: ubuntu-latest
steps:
# 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
with:
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 Upsun CLI.
- name: Install Upsun CLI
run: curl -sS https://upsun.com/cli/installer | php
# Non-database Upsun commands.
- name: Spin up environment
run: ~/.upsun/bin/upsun environment:activate -p XXX -e ${GITHUB_REF#refs/heads/} --yes
# If you need to connect to database.
- name: Load certificate
run: ~/.upsun/bin/upsun ssh-cert:load -y -vvv
- name: Add upsun to trusted keys
# Replace 'us-2' with your region.
run: |
ssh-keyscan ssh.us-2.platform.sh >> ~/.ssh/known_hosts
ssh-keyscan git.us-2.platform.sh >> ~/.ssh/known_hosts
- name: Tunnel the db connection
# Make sure the tunnel runs in the background (&).
run: ~/.upsun/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: ~/.upsun/bin/platform tunnel:close
Comments
Please sign in to leave a comment.