Skip to content

Commit

Permalink
[CICD-221] Refactor entrypoint.sh (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marksabbath authored Dec 2, 2022
1 parent 57f02d5 commit 58eb612
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 113 deletions.
30 changes: 30 additions & 0 deletions .changeset/swift-maps-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@wpengine/site-deploy": major
---

# Refactor the Main Script

Updates the main script to be more generic and allow the script and image to be used around other CI/CD Vendors.

In order to use this script, each CI/CD vendor will need to set the environment variables accordingly:

```sh
REMOTE_PATH # Default is empty
SRC_PATH # Default is the current directory
FLAGS # Default is -azvr --inplace --exclude=".*"
PHP_LINT # Default is "FALSE"
CACHE_CLEAR # Default is "TRUE"
SCRIPT # Default is empty
CICD_VENDOR # Default is "wpe-cicd"
```

Example of how to run this image:

```sh
docker run \
-e "WPE_SSHG_KEY_PRIVATE" \
--env-file ./.env \
-v <full_path_of_site>:/site \
--workdir=/site \
wpengine/site-deploy:latest
```
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ You can use this image to deploy a site from your local machine.
3. Create a `.env` file with the following variables, changing their values as needed.

```sh
INPUT_WPE_ENV=yourinstall # The target WP Engine install name.
GITHUB_REF=main # Inconsequential, but must be defined for now.
INPUT_REMOTE_PATH=
INPUT_SRC_PATH=.
INPUT_PHP_LINT=TRUE
INPUT_CACHE_CLEAR=TRUE
WPE_ENV=yourinstall # The target WP Engine install name.
GIT_REF=main # Inconsequential, but must be defined for now.
REMOTE_PATH=
SRC_PATH=.
PHP_LINT=TRUE
CACHE_CLEAR=TRUE
```

3. Set an environment variable with your private SSH key, replacing the key file name with your own.
Expand All @@ -41,7 +41,7 @@ export INPUT_WPE_SSHG_KEY_PRIVATE=`cat ~/.ssh/my_sshg_key_rsa`
-e "INPUT_WPE_SSHG_KEY_PRIVATE" \
-e "INPUT_FLAGS=-azvr --inplace --exclude=\".*\"" \
--env-file ./.env \
-v /path/to/your/install:/site \
-v /Users/marcos.schratzenstaller/workspace/wpe/qpdnpsx22q:/site \
--workdir=/site \
wpengine/site-deploy:latest
```
224 changes: 118 additions & 106 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,129 @@

set -e

: "${INPUT_WPE_SSHG_KEY_PRIVATE?Required secret not set.}"

#Alias logic for ENV names
if [[ -n ${INPUT_WPE_ENV} ]]; then
WPE_ENV_NAME="${INPUT_WPE_ENV}";
elif [[ -n ${INPUT_PRD_ENV} ]]; then
WPE_ENV_NAME="${INPUT_PRD_ENV}";
elif [[ -n ${INPUT_STG_ENV} ]]; then
WPE_ENV_NAME="${INPUT_STG_ENV}";
elif [[ -n ${INPUT_DEV_ENV} ]]; then
WPE_ENV_NAME="${INPUT_DEV_ENV}";
else echo "Failure: Missing environment variable..." && exit 1;
fi

echo "Deploying ${GITHUB_REF} to ${WPE_ENV_NAME} ..."

# Deploy Vars
WPE_SSH_HOST="$WPE_ENV_NAME.ssh.wpengine.net"
DIR_PATH="$INPUT_REMOTE_PATH"
SRC_PATH="$INPUT_SRC_PATH"

# Set up our user and path
WPE_SSH_USER="$WPE_ENV_NAME"@"$WPE_SSH_HOST"
WPE_FULL_HOST=wpe_gha+"$WPE_SSH_USER"
WPE_DESTINATION=wpe_gha+"$WPE_SSH_USER":sites/"$WPE_ENV_NAME"/"$DIR_PATH"


# Setup our SSH Connection & use keys
if [ ! -d "${HOME}"/.ssh ]; then
mkdir "${HOME}/.ssh"
SSH_PATH="${HOME}/.ssh"
mkdir "${SSH_PATH}/ctl/"
# Set Key Perms
chmod -R 700 "$SSH_PATH"
validate() {
# mandatory params
: WPE_SSHG_KEY_PRIVATE="${WPE_SSHG_KEY_PRIVATE:?'WPE_SSHG_KEY_PRIVATE variable missing from Repo or Workspace variables.'}"
# optional params
: REMOTE_PATH="${REMOTE_PATH:=""}"
: SRC_PATH="${SRC_PATH:="."}"
: FLAGS="${FLAGS:="-azvr --inplace --exclude=".*""}"
: PHP_LINT="${PHP_LINT:="FALSE"}"
: CACHE_CLEAR="${CACHE_CLEAR:="TRUE"}"
: SCRIPT="${SCRIPT:=""}"
: CICD_VENDOR="${CICD_VENDOR:="wpe-cicd"}"
}

setup_env() {
if [[ -n ${WPE_ENV} ]]; then
WPE_ENV_NAME="${WPE_ENV}";
elif [[ -n ${PRD_ENV} ]]; then
WPE_ENV_NAME="${PRD_ENV}";
elif [[ -n ${STG_ENV} ]]; then
WPE_ENV_NAME="${STG_ENV}";
elif [[ -n ${DEV_ENV} ]]; then
WPE_ENV_NAME="${DEV_ENV}";
else echo "Failure: Missing environment variable..." && exit 1;
fi

echo "Deploying your code to:"
echo "${WPE_ENV_NAME}"

WPE_SSH_HOST="${WPE_ENV_NAME}.ssh.wpengine.net"
DIR_PATH="${REMOTE_PATH}"

# Set up WPE user and path
WPE_SSH_USER="${WPE_ENV_NAME}"@"${WPE_SSH_HOST}"
WPE_FULL_HOST="${CICD_VENDOR}+$WPE_SSH_USER"
WPE_DESTINATION="${CICD_VENDOR}+${WPE_SSH_USER}:sites/${WPE_ENV_NAME}"/"${DIR_PATH}"
}

setup_ssh_dir() {
echo "setup ssh path"

if [ ! -d "${HOME}/.ssh" ]; then
mkdir "${HOME}/.ssh"
SSH_PATH="${HOME}/.ssh"
mkdir "${SSH_PATH}/ctl/"
# Set Key Perms
chmod -R 700 "$SSH_PATH"
else
SSH_PATH="${HOME}/.ssh"
echo "using established SSH KEY path...";
fi

#Copy secret keys to container
WPE_SSHG_KEY_PRIVATE_PATH="${SSH_PATH}/wpe_id_rsa"
umask 077 ; echo "${WPE_SSHG_KEY_PRIVATE}" > "${WPE_SSHG_KEY_PRIVATE_PATH}"
chmod 600 "${WPE_SSHG_KEY_PRIVATE_PATH}"
#establish knownhosts
KNOWN_HOSTS_PATH="${SSH_PATH}/known_hosts"
ssh-keyscan -t rsa "${WPE_SSH_HOST}" >> "${KNOWN_HOSTS_PATH}"
chmod 644 "${KNOWN_HOSTS_PATH}"
}

check_lint() {
if [ "${PHP_LINT^^}" == "TRUE" ]; then
echo "Begin PHP Linting."
find "$SRC_PATH"/ -name "*.php" -type f -print0 | while IFS= read -r -d '' file; do
php -l "$file"
status=$?
if [[ $status -ne 0 ]]; then
echo "FAILURE: Linting failed - $file :: $status" && exit 1
fi
done
echo "PHP Lint Successful! No errors detected!"
else
SSH_PATH="${HOME}/.ssh"
echo "using established SSH KEY path...";
fi

# Copy Secret Keys to container
WPE_SSHG_KEY_PRIVATE_PATH="$SSH_PATH/github_action"
echo "$INPUT_WPE_SSHG_KEY_PRIVATE" > "$WPE_SSHG_KEY_PRIVATE_PATH"
chmod 600 "$WPE_SSHG_KEY_PRIVATE_PATH"

#establish known hosts
KNOWN_HOSTS_PATH="$SSH_PATH/known_hosts"
ssh-keyscan -t rsa "$WPE_SSH_HOST" >> "$KNOWN_HOSTS_PATH"
chmod 644 "$KNOWN_HOSTS_PATH"

echo "prepping file perms..."
find "$SRC_PATH" -type d -exec chmod -R 775 {} \;
find "$SRC_PATH" -type f -exec chmod -R 664 {} \;
echo "file perms set..."

# pre deploy php lint
if [ "${INPUT_PHP_LINT^^}" == "TRUE" ]; then
echo "Begin PHP Linting."
find "$SRC_PATH"/ -name "*.php" -type f -print0 | while IFS= read -r -d '' file; do
php -l "$file"
status=$?
if [[ $status -ne 0 ]]; then
echo "FAILURE: Linting failed - $file :: $status" && exit 1
echo "Skipping PHP Linting."
fi
}

check_cache() {
if [ "${CACHE_CLEAR^^}" == "TRUE" ]; then
CACHE_CLEAR="&& wp --skip-plugins --skip-themes page-cache flush && wp --skip-plugins --skip-themes cdn-cache flush"
elif [ "${CACHE_CLEAR^^}" == "FALSE" ]; then
CACHE_CLEAR=""
else echo "CACHE_CLEAR value must be set as TRUE or FALSE only... Cache not cleared..." && exit 1;
fi
}

sync_files() {
#create multiplex connection
ssh -nNf -v -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlMaster=yes -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST"
echo "!!! MULTIPLEX SSH CONNECTION ESTABLISHED !!!"

# shellcheck disable=SC2086
rsync --rsh="ssh -p 22 -i ${WPE_SSHG_KEY_PRIVATE_PATH} -o StrictHostKeyChecking=no" ${FLAGS} --exclude-from='/exclude.txt' --chmod=D775,F664 ${SRC_PATH} "${WPE_DESTINATION}"

if [[ -n ${SCRIPT} || -n ${CACHE_CLEAR} ]]; then

if [[ -n ${SCRIPT} ]]; then
if ! ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "test -s sites/${WPE_ENV_NAME}/${SCRIPT}"; then
status=1
fi
done
echo "PHP Lint Successful! No errors detected!"
else
echo "Skipping PHP Linting."
fi

# post deploy script
if [[ -n ${INPUT_SCRIPT} ]]; then
SCRIPT="&& sh ${INPUT_SCRIPT}";
else
SCRIPT=""
fi

# post deploy cache clear
if [ "${INPUT_CACHE_CLEAR^^}" == "TRUE" ]; then
CACHE_CLEAR="&& wp --skip-plugins --skip-themes page-cache flush && wp --skip-plugins --skip-themes cdn-cache flush"
elif [ "${INPUT_CACHE_CLEAR^^}" == "FALSE" ]; then
CACHE_CLEAR=""
else echo "CACHE_CLEAR must be TRUE or FALSE only... Cache not cleared..." && exit 1;
fi

# Deploy via SSH
# setup master ssh connection
ssh -nNf -v -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlMaster=yes -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST"

echo "!!! MASTER SSH CONNECTION ESTABLISHED !!!"
#rsync
rsync --rsh="ssh -v -p 22 -i ${WPE_SSHG_KEY_PRIVATE_PATH} -o StrictHostKeyChecking=no -o 'ControlPath=$SSH_PATH/ctl/%C'" $INPUT_FLAGS --exclude-from='/exclude.txt' "$SRC_PATH" "$WPE_DESTINATION"

# post deploy script and cache clear
if [[ -n ${SCRIPT} || -n ${CACHE_CLEAR} ]]; then

if [[ -n ${SCRIPT} ]]; then
if ! ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "test -s sites/${WPE_ENV_NAME}/${INPUT_SCRIPT}"; then
status=1
fi

if [[ $status -ne 0 && -f ${INPUT_SCRIPT} ]]; then
ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "mkdir -p sites/${WPE_ENV_NAME}/$(dirname "${INPUT_SCRIPT}")"
if [[ $status -ne 0 && -f ${SCRIPT} ]]; then
ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "mkdir -p sites/${WPE_ENV_NAME}/$(dirname "${SCRIPT}")"

rsync --rsh="ssh -v -p 22 -i ${WPE_SSHG_KEY_PRIVATE_PATH} -o StrictHostKeyChecking=no -o 'ControlPath=$SSH_PATH/ctl/%C'" "${SCRIPT}" "${CICD_VENDOR}+$WPE_SSH_USER:sites/$WPE_ENV_NAME/$(dirname "${SCRIPT}")"
fi
fi

rsync --rsh="ssh -v -p 22 -i ${WPE_SSHG_KEY_PRIVATE_PATH} -o StrictHostKeyChecking=no -o 'ControlPath=$SSH_PATH/ctl/%C'" "${INPUT_SCRIPT}" "wpe_gha+$WPE_SSH_USER:sites/$WPE_ENV_NAME/$(dirname "${INPUT_SCRIPT}")"
if [[ -n ${SCRIPT} ]]; then
SCRIPT="&& bash ${SCRIPT}"
fi
fi

ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "cd sites/${WPE_ENV_NAME} ${SCRIPT} ${CACHE_CLEAR}"
fi
ssh -v -p 22 -i "${WPE_SSHG_KEY_PRIVATE_PATH}" -o StrictHostKeyChecking=no -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST" "cd sites/${WPE_ENV_NAME} ${SCRIPT} ${CACHE_CLEAR}"
fi

#close master ssh
ssh -O exit -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST"
#close multiplex connection
ssh -O exit -o ControlPath="$SSH_PATH/ctl/%C" "$WPE_FULL_HOST"
echo "closing ssh connection..."
}

echo "SUCCESS: Your code has been deployed to WP Engine!"
validate
setup_env
setup_ssh_dir
check_lint
check_cache
sync_files

0 comments on commit 58eb612

Please sign in to comment.