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

Feature/add more flexibility #413

Merged
merged 29 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
77c4063
fix deprecation warning, support a more modern layout
dustinrue Jul 5, 2024
cb6d5c9
fix typo in readme
dustinrue Jul 5, 2024
80092f3
improved init and build systems
dustinrue Jul 5, 2024
147c729
attempt to make linter happy
dustinrue Jul 5, 2024
bbaae33
fix description for create-payload
dustinrue Aug 9, 2024
a50a15d
monorepo instead of modern, common instead of legacy, improve init pr…
dustinrue Aug 9, 2024
686b4e4
convenience method for updating the composer lock file
dustinrue Aug 9, 2024
83b03a4
ability to package into a container image, updated readme
dustinrue Aug 9, 2024
14e3cdc
Remove directory slash detection; properly name templateInitPath vari…
tlovett1 Aug 15, 2024
da5a17b
Merge branch 'feature/add-more-flexibility' into feature/add-more-fle…
tlovett1 Aug 15, 2024
71547ff
Change standard layout to wpparent; remove create-payload and support…
tlovett1 Aug 15, 2024
53698e1
Fix typo
tlovett1 Aug 15, 2024
81dc4c4
Use wpparent
tlovett1 Aug 15, 2024
2741ae7
Remove build_script_path as it's not needed anymore
tlovett1 Aug 15, 2024
e31e262
Remove deploy_script_path
tlovett1 Aug 15, 2024
ea8fc46
Rename build-setup.sh to scripts.sh
tlovett1 Aug 15, 2024
345a06e
Now we get the git branch in node so we can pass the proper environme…
tlovett1 Aug 15, 2024
470ae80
Add skip ci option
tlovett1 Aug 19, 2024
fc3f2c9
Remove unneeded default variable
tlovett1 Aug 19, 2024
4356cdd
Remove duplicate composer installs; fix project layout checking logic
tlovett1 Aug 19, 2024
f848f27
Fix variables
tlovett1 Aug 19, 2024
800b38d
Doc updates
tlovett1 Aug 19, 2024
c6700d8
Doc updates
tlovett1 Aug 19, 2024
47aef83
Fix message
tlovett1 Aug 19, 2024
99fafc7
Only pushd and popd in wpparent layout
tlovett1 Aug 20, 2024
9a76fed
Merge pull request #421 from 10up/feature/add-more-flexibility-tl
tlovett1 Aug 20, 2024
a4499e8
keep consistent with the project on tabs vs spaces
dustinrue Aug 20, 2024
61ba982
maybe support shells other than bash
dustinrue Aug 20, 2024
8587947
Add changeset
tlovett1 Aug 21, 2024
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
12 changes: 9 additions & 3 deletions packages/toolkit/PROJECTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The project subcommand provides a variety of utlities for creating, building, an
List of commands:

```bash
10up-toolkit project init [<path>] [--template=<Git repository>] [--name=<Project Name>] [--confirm] [--skip-composer]
10up-toolkit project init [--path=<path>] [--template=<Git repository>] [--name=<Project Name>] [--confirm] [--skip-composer]
```

