Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PHP version to 8.1 and other updates to helpers.sh #131

Merged
merged 18 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-74.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 7.4
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 8
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-80.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 8.0
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-81.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 8.1
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-82.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 8.2
4 changes: 4 additions & 0 deletions .github/fixtures/pantheon-83.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Put overrides to your pantheon.upstream.yml file here.
# For more information, see: https://pantheon.io/docs/pantheon-yml/
api_version: 1
php_version: 8.3
78 changes: 78 additions & 0 deletions .github/tests/1-test-update-php.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bats

custom_setup() {
local version=$1
# Print debugging information
echo "BATS_TEST_DIRNAME: $BATS_TEST_DIRNAME"
echo "Current directory: $(pwd)"

# Check if the .github directory exists
if [ ! -d ".github" ]; then
echo "Error: .github directory not found in $(pwd)"
exit 1
fi

if [ ! -f "pantheon.upstream.yml" ]; then
echo "It doesn't look like you are in an upstream repository. Check the current directory: $(pwd)"
exit 1
fi

# Copy the fixture file to pantheon.yml before each test
cp ".github/fixtures/pantheon-${version}.yml" pantheon.yml

if [ ! -f "pantheon.yml" ]; then
echo "Failed to copy the fixture file to $(pwd)/pantheon.yml"
else
cat pantheon.yml
fi
}

teardown() {
# Clean up by removing the pantheon.yml file after each test
rm -f pantheon.yml
}

@test "Update PHP version 7.4 to 8.1" {
custom_setup "74"
run bash private/scripts/helpers.sh update_php
echo "$output"
[ "$status" -eq 0 ]
run grep -q "php_version: 8.1" pantheon.yml
[ "$status" -eq 0 ]
}

@test "Update PHP version 8.0 to 8.1" {
custom_setup "80"
run bash private/scripts/helpers.sh update_php
echo "$output"
[ "$status" -eq 0 ]
run grep -q "php_version: 8.1" pantheon.yml
[ "$status" -eq 0 ]
}

@test "Keep PHP version 8.1" {
custom_setup "81"
run bash private/scripts/helpers.sh update_php
echo "$output"
[ "$status" -eq 0 ]
run grep -q "php_version: 8.1" pantheon.yml
[ "$status" -eq 0 ]
}

@test "Keep PHP version 8.2" {
custom_setup "82"
run bash private/scripts/helpers.sh update_php
echo "$output"
[ "$status" -eq 0 ]
run grep -q "php_version: 8.2" pantheon.yml
[ "$status" -eq 0 ]
}

@test "Keep PHP version 8.3" {
custom_setup "83"
run bash private/scripts/helpers.sh update_php
echo "$output"
[ "$status" -eq 0 ]
run grep -q "php_version: 8.3" pantheon.yml
[ "$status" -eq 0 ]
}
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
- synchronize
- ready_for_review

permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -46,3 +49,12 @@ jobs:

- name: Run tests
run: composer test

- name: Install bats
uses: bats-core/[email protected]

