Skip to content

Commit

Permalink
using our new db script, updating docs and tasks to use (#237)
Browse files Browse the repository at this point in the history
* using our new db script, updating docs and tasks to use

* now that wp env is checked in, add to deploy exclude

* minor spacing
  • Loading branch information
ccorda authored Oct 27, 2022
1 parent b4ca258 commit 4b4549c
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 232 deletions.
17 changes: 11 additions & 6 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
"command": "chmod -R 777 wordpress/wp-content/themes/headless/acf-json"
},
{
"label": "Wordpress DB Export",
"label": "Wordpress DB Export Production",
"type": "shell",
"command": "cd wordpress && bash _build/db.sh export"
"command": "cd wordpress && bash _build/db.sh export production"
},
{
"label": "Wordpress DB Export Staging",
"type": "shell",
"command": "cd wordpress && bash _build/db.sh export staging"
},
{
"label": "Wordpress DB Import",
Expand All @@ -46,22 +51,22 @@
{
"label": "Wordpress Composer Install",
"type": "shell",
"command": "cd wordpress && composer install"
"command": "cd wordpress && composer install --ignore-platform-reqs"
},
{
"label": "Wordpress Composer Update",
"type": "shell",
"command": "cd wordpress && composer update"
"command": "cd wordpress && composer update --ignore-platform-reqs"
},
{
"label": "WP Engine Deploy Production",
"type": "shell",
"command": "cd wordpress && sh _build/deploy.sh production"
"command": "cd wordpress && bash _build/deploy.sh production"
},
{
"label": "WP Engine Deploy Staging",
"type": "shell",
"command": "cd wordpress && sh _build/deploy.sh staging"
"command": "cd wordpress && bash _build/deploy.sh staging"
}
]
}
16 changes: 9 additions & 7 deletions docs/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ Inside of the WordPress folder is a docker-commpose.yml file that will spin up a

The VS Code task `WordPress Docker Recreate` can be run from the task runner as an alias to get your container started.

Inside of the WordPress folder you'll find a .env.sample. You'll want to copy that to .env, and setup a unique database name for the project.
Inside of the WordPress folder you'll find a .env. You'll want to configure your name for the project, and any WP Engine deployment targets. This variables are used in the db.sh script to manage imports/exports of your database, and deploy.sh which deploys to WP Engine.

## Exporting and Importing database

If you want to pull down your DB from production to mirror locally, there is a DB import script accessible via the "Wordpress DB Import" task that will run and import the latest version of a database file that exists inside of `wordpress/_data`. After importing, it will run a `local.sql` file that resets a few variables and resets the admin login to `admin:password` for ease of local dev. You can customize local.sql to make additional overides if you want that are project specific.

Your wordpress admin will be viewable at /wp-login.php, and you can use the username: `admin` and password: `password`.