`init` creates a project. You can optionally provide it a number of parameters or answer the prompts. If no path is provided, it will initialize the project in the current directory. You will be prompted to choose a template e.g. [WP Scaffold](https://github.com/10up/wp-scaffold). Init will automatically search and replace prefixes using the project name you provide.
Expand All @@ -85,15 +85,21 @@ List of commands:
`build` simply executes your `scripts/build.sh` file (or other path you specify). `build` will be executed before deploying files.

```bash
10up-toolkit project create-payload <branch>
10up-toolkit project create-payload
```

This command creates a payload directory of the built project (including WordPress) for deployment. Engineers likely won't need to run this command themselves as GitLab does it automatically. You must provide a branch that corresponds to an environment in `.tenup.yml`.
This command performs a simulated CI/CD build and creates a payload directory of the built project (including WordPress core). Useful for validating how a build will behave in CI/CD for deployment. Engineers likely won't need to run this command themselves as CI/CD does it automatically.

```bash
10up-toolkit project generate-ci [--confirm] [--path=<path>]
```

This command generates necessary CI files. For GitLab, this would be `.gitlab-ci.yml`. Right now this only supports GitLab but we will add support for GitHub in the future.

```bash
10up-toolkit project update-composer
```

Convenience function to update all dependencies in all found composer.json files. Also generates updated lock files.

**Note that generating CI files is currently in alpha and may require manual editing to fix issues.**
2 changes: 1 addition & 1 deletion packages/toolkit/project/gitlab/.gitlab-ci.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build_plugins_and_themes:
stage: build
script:
- nvm install 18
- npx 10up-toolkit project create-payload $CI_COMMIT_REF_NAME
- npx 10up-toolkit project create-payload
artifacts:
paths:
- payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deploy_production:
MULTI_DEV_ENVIRONMENT: "{{branch}}"
WORDPRESS_VERSION: {{wordpress_version}}
GIT_URL: {{deploy_to}}
EXCLUDES: {{deploy_file_excludes}}
EXCLUDES: {{rsync_file_excludes}}
allow_failure: false
only:
refs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deploy_{{branch}}:
MULTI_DEV_ENVIRONMENT: "{{branch}}"
WORDPRESS_VERSION: {{wordpress_version}}
GIT_URL: {{deploy_to}}
EXCLUDES: {{deploy_file_excludes}}
EXCLUDES: {{rsync_file_excludes}}
allow_failure: false
only:
refs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deploy_production:
variables:
DESTINATION: {{deploy_to}}
SUBDIR: {{deploy_to_subdir}}
EXCLUDES: {{deploy_file_excludes}}
EXCLUDES: {{rsync_file_excludes}}
only:
refs:
- {{branch}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deploy_{{branch}}:
variables:
DESTINATION: {{deploy_to}}
SUBDIR: {{deploy_to_subdir}}
EXCLUDES: {{deploy_file_excludes}}
EXCLUDES: {{rsync_file_excludes}}
only:
refs:
- {{branch}}
26 changes: 26 additions & 0 deletions packages/toolkit/project/local/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
payload
build/vendor
build/composer.lock
build/composer.json
/vendor
wordpress/.env
wordpress/*php
wordpress/wp-content/uploads
wordpress/wp-includes
wordpress/wp-admin
wordpress/license.txt
wordpress/readme.html
!wordpress/wp-config.php
docker-compose.override.yml
.config.json
wordpress/wp-content/advanced-cache.php
wordpress/wp-content/mu-plugins/10up-experience.php
wordpress/wp-content/mu-plugins/10up-experience/
wordpress/wp-content/mu-plugins/batcache.php
wordpress/wp-content/mu-plugins/batcache/
wordpress/wp-content/mu-plugins/redis-cache.php
wordpress/wp-content/mu-plugins/redis-cache/
wordpress/wp-content/mu-plugins/s3-uploads.php
wordpress/wp-content/mu-plugins/s3-uploads/
wordpress/wp-content/object-cache.php
.lock
21 changes: 17 additions & 4 deletions packages/toolkit/project/local/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Add builds scripts here. This script will be run from the root of your project (same directory as .tenup.yml).
#!/usr/bin/env bash -l
# NOTE: you should keep the above line, at line 1. Only modify if you know what you are doing.

nvm install
node -v
npm install
# You should use npx 10up-toolkit build to build your project. When you do, there are a number of things
# you get "for free" including nvm handling and the basics of building are handled automatically. You
# only need to provide additional build routines if the default build system doesn't quite get things
# right. Adding build scripts is easy. Create as many .sh files as you need in this scripts directory.

# Here is an example script you can use to get you started It assumes you are using a "modern" layout.

# change directories to your theme or plugin
pushd .

# run your build commands
echo "Hello World!"

# return to where we were so the next build script starts off from the same place
popd
7 changes: 7 additions & 0 deletions packages/toolkit/scripts/project/bash/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:latest

USER root
WORKDIR /var/www/html
COPY payload .
RUN mkdir wp-content/uploads && \
chown 33:33 wp-content/uploads
244 changes: 238 additions & 6 deletions packages/toolkit/scripts/project/bash/build-setup.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,245 @@
#!/bin/bash
#!/usr/bin/env bash -l

maybe_init_nvm() {
SHARE_DIR="$(dirname "$(realpath "$0")")"
# Various tasks to determine some things like what kind of project is this
# such as standard, wp-content rooted...something else?
function build:preflight {
PROJECT_TYPE="standard"
# Check for a default, standard layout that has a wordpress directory
if [ -d wordpress ] && [ -d build ]; then # this is probably a standard setup
echo "Detected standard WordPress repository layout"

if [ $(find . -name .nvmrc | wc -l) -gt 0 ] && [ ! -f $NVM_DIR/nvm.sh ]; then
echo "This project requires nvm. Please install nvm and try again"
WORDPRESS_BUILD_ROOT="wordpress/wp-content"
return
fi

# Check for a wp-content rooted style repository
if [ -d plugins ] || [ -d themes ]; then # this is probably a wp-root repo
echo "Detected wp-content rooted WordPress repository layout"
PROJECT_TYPE="wpcontent"
WORDPRESS_BUILD_ROOT="."
return
fi

}


# Routine to determine what version of WordPress to install
function build:version {

WORDPRESS_VERSION="latest"
if [ ${CI:-false} = "true" ]; then
if [ -z ${WORDPRESS_VERSION} ]; then
WORDPRESS_VERSION="latest"
fi
else
GIT_BRANCH=$(git branch --format='%(refname:short)' --show-current)
GIT_BRANCH_SLUG=$(utilities:create-gitlab-slug ${GIT_BRANCH})
ENVIRONMENT=$(yq eval '.environments | to_entries[] | select(.value.branch == "'${GIT_BRANCH_SLUG}'") | .key' ${TENUP_CI_FILE})

if [ ${ENVIRONMENT:-null} != "null" ]; then
WORDPRESS_VERSION=$(yq '.environments.'${ENVIRONMENT}'.wordpress_version' ${TENUP_CI_FILE})
fi
fi

if [ "${WORDPRESS_VERSION}" == "latest" ]; then
WORDPRESS_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq '.offers[0].current' | tr -d '"')
fi

echo ${WORDPRESS_VERSION}
}

function build:install {
build:preflight

# we use command in case wp-cli is installed as an alias
if [[ -z $(command -v wp) ]]; then
echo "wp-cli is not installed or in your path but it is required"
exit 1
fi

local WORDPRESS_VERSION=$(build:version)
echo "Installing WordPress version: ${WORDPRESS_VERSION}"

if [ ${PROJECT_TYPE} = "standard" ]; then
mkdir -p wordpress/wp-content
pushd wordpress
else
mkdir -p payload/wp-content
pushd payload
fi
wp core download --version=${WORDPRESS_VERSION} --skip-content --force
popd

}

function build:main {
set -eo pipefail

build:preflight

# don't call this script directly
if [ $(shopt -q login_shell) ]; then
echo "Please call this using build/local.sh rather than directly"
exit 1
fi

. $NVM_DIR/nvm.sh
# This is your "main" build file. By default, it builds a 10up/wp-scaffold style project
# but you are free to modify it as required for your project. Remember, you can also
# drop in any number of scripts and they will be run in alphabetical order AFTER main.sh

# detect if this is a standard layout or not

if [ -d wordpress/wp-content ]; then
pushd wordpress/wp-content
elif [ -d plugins ]; then
pushd . # go no where, we are already in the right spot
fi
if [ "${CI:-false}" = "true" ] && [ -f composer.json ]; then
if [ ! -f composer.lock ] && [ -f composer.json ]; then
echo "No composer.lock file detected. You should create/commit an up to date composer.lock file!"
exit 1
else
composer install --no-dev
fi
elif [ -f composer.json ]; then
composer install
fi


if [ -f package.json ]; then
# Ensure we have the correct node version installed
nvm install
nvm use

if [ "${CI:-false}" = "true" ] && [ -f package.json ]; then
if [ -f package-lock.json ]; then
npm ci
else
echo "No package-lock.json file detected. You should create/commit an up to date package-lock.json file!"
exit 1
fi
else
npm install
fi

npm run build
fi
popd
}

function build:local {
set -eo pipefail
# Create additional build scripts in the build directory with a .sh
# extension. They should do their work inside the wordpress directory.

# We always call main.sh first
build:preflight
build:main

# Then call any other drop in scripts next
for I in $(ls scripts/*sh)
do
. $I
done
}

# Perform a CI like build
function build:full {
set -eo pipefail

build:preflight
build:install
build:local

# This rsync will typically work but if you have integrated the CI Library
# into a non project template based project you should adjust it

# First determine if we are using a project rsync-exclude or the included one
if [ -f scripts/rsync-excludes.txt ]; then
RSYNC_EXCLUDES="scripts/rsync-excludes.txt"
fi

if [ ${PROJECT_TYPE} == "standard" ]; then
rsync -a --exclude-from=${RSYNC_EXCLUDES} wordpress/ payload/
else
for I in themes mu-plugins plugins
do
if [ -d $I ]; then
rsync -a --exclude-from=${RSYNC_EXCLUDES} $I/ payload/wp-content/$I
fi
done
fi

}

function build:update-composer {

build:preflight

if [ ${PROJECT_TYPE} == "standard" ]; then
pushd wordpress/wp-content
else
pushd .
fi

if [ ! composer.json ]; then
echo "No composer.json file found. Run this from the root of a project and try again."
exit 1
fi

for I in $(find . -maxdepth 1 -name composer.json)
do
composer update --no-install
done

pushd themes
for I in $(find . -maxdepth 2 -name composer.json)
do
pushd $(dirname $I)
composer update --no-install
popd
done

popd
popd
}

function build:initialize-git {
git init .
echo
echo
echo 'Git has been initialized. Please run "git remote add origin <project git url>" to set the remote repository location.'
}

function build:package {

if [ -z $(which docker) ]; then
echo "You don't seem to have Docker installed but it is required for this to work."
exit 1
fi

if [ ! -d payload ]; then
echo "No payload directory found. Please run 10up-toolkit project create-payload first."
exit 1
fi

# First determine if we are using a project Dockerfile or the included one
if [ -f Dockerfile ]; then
DOCKERFILE="Dockerfile"
else
DOCKERFILE="${SHARE_DIR:?}/Dockerfile"
fi
# FIXME: This should be updated to use variables from .tenup-ci.yml
docker buildx build --load -f ${DOCKERFILE} . -t tenup-project:latest
}


# Converts a git branch to a gitlab compatible slug
function utilities:create-gitlab-slug {
local VALUE=$(echo $1 | sed 's/[^a-zA-Z0-9]/-/g' | awk '{print tolower($0)}')

echo ${VALUE}
}

maybe_init_nvm
eval build:$@
2 changes: 1 addition & 1 deletion packages/toolkit/scripts/project/bash/create-payload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ download https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz wordpress.t
tar --strip-components=1 -zxmf wordpress.tar.gz -C .
rm wordpress.tar.gz

rsync -avz --exclude-from=$deploy_file_excludes_absolute $project_root/$deploy_from $deploy_to_subdir
rsync -avz --exclude-from=$rsync_file_excludes_absolute $project_root/$deploy_from $deploy_to_subdir
Loading
Loading