- name: Test Helper functions
env:
CI: 1
run: |
bats -p -t .github/tests
2 changes: 1 addition & 1 deletion .github/workflows/sage-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:
# Fetch the genre name from the Genrenator API
SAGENAME=$(curl -s https://binaryjazz.us/wp-json/genrenator/v1/genre/)
# Replace spaces with hyphens and remove all non-alphanumeric characters except hyphens
SAGENAME=$(echo "$SAGENAME" | tr ' ' '-' | sed 's/[^a-zA-Z0-9\-]//g')
SAGENAME=$(echo "$SAGENAME" | tr ' ' '-' | tr -cd 'a-zA-Z0-9-')
echo "SAGENAME=$SAGENAME" >> $GITHUB_ENV
- name: Run Sage Install Script
env:
Expand Down
91 changes: 47 additions & 44 deletions private/scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sitename="${SITENAME:-}"
sftpuser="${SFTPUSER:-}"
sftphost="${SFTPHOST:-}"
sagename="${SAGENAME:-}"
phpversion="${PHPVERSION:-8.0}"
phpversion="${PHPVERSION:-8.1}"
is_ci="${CI:-0}"
siteenv="${SITEENV:-dev}"
if [ "$siteenv" == "dev" ]; then
Expand All @@ -38,7 +38,8 @@ function main() {
help_msg="Usage: bash ./private/scripts/helpers.sh <command>
Available commands:
install_sage: Install Sage.
maybe_create_symlinks: Create a symlinks to WordPress files, if they don't already exist."
maybe_create_symlinks: Create a symlinks to WordPress files, if they don't already exist.
update_php: Updates PHP version to 8.1 if it's not already set at 8.1 or higher."

if [ -z "$1" ]; then
echo "${red}No command specified.${normal}"
Expand All @@ -52,7 +53,7 @@ function main() {
fi

# Check for a valid command.
if [ "$1" != "install_sage" ] && [ "$1" != "maybe_create_symlinks" ]; then
if [ "$1" != "install_sage" ] && [ "$1" != "maybe_create_symlinks" ] && [ "$1" != "update_php" ]; then
echo "${red}Invalid command specified.${normal}"
echo "${help_msg}"
exit 1;
Expand Down Expand Up @@ -233,53 +234,52 @@ get_field() {
echo "$2" | awk -v field="$1" '$1 == field { print $2 }'
}

# Update to PHP 8.0
# Update to PHP 8.1 or higher
function update_php() {
if [ "$phpversion" == "8" ]; then
phpversion="8.0"
fi
if [ "$phpversion" != "8.0" ]; then
echo "${yellow}You've specified PHP version ${phpversion}. The default is 8.0, but we'll use the version you asked for.${normal}"
if [ "$phpversion" == "8.3" ]; then
echo "${yellow}PHP 8.3 is not yet supported. Using 8.2 instead.${normal}"
phpversion="8.2"
fi
# Check if $phpversion is < 8.0.
if [ "$(echo "$phpversion < 8.0" | bc)" -eq 1 ]; then
echo "${red}PHP version must be 8.0 or greater. Exiting here.${normal}"
exit 1;
fi
# If an old PHP version was passed into the script from the outside, fall back to 8.1.
if [ "$phpversion" == "8" ] || [ "$phpversion" == "8.0" ]; then
phpversion="8.1"
fi

# Check if $phpversion is < 8.1. We shouldn't get here because we just updated $phpversion (which is passed from the environment), so if we are here, it's a problem.
if [ "$(echo "$phpversion < 8.1" | bc)" -eq 1 ]; then
echo "${red}PHP version must be 8.1 or greater. Exiting here.${normal}"
exit 1
fi

echo ""
echo "${yellow}Updating PHP version to ${phpversion}.${normal}"
echo "${yellow}Checking PHP version and maybe updating to ${phpversion}.${normal}"

# Check for pantheon.yml file.
if [ ! -f "pantheon.yml" ]; then
echo "${red}No pantheon.yml file found. Exiting here.${normal}"
echo "Make sure you are inside a valid Pantheon repository."
exit 1;
exit 1
fi

# Testing for any version of PHP 8.x and/or PHP 7.4.
phpAlreadyVersion8=$(grep -c "php_version: 8." < pantheon.yml)
phpDeclaredInFile=$(grep -c "php_version: 7.4" < pantheon.yml)
# Check the current PHP version declared in pantheon.yml
currentPhpVersion=$(awk '/php_version:/{print $2}' pantheon.yml)

# Only alter if not already PHP 8.x.
if [ "$phpAlreadyVersion8" -eq 0 ]; then
# Test for PHP version declartion already in pantheon.yml.
if [ ! "$phpDeclaredInFile" -eq 0 ]; then
# Update version to 8.x.
sed -i '' "s/7.4/${phpversion}/" pantheon.yml
else
# Add full PHP version declaration to pantheon.yml.
echo "" >> pantheon.yml
echo "php_version: ${phpversion}" >> pantheon.yml
fi
git diff HEAD~1 HEAD -- pantheon.yml
git commit -am "[Sage Install] Update PHP version to ${phpversion}"
if [ -z "$currentPhpVersion" ]; then
# Add the PHP version declaration if not present.
echo "" >> pantheon.yml
echo "php_version: ${phpversion}" >> pantheon.yml
elif [ "$(echo "$currentPhpVersion < 8.1" | bc)" -eq 1 ]; then
# Update the PHP version declaration if it's less than 8.1.
sed -i.bak "s/php_version: [0-9.]*/php_version: ${phpversion}/" pantheon.yml && rm pantheon.yml.bak
else
# We've got a good PHP version, so we can bail here.
echo "${green}PHP version is already ${currentPhpVersion} which is >= 8.1.${normal}"
exit 0
fi

# If we're in CI, don't run the push actions. Note: if you're running Bats tests locally, you should pass CI=1 before running the tests.
if [ "$is_ci" -eq 0 ]; then
git add pantheon.yml
git commit -m "[Sage Install] Update PHP version to ${phpversion}"
git push origin "$branch"
else
echo "${green}PHP version is already 8.x.${normal}"
echo "${yellow}CI detected. Skipping Git operations. PHP updated to ${phpversion}.${normal}"
fi
}

Expand Down Expand Up @@ -476,24 +476,27 @@ function clean_up() {

# If the site is multisite, we'll need to enable the theme so we can activate it.
echo "${yellow}Checking if this is a multisite.${normal}"
if terminus wp -- "$sitename"."$siteenv" config is-true MULTISITE; then
if terminus wp -- "$sitename"."$siteenv" config is-true MULTISITE > /dev/null 2>&1; then
echo "${yellow}Site is multisite.${normal}"
terminus wp -- "$sitename"."$siteenv" theme enable "$sagename"
fi

# List the themes.
terminus wp -- "$sitename"."$siteenv" theme list
# Get the themes.
themelist=$(terminus wp -- "$sitename"."$siteenv" theme list --format=csv | tr -d '\n' | tr -d ' ')

# Activate the new theme
echo "${yellow}Activating the ${sagename} theme.${normal}"
if ! terminus wp -- "$sitename"."$siteenv" theme activate "$sagename"; then
echo "${red}Theme activation failed. Exiting here.${normal}"
if ! echo "$themelist" | grep -q "$sagename"; then
echo "${red}Theme $sagename not found in the theme list. Exiting here.${normal}"
terminus wp -- "$sitename"."$siteenv" theme list
echo "Check the theme list above. If the theme you created is not listed, it's possible that the deploy has not completed. You can try again in a few minutes using the following command:"
echo "terminus wp -- $sitename.dev theme activate $sagename"
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved
echo "Once you do this, you will need to open the site to generate the requisite files and then commit them in SFTP mode."
exit 1;
fi

# Activate the new theme
echo "${yellow}Activating the ${sagename} theme.${normal}"
terminus wp -- "$sitename"."$siteenv" theme activate "$sagename"

# If this is a CI environment, stop here.
if [ "$is_ci" == 1 ]; then
echo "${yellow}CI detected. All done here.${normal} 🍵"
Expand Down
Loading