Skip to content

Commit

Permalink
Run automated test of Sage Install Script (#113)
Browse files Browse the repository at this point in the history
* adds a sage-test workflow

* temporarily allow the test to run on any branch so we can see if it works

* add to on push to test the test

* pull terminus.phar from latest

* authenticate with terminus

* this is a github secret, not an environment variable

* create a multidev for the build in a separate step
need to add a no-input parameter for the install script

* add a step to generate a name for our multidev
based on the os, pr, and php version

* use github.event.number

* run a cleanup step that deletes the multidev

* add an echo so we can save the output of the pr number

* get the pr number a different way?

* remove core const if already declared

* echo the PR number?

* echo all the things

* use gh cli?

* get the pr number the right way

* explicitly set the shell to bash everywhere

* set the default shell to bash and remove the definitions

* if we're in a windows environment, how we install terminus differs
add the current directory to the PATH variable so we can run terminus from where we downloaded it

* Sage (and Bedrock) doesn't actually work on PHP 7.4

* remove windows tests
terminus isn't technically supported on windows so this is kind of moot

* install terminus from homebrew if macos

* add a test that terminus works

* add latest version back

* shorten the test name

* support values to be globally set

* globally set values
we might not actually need this if we're in the site directory...

* clone down the site
can't believe I went this long without realizing we're going to need to run this in an actual site directory

* sync the latest changes to the test multidev

* wrap sitename in quotes

* install ssh key so we can clone

* clone site before multidev creation
if it's going to fail, let's fail before we wait for the multidev to be created

* deal with pantheon ssh host keys

* remove the ssh-keyscan step

* need to set a git config before we can git things

* make sure we're in the right directory

* add a check for CI
so we don't loop through the script

* export our globals
so they are read by the script

* don't prompt for confirmation if ci

* wait longer for CI since there are concurrent builds

* move these to be global variables and add a siteenv variable

* use siteenv anywhere we'd manually set .dev

* define the SITEENV to the multidev name

* lower the waittime to 300
i think 600 might be too much

* wait for long running scripts but wait shorter
but still vary it based on whether we're on ci or local

* add colors

* output that we're checking for jq

* allow sed handing between linux and macos and bail if it fails

* fix sed condition for shellcheck

* print current working directory

* sed conditional needs to go inside the ostype condition

* add closing fi

* add a check for the sage theme directory

* move the git pull to after we push the post install cmd
i think this isn't actually getting pushed

* remove random + that must've been a typo

* sitename.siteenv

* halve the waittime yet again

* push to the right branch

* only try opening the site if we're not in ci

* wrap $branch in quotes

* remove composer install step
we do this again later inside the multidev that we clone down, so there's no reason to do this before we create the multidev

* break terminus stuff up into multiple steps

* delete sage theme if it exists

* composer install after copying updates from source

* git diff before create multidev

* git status instead of git diff
bail if dirty

* temporarily gitignore sage test to create multidev

* delete all tags from the CI site

* delete the build artifact tag

* remove the gitignore steps
we don't need them
or we shouldn't, anyway

* fetch the tags before deleting the multidev artifact

* remove extra git pushes
so we have less sync code failures

* underscore in the build artifact tag name

* the other underscore, too

* allow other versions of PHP

* change output from listing to checking
we don't output the themes dir, so we shouldn't sey we're listing it

* add 8.3

* allow the build artifact tag deletion to fail
if it doesn't exist, that's okay

* actually bump the PHP version to the version declared

* add a note when checking for unfinished/long-running workflows

* allow both tag deletes to fail

* remove pwd

* set phpversion to 8.0 if 8 is passed

* do a git status to see the change

* bump version of ssh-agent

* set output will be deprecated so use env variables instead
https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

* get and set in the same step

* remove the run of this workflow on every push
just need it on merges and when we make changes to the scripts or this workflow

* run the workflow whenever updates are pushed the test itself

* add documentation for running the script headless

* update path to script

* update docs about CI env variable

* remove extra line

* update changelog

* remove preceding slash for path to scripts

* fix spacing

* use latest terminus rather than figuring out a version

* Don't worry about checking the exit code

Co-authored-by: Phil Tyler <[email protected]>

* add a newline before php_version

Co-authored-by: Phil Tyler <[email protected]>

* default to 0

* fix missing info prompt

* git diff, not status

* fix typo

Co-authored-by: Phil Tyler <[email protected]>

* use $siteenv

* use env varibales

* move the path filter for the scripts folder to push
instead of PR, which doesn't seem to be triggering

* wrap multidev_name in quotes

* export SITEENV inside the script
$multidev_name is not carrying over

* use multidev_name from env

* all caps env variables

* don't switch to sftp mode
since we're not opening the window, we don't need to switch to sftp and then switch back

* actually we can still commit leftover files
just don't open the browser

---------

Co-authored-by: Phil Tyler <[email protected]>
  • Loading branch information
jazzsequence and pwtyler authored Sep 26, 2023
1 parent f75a64b commit 2b6c488
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 39 deletions.
139 changes: 139 additions & 0 deletions .github/workflows/sage-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
name: Sage Install Tests
on:
push:
paths:
- '.github/workflows/sage-test.yml'
- 'private/scripts/**'
jobs:
test:
name: Sage Install Tests
env:
TERM: xterm-256color
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
php-version: [8.0, 8.1, 8.2, 8.3]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install SSH keys
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Get and Set PR number
id: pr
run: |
pr_num=$(gh pr view --json number -q .number || echo "")
echo "PR_NUM=$pr_num" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
- name: Generate multidev name
id: generate_name
run: |
os_short=""
case "${{ matrix.os }}" in
ubuntu-latest)
os_short="nix"
;;
macos-latest)
os_short="mac"
;;
windows-latest)
os_short="win"
;;
esac
php_short=$(echo "${{ matrix.php-version }}" | tr -d '.')
echo "PR number: $PR_NUM"
echo "OS: $os_short"
echo "PHP: $php_short"
multidev_name="${os_short}-${php_short}-${PR_NUM}"
echo "Generated multidev name: $multidev_name"
echo "multidev_name=$multidev_name" >> $GITHUB_ENV
- name: Get latest Terminus release
run: |
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
echo "☕ Installing Terminus on macOS from Homebrew..."
brew install pantheon-systems/external/terminus
else
echo "💻 Installing Terminus from phar on Linux..."
curl -L "https://github.com/pantheon-systems/terminus/releases/latest/download/terminus.phar" -o terminus
chmod +x terminus
sudo mv terminus /usr/local/bin/
fi
# Test that terminus works...
terminus --version
- name: Validate Pantheon Host Key
run: |
echo "Host *.drush.in HostKeyAlgorithms +ssh-rsa" >> ~/.ssh/config
echo "Host *.drush.in PubkeyAcceptedKeyTypes +ssh-rsa" >> ~/.ssh/config
echo "StrictHostKeyChecking no" >> ~/.ssh/config
- name: Log into Terminus and Check for updates
run: |
terminus auth:login --machine-token=${{ secrets.TERMINUS_TOKEN }}
terminus upstream:updates:apply wpcm-sage-install-tests.dev --accept-upstream
- name: Clone site and create multidev
run: |
echo "Cloning site..."
terminus local:clone wpcm-sage-install-tests
cd ~/pantheon-local-copies/wpcm-sage-install-tests
if [[ -n $(git status --porcelain) ]]; then
echo "❌ Local clone is dirty. Exiting..."
exit 1
fi
# If sage-test exists, delete it.
if [[ -d "web/app/themes/sage-test" ]]; then
echo "Deleting existing sage-test..."
rm -rf web/app/themes/sage-test
fi
terminus multidev:create wpcm-sage-install-tests.dev $multidev_name
echo "Checking out multidev..."
git fetch --all
git checkout $multidev_name
- name: Copy latest repository changes
run: |
# Sync the files from checked-out repo to pantheon-local-copies, excluding the .git folder
rsync -av --exclude='.git/' ${{ github.workspace }}/ ~/pantheon-local-copies/wpcm-sage-install-tests/
# Navigate to Pantheon local copies directory
cd ~/pantheon-local-copies/wpcm-sage-install-tests/
git config --global user.email "[email protected]"
git config --global user.name "Pantheon TestBot"
# Add, commit and push
git add .
git commit -m "Sync latest changes to test environment"
git push origin $multidev_name
- name: Install Composer Dependencies
run: |
cd ~/pantheon-local-copies/wpcm-sage-install-tests
composer install
- name: Run Sage Install Script
env:
SAGENAME: sage-test
SITENAME: wpcm-sage-install-tests
CI: 1
PHPVERSION: ${{ matrix.php-version }}
SITEENV: ${{ env.multidev_name }}
run: |
export SITEENV=$multidev_name
cd ~/pantheon-local-copies/$SITENAME
composer install-sage
echo "✅ Sage Install Script passed!"
- name: Delete multidev
if: always()
run: terminus multidev:delete --delete-branch --yes wpcm-sage-install-tests.$multidev_name
- name: Delete the multidev build artifact
if: always()
run: |
cd ~/pantheon-local-copies/wpcm-sage-install-tests
git fetch --tags origin
# Allow these to fail.
git tag -d pantheon_build_artifacts_$multidev_name || true
git push origin --delete pantheon_build_artifacts_$multidev_name || true
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2023-09-25
* Updates to the [Sage install script](docs/Installing-Sage.md) to support running the script without prompting for input. Also adds automated test runs of the script on `ubuntu-latest` and `macos-latest` environments. ([#113](https://github.com/pantheon-systems/wordpress-composer-managed/pull/113))

### 2023-06-27
* Fixed a bug that failed to prevent a `composer.lock` file from being committed to the repository. ([#103](https://github.com/pantheon-systems/wordpress-composer-managed/pull/103))
* Removed the `upstream-require` script ([#105](https://github.com/pantheon-systems/wordpress-composer-managed/pull/105)). This is now available as a standalone Composer plugin: [`pantheon-systems/upstream-management`](https://packagist.org/packages/pantheon-systems/upstream-management)
Expand Down
37 changes: 36 additions & 1 deletion docs/Installing-Sage.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,43 @@ If you have all the above requirements, including a copy of the site locally, `c
composer install-sage
```

### Running the Script in Headless Mode

The script can also be run with no user interaction at all. Instead of answering the prompts in the script itself, you can pass information via environment variables before running the script. The following environment variables are available:

* **`SITENAME`** - The name of the site on Pantheon. This is the name that you would use to run Terminus commands like `terminus site:info` or `terminus wp`. This is not required if the script is being run from inside a site repo.
* **`SFTPUSER`** - The SFTP username for the site. This is not required if the script is being run from inside a site repo. This is the SFTP username that you can get from the Pantheon dashboard.
* **`SFTPHOST`** - The SFTP hostname for the site. This is not required if the script is being run from inside a site repo. This is the SFTP hostname that you can get from the Pantheon dashboard.
* **`SAGENAME`** - The name of the theme that you want to create. This is _required_.
* **`PHPVERSION`** - The PHP version that you want to use. This is optional and defaults to `8.0`. `8.3` is a valid option but will be updated to 8.2 until PHP 8.3 is available on Pantheon.
* **`SITEENV`** - The Pantheon environment that you want to use. This is optional and defaults to `dev`. This is the environment that you want to install the theme on. This can be `dev` or any valid multidev.
* **`CI`** - Whether the script is being run from a Continuous Integration (CI) environment. When this exists, additional time is given to allow simultaneous workflows to run and the script will not attempt to open the site in a browser. This is optional and defaults to `0` (`false`). If you want to enable CI-mode, set this to `1`. Setting the `CI` variable to `1` will also remove the confirmation prompts and prevent the script from restarting if no input is entered. _Only use this if you are sure you have the correct information_ as the script will run autonomously with whatever data it is able to fetch from Terminus based on the current site repository.

### Using environment variables

You can use any environment variables you want by simply `export`ing them in your shell or bash script before running the script. For example:

```bash
export SAGENAME="sage-theme"
export PHPVERSION="8.2"
export SITEENV="sage-1"
export SITENAME="my-site"
terminus multidev:create "$SITENAME".dev "$SITEENV"
git fetch --all
git checkout "$SITEENV"
composer install-sage
```

The above code will:

* Set the Sage theme name to `sage-theme`
* Set the PHP version to `8.2` in `pantheon.yml`
* Create a multidev environment named `sage-1`
* Check out the `sage-1` branch
* Run the Sage installation script on the `sage-1` multidev environment

## How it Works
The script does a lot of things and each step builds onto the last. You can look at the script in [`private/scripts/sage-theme-install.sh`](../private/scripts/sage-theme-install.sh) if you're interested in the precise commands that are being run. This overview will walk through each step.
The script does a lot of things and each step builds onto the last. You can look at the script in [`private/scripts/helpers.sh`](../private/scripts/helpers.sh) if you're interested in the precise commands that are being run. This overview will walk through each step.

### Check Login
The first thing the script will do is check to see if you are logged into Terminus with a `terminus whoami` command. If you are not logged in, the script will end and you will be prompted to log in first.
Expand Down
Loading

0 comments on commit 2b6c488

Please sign in to comment.