To help with exports, we also have tasks "Wordpress DB Export" for both production and staging that will save the latest DB to the \_data folder. You will need a local version of [WP CLI](https://wp-cli.org/) in order for this to work. You can quickly install by running `brew install wp-cli` if you use use homebrew.

## Managing WP deps with Composer

Expand All @@ -31,12 +39,6 @@ When you want to update wordpress core, you need to update the `wordpress/docker

Also update `composer.json` so that non-docker users get the updated WP version.

## WordPress DB

If you want to pull down your DB from production to mirror locally, there is a DB import script that will run and import the latest version of a database file that exists inside of `wordpress/_data`. After importing, it will run a `local.sql` file that resets a few variables and resets the admin login to `admin:password` for ease of local dev. You can customize local.sql to make additional ovverides if you want that are project specific.

This script can be run via the VS Code task `Wordpress DB Import`

## Theme

The `headless` theme is configured to make some light adjustments to WordPress to support headless dev.
Expand Down
2 changes: 1 addition & 1 deletion website/src/styles/_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// spacing
// we set spacing in 10px increments, allowing up to 10, e.g. p-10.
// we also enable negative spacers
$enable-negative-margins: true;
$spacer-px: 10px;
$spacer: rem-calc(10px);
$spacers: (
Expand All @@ -24,7 +25,6 @@ $spacers: (
);
$section-padding: 80px;
$section-padding-mobile: 40px;
$enable-negative-margins: true;

// type
$font-sans: 'Source Sans Pro', sans-serif;
Expand Down
4 changes: 2 additions & 2 deletions website/src/styles/shared/_type.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// As much as possible, set type by customizing the default bootstrap variables in _variables.scss
// As much as possible, set type by customizing the default bootstrap variables in _custom.scss
body {
font-size: rem-calc(24px);
font-size: rem-calc(16px);
line-height: 130%;
}

Expand Down
22 changes: 22 additions & 0 deletions wordpress/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# BY DEFAULT THIS FILE WE BE COMMITTED TO SOURCE CONTROL
# IF YOU WANT TO STORE SENSITIVE DATA, THEN BE SURE AND GITIGNORE FIRST

# This environment variable is automatically read by Docker and sets
# the project name which is useful when starting new projects based
# on this template. We also use this variable to set the wordpress database
# name in docker-compose.yml and _build/db.sh

# Should match your docker project name locally
COMPOSE_PROJECT_NAME=bubsnext

# For WP Engine, the following are used to connect via SSH for deployment
# and DB exporting

# WPE environment names
COMPOSE_WPE_PRODUCTION=bubsnext
COMPOSE_WPE_STAGING=bubsnexts
COMPOSE_WPE_DEVELOPMENT=bubsnextd

# These are used for the forced deploys to WPE
GIT_EMAIL="[email protected]"
GIT_NAME="Bubs Deploy"
10 changes: 0 additions & 10 deletions wordpress/.env.sample

This file was deleted.

3 changes: 3 additions & 0 deletions wordpress/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ composer.lock

#### JTS RULES

# checkin WP env (no secrets, local configs)
!.env

# development-environment
tmp/
wp-content/themes/timber/dev/
Expand Down
70 changes: 2 additions & 68 deletions wordpress/README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,2 @@
# BUBS - A Wordpress Development Environment

Bubs is Patronage's open-source Wordpress development environment.

## Getting Started (Docker)

### Prerequisites

You should have the following installed and ready in your development environment:

- `composer`
- `Node.js`
- `nvm`
- `Yarn Package Manager`
- `Sequel Ace` or a SQL GUI to import site database

### 1. Install Docker

Install Docker on your computer. [Here's a link to the Desktop Installer](https://www.docker.com/products/docker-desktop).

### 2. Checkout this repo and prepare local dependencies

From the /wordpress directory, run the following in your terminal:

- `composer install`
- `yarn`

### 3. Start Docker and development environment

From the /wordpress directory, run the following in your terminal:

- `docker-compose up`

### 4. Get Started

- Open a browser tab to (http://localhost:8000/wp-login.php)[http://localhost:8000/wp-login.php].

### 5. Wrapping up development

- Open Docker Desktop, and click the "stop" button to turn off Docker and make your computer a little faster and less warm:

![image](https://user-images.githubusercontent.com/525011/77448037-c5573380-6dc6-11ea-8bdd-e9d4025d671d.png)

### 6. Updating SQL database with newer SQL

First save your new DB file (.sql, .zip, or .gz) to the `_data` folder. to check the option to drop tables. Then run `yarn db` to import.

Alternatively, you can use Sequel Pro or another client and import manually.

To connect from Sequel Pro to explore the database, use the following settings:

Host: `127.0.0.1`
User: `root`
Pass: `somewordpress`
Port: `3307`

Docker is configured to persist the database, and each project will use it's own DB name.

### Deploying

The `_build` folder has our deploy scripts:

- to manually deploy to staging from your current branch, run `./_build/deploy.sh staging`
- to manually deploy to production from your current branch, run `./_build/deploy.sh production`

### Based on Bubs

This project is based on [Bubs](https://github.com/patronage/bubs-next/) by [Patronage](http://www.patronage.org/).
See these docs for instructions on getting WordPress running locally:
https://github.com/patronage/bubs-next/blob/main/docs/wordpress.md
3 changes: 2 additions & 1 deletion wordpress/_build/.deploy_exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ _build/
_data/
_init/
.editorconfig
.env
.gitignore
.npmrc
.nvmrc
Expand All @@ -19,4 +20,4 @@ wp-content/plugins/debug-bar/*
wp-content/plugins/debug-bar-timber/*
wp-content/plugins/query-monitor/*
wp-content/plugins/user-switching/*
wp-content/plugins/wp-graphiql/*
wp-content/plugins/wp-graphiql/*
68 changes: 62 additions & 6 deletions wordpress/_build/db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,41 @@
## env export, with unset at end of script
if [ -f ".env" ]; then
export $(grep -v '^#' .env | xargs)
else
echo ".env file required, please copy from the .env.sample"
exit 1;
fi

## Per Project Variables -- CUSTOMIZE THESE FIRST IN .ENV
WORDPRESS_DB_USER="root"
WORDPRESS_DB_PASSWORD="somewordpress"
WORDPRESS_DB_NAME=${COMPOSE_PROJECT_NAME:-wordpress}
DB_CONTAINER="${COMPOSE_PROJECT_NAME:-wordpress}_db_1"
PRODUCTION_SSH="${COMPOSE_WPE_PRODUCTION}@${COMPOSE_WPE_PRODUCTION}.ssh.wpengine.net"
STAGING_SSH="${COMPOSE_WPE_STAGING}@${COMPOSE_WPE_STAGING}.ssh.wpengine.net"
DEVELOPMENT_SSH="${COMPOSE_WPE_DEVELOPMENT}@${COMPOSE_WPE_STAGING}.ssh.wpengine.net"

# handle script errors, exit and kick you to working branch
function error_exit {
echo "$1" 1>&2
echo "Aborting export attempt"
exit 1
}

function db_import() {
# get the docker container
containers=$(docker ps --format "{{.Image}}|{{.Names}}")
for str in ${containers[@]}; do
if [[ $str == *${COMPOSE_PROJECT_NAME}* ]] && [[ $str == *"mariadb"* ]]; then
DB_CONTAINER=${str#*|}
fi
done

if [ -z ${DB_CONTAINER+x} ]; then
error_exit "Couldn't find a DB container for '$COMPOSE_PROJECT_NAME', please make sure it is running."
else
echo "DB container '$DB_CONTAINER' detected; proceeding with import";
fi

sql=`ls -Art _data/* | tail -n 1`
echo $sql
ext=${sql##*.}
Expand Down Expand Up @@ -42,16 +68,46 @@ function db_import() {
}

function db_export() {
echo "$PRODUCTION_SSH";
wp db export --add-drop-table --ssh=$PRODUCTION_SSH - | gzip > _data/$(date +'%Y-%m-%d-%H-%M-%S').sql.gz
echo "export complete";
local TARGET=${1}

if [ "$TARGET" = "staging" ]; then
SSH_TARGET=$STAGING_SSH
elif [ "$TARGET" = "development" ]; then
SSH_TARGET=$DEVELOPMENT_SSH
else
SSH_TARGET=$PRODUCTION_SSH
fi

echo "connecting to $SSH_TARGET"

status=$(ssh -o BatchMode=yes -o ConnectTimeout=5 $SSH_TARGET echo ok 2>&1)

if [[ $status == ok ]] ; then
echo "auth ok, proceeding with export"
if ! command -v wp &> /dev/null
then
error_exit "'wp' command could not be found. WP CLI local install required to export DB"
else
mkdir -p _data
filename=$(date +'%Y-%m-%d-%H-%M-%S').sql.zip
wp db export --add-drop-table --ssh=$SSH_TARGET - | gzip > _data/$(date +'%Y-%m-%d-%H-%M-%S').sql.gz
echo "export complete";
fi
elif [[ $status == "Permission denied"* ]] ; then
echo no_auth
elif [[ $status == "Host key verification failed"* ]] ; then
echo "host key not yet verified, please run: ssh $SSH_TARGET then try again"
else
echo "SSH couldn't connect, please check that environments are defined in your .env, and your SSH key is added to WP Engine"
fi
}

CALLED_FUNCTION=${1}
TARGET=${2}

if [ "$CALLED_FUNCTION" = "export" ]; then
echo "running DB export script"
db_export
echo "Running DB export for $TARGET"
db_export $TARGET
elif [ "$CALLED_FUNCTION" = "import" ]; then
echo "running DB import script"
db_import
Expand Down
Loading

1 comment on commit 4b4549c

@vercel
Copy link

@vercel vercel bot commented on 4b4549c Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.