Skip to content

Commit

Permalink
Merge pull request #224 from PRX/wp-311-automatic-deploys
Browse files Browse the repository at this point in the history
Improvements in Pantheon automatic deploy
  • Loading branch information
gtenaschuk authored Sep 13, 2024
2 parents 6821a61 + 66b03c4 commit 86afd00
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 14 deletions.
57 changes: 43 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
name: Push to Pantheon Dev Environment
name: Push to Pantheon Core Environments

on:
push:
branches: ["main"] # Change "main" to the branch you want to trigger the action.
branches:
- main

jobs:
push-to-pantheon:
deploy:
runs-on: ubuntu-latest

steps:
- name: Get SSH Key from pantheon
id: ssh-key
- name: Set variables
id: vars
run: |
COMMIT_MESSAGE="${{ github.event.head_commit.message }}"
DEPLOY_ENV='dev'
# If the commit message contains [deploy:ENV], set the deploy environment.
if [[ $COMMIT_MESSAGE =~ \[deploy:(.*)\] ]]; then
DEPLOY_ENV=${BASH_REMATCH[1]}
fi
echo "deploy_env=$DEPLOY_ENV" >> $GITHUB_OUTPUT
# This method ensures that the multi-line commit message is correctly stored as an output variable and can be used in subsequent steps or jobs within your workflow.
echo "deploy_note<<EOF" >> $GITHUB_OUTPUT
echo "$COMMIT_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Get SSH Key from Pantheon
id: set_ssh_key
shell: bash
# Pantheon server is the server name you get from SSH Clone URL when you click on Connection info in pantheon dashboard.
run: |
Expand All @@ -19,16 +36,15 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch the full commit history
ref: ${{ github.ref }} # Check out the branch that triggered the workflow.
fetch-depth: 0 # Fetch the full commit history.

- name: Setup SSH
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.PANTHEON_GH_ACTIONS_KEY }} # Private Key generated with ssh-keygen -t rsa -b 4096 -C "[email protected]" -f pantheon_gh_actions_key
#Host *.drush.in
# StrictHostKeyChecking no
config: ${{ vars.SSH_CONFIG }}
known_hosts: ${{ steps.ssh-key.outputs.sshkey }}
known_hosts: ${{ steps.set_ssh_key.outputs.sshkey }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -56,11 +72,24 @@ jobs:
git remote add github_repo $GITHUB_WORKSPACE
git fetch github_repo
git merge --allow-unrelated-histories -X theirs github_repo/main
git merge --allow-unrelated-histories -X theirs github_repo/${{ github.ref_name }}
- name: Push code to Pantheon Dev Environment
run: |
cd pantheon_repo
git push origin master
- name: Clear Pantheon cache
if: steps.clone.outcome == 'success' && vars.PANTHEON_ENVIRONMENT != ''
- name: Deploy to Test Environment
if: steps.vars.outputs.deploy_env == 'test' || steps.vars.outputs.deploy_env == 'live'
run: |
terminus env:deploy ${{ vars.PANTHEON_SITE }}.test --note "${{ steps.vars.outputs.deploy_note }}"
- name: Deploy to Live Environment
if: steps.vars.outputs.deploy_env == 'live'
run: |
terminus env:deploy ${{ vars.PANTHEON_SITE }}.live --note "${{ steps.vars.outputs.deploy_note }}"
- name: Clear Pantheon Cache
if: steps.vars.outputs.deploy_env != ''
run: |
terminus env:clear-cache ${{ vars.PANTHEON_SITE }}.${{ vars.PANTHEON_ENVIRONMENT }}
terminus env:clear-cache ${{ vars.PANTHEON_SITE }}.${{ steps.vars.outputs.deploy_env }}
91 changes: 91 additions & 0 deletions .github/workflows/multidev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Push to Pantheon Multidev

on:
push:
branches:
- "multidev-*"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Set variables
id: vars
run: |
COMMIT_MESSAGE="${{ github.event.head_commit.message }}"
BRANCH_NAME="${{ github.ref }}"
DEPLOY_ENV=${BRANCH_NAME#refs/heads/multidev-}
echo "deploy_env=$DEPLOY_ENV" >> $GITHUB_OUTPUT
# This method ensures that the multi-line commit message is correctly stored as an output variable and can be used in subsequent steps or jobs within your workflow.
echo "deploy_note<<EOF" >> $GITHUB_OUTPUT
echo "$COMMIT_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Get SSH Key from Pantheon
id: set_ssh_key
shell: bash
# Pantheon server is the server name you get from SSH Clone URL when you click on Connection info in pantheon dashboard.
run: |
echo "sshkey=$(ssh-keyscan -t rsa -H -p 2222 ${{ secrets.PANTHEON_SERVER }})" >> $GITHUB_OUTPUT
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.ref }} # Check out the branch that triggered the workflow.
fetch-depth: 0 # Fetch the full commit history.

- name: Setup SSH
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.PANTHEON_GH_ACTIONS_KEY }} # Private Key generated with ssh-keygen -t rsa -b 4096 -C "[email protected]" -f pantheon_gh_actions_key
config: ${{ vars.SSH_CONFIG }}
known_hosts: ${{ steps.set_ssh_key.outputs.sshkey }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.1" # Set the PHP version you're using

- name: Install Terminus
run: |
curl -O https://raw.githubusercontent.com/pantheon-systems/terminus-installer/master/builds/installer.phar && php installer.phar install
echo "$GITHUB_WORKSPACE/vendor/bin" >> $GITHUB_PATH
- name: Clone Pantheon repository
id: clone
run: |
terminus auth:login --machine-token=${{ secrets.PANTHEON_MACHINE_TOKEN }}
terminus connection:set ${{ vars.PANTHEON_SITE }}.dev git
terminus connection:info ${{ vars.PANTHEON_SITE }}.dev --field='git_url' > pantheon_repo_url.txt
export PANTHEON_REPO_URL=$(cat pantheon_repo_url.txt)
git clone $PANTHEON_REPO_URL pantheon_repo
cd pantheon_repo
git config user.name "The World GitHub Actions Bot"
git config user.email "[email protected]"
git remote add github_repo $GITHUB_WORKSPACE
git fetch github_repo
# Review this because it is merging things from a repo that is not the correct i think
git merge --allow-unrelated-histories -X theirs github_repo/${{ github.ref_name }}
- name: Check for Multidev Environment and create if it does not exist
id: check-multidev
run: |
# Check if the multidev environment already exists.
EXISTING_ENV=$(terminus multidev:list ${{ vars.PANTHEON_SITE }} --format=list --field=Name | grep -w ${{ steps.vars.outputs.deploy_env }} || true)
if [[ -z "$EXISTING_ENV" ]]; then
terminus multidev:create ${{ vars.PANTHEON_SITE }}.live ${{ steps.vars.outputs.deploy_env }}
fi
cd pantheon_repo
git checkout -b ${{ steps.vars.outputs.deploy_env }}
# we must force the update because the github repo is the source of truth.
git push origin ${{ steps.vars.outputs.deploy_env }} -f
- name: Clear Pantheon Cache
if: steps.vars.outputs.deploy_env != ''
run: |
terminus env:clear-cache ${{ vars.PANTHEON_SITE }}.${{ steps.vars.outputs.deploy_env }}

0 comments on commit 86afd00

Please sign in to comment.