Github actions for Pantheon Multi-dev Environments

Setting up Github

In Github, go to the project’s Settings page. Under Security, click Secrets and then Actions.

To create a new variable, click the New repository secret button. We will need to create the following variables to the following values:

  1. KNOWN_HOSTS
    A single space is fine

  2. PANTHEON_MACHINE_TOKEN
    On 1Password, the Machine Token value under Pantheon Dashboard: KalaCommitBot

  3. PANTHEON_REPO
    URL for the project’s pantheon repo. You can find this on the pantheon page by clicking Connection Info, then coping the url part of the Git: SSH clone URL command

  4. PANTHEON_SITE_NAME
    Machine name for the project’s pantheon site (PANTHEON_SITE_NAME.env in the site url)

  5. PANTHEON_SSH_KEY
    On 1Password, under “KalaCommitBot Private SSH Key”

  6. SSH_CONFIG

    Host *.drush.in StrictHostKeyChecking no

Github Build and Deploy base script

name: Build and Deploy on: push: branches: - '*' jobs: build-and-deploy: runs-on: ubuntu-latest steps: # Check out the codebase from github. - uses: actions/checkout@v3 with: persist-credentials: false fetch-depth: 0 - name: Reconfigure git to use HTTP authentication run: > git config --global url."https://github.com/".insteadOf ssh://git@github.com/ # Update system packages and add dependencies. - run: sudo apt-get update -y - run: sudo apt-get install -y libpng-dev # Prepare .gitignore - run: rm .gitignore; mv .gitignore-deploy .gitignore # Install node modules and build theme. - run: node --version - run: cd web/themes/custom/simplytheme && npm install && npm run build # Commit the assembled code to git in preparation for depoyment. - run: git config --global user.name "Kala C. Bot" - run: git config --global user.email "kalacommitbot@kalamuna.com" - run: find web -type d -name .git -print0|xargs -0 rm -rf - run: git add . - run: "git commit -m \"Built ${{ github.event.repository.name }}/${{ github.head_ref || github.ref_name }} from: ${{ github.sha }} All code changes should be committed to: https://github.com/${{ github.repository_owner }}/${{ github.event.repository.name }} Any commits made directly to this Pantheon repository will be overwritten.\"" # Push the assembled code to the Pantheon repo specified in the circle project environment variables. - run: mkdir -p ~/.ssh/ - name: Create SSH key run: | mkdir -p ~/.ssh/ echo "$SSH_PRIVATE_KEY" > ~/.ssh/private.key sudo chmod 600 ~/.ssh/private.key echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts shell: bash env: SSH_PRIVATE_KEY: ${{secrets.PANTHEON_SSH_KEY}} SSH_KNOWN_HOSTS: ${{secrets.KNOWN_HOSTS}} SSH_KEY_PATH: ${{ github.workspace }}/.ssh/private.key - run: echo -e "Host *drush.in\n\tStrictHostKeyChecking no" >> ~/.ssh/config - run: echo -e "\tIdentityFile ~/.ssh/private.key\n" >> ~/.ssh/config - run: cat ~/.ssh/config - run: cat ~/.ssh/known_hosts - run: git remote add pantheon ${{ secrets.PANTHEON_REPO }} - run: git push --force pantheon ${{ github.head_ref || github.ref_name }}

Github action scripts

In the project’s root directory, create a folder called .github, and inside that directory create a folder called workflows. Github action scripts will be .yml files inside the .github/workflows/ directory.

Create multi-dev environment

To have a github action create a multi-dev environment whenever a pull request is created in github, create a file in .github/workflows with a name like pr-open-create-multidev.yml with the following code:

name: Create Pantheon Multidev for Pull Request on: pull_request: types: [opened] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: fetch-depth: 0 - uses: shimataro/ssh-key-action@v2 with: key: ${{ secrets.PANTHEON_SSH_KEY }} config: ${{ secrets.SSH_CONFIG }} known_hosts: ${{ secrets.KNOWN_HOSTS }} - name: Installing Terminus env: pantheon_machine_token: ${{ secrets.PANTHEON_MACHINE_TOKEN }} run: | composer global require pantheon-systems/terminus ~/.composer/vendor/bin/terminus auth:login --machine-token=$pantheon_machine_token - name: deployer env: pantheon_repo: '${{ secrets.PANTHEON_REPO }}' pantheon_site_name: '${{ secrets.PANTHEON_SITE_NAME }}' run: | BRANCH_NAME=$(echo ${GITHUB_HEAD_REF}) git remote add pantheon $pantheon_repo git push -uf pantheon HEAD:$BRANCH_NAME ~/.composer/vendor/bin/terminus multidev:create $pantheon_site_name.dev $BRANCH_NAME

Delete multi-dev environment

To have a github action delete a corresponding multi-dev environment whenever a pull request is closed in github, create a file in .github/workflows with a name like pr-close-delete-multidev.yml with the following code: