Skip to content

Instantly share code, notes, and snippets.

@slattery
Last active May 13, 2019 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slattery/511d55c655a4b8d3dfb0da3a71abba57 to your computer and use it in GitHub Desktop.
Save slattery/511d55c655a4b8d3dfb0da3a71abba57 to your computer and use it in GitHub Desktop.
pantheon-drops8ex-noci.mk

drops8ex no CI Makefile

Makefile to create a new sandbox on the Pantheon service that follows the instructions found in the Pantheon article "Drupal 8 and Composer on Pantheon Without Continuous Integration"

pantheon-drops8ex-noci.mk

Create new composer-managed d8 base sandboxes on Pantheon w/o CI

Setup:

  • assumes user has php, git, terminus, GNU make, bash, sed, awk.
  • assumes user has Pantheon auth token and SSL key in profile.

You will need to supply your Pantheon machine token in ENV using the PANTHEON_MACH_TOKEN variable, and optional sandbox name with a PANTHEON_SITE_NAME variable. The default will use a portion of your account ID in the site name.

Copy or rename this file to 'Makefile'. Place makefile in work directory, type 'make' to get help text to run parts separately.

make all will create a copy of the example-drops-8-composer repo in your directory, initializes a new sandbox on pantheon, and make edits proposed in the article: https://pantheon.io/docs/guides/drupal-8-composer-no-ci/

Afterwards it runs composer, uploads the results to the sandbox, and installs drupal on the pantheon sandbox.

The local repo is committed after the configuration phase, then again after the composer phase, using tags. You can run make per stage, try various composer requires, etc. and run 'clean' recipes to rewind and make changes, then try to repeat stages and move toward upload.

make

 # Pantheon no-CI sandbox installer
 # alters composer config values and requirements following Pantheon advice:
 # https://pantheon.io/docs/guides/drupal-8-composer-no-ci/
 # The following ENV variables are important:
 # PANTHEON_MACH_TOKEN=yourPantheonKey PANTHEON_SITE_NAME=your-new-noci-sitename
    Usage: <option=pairs> make [TARGET|all]

all                            full cycle new sandbox creation
init                           initialize pantheon drop8 codebase and companion sandbox
install-local-config           perform edits to support no-ci config
install-local-composer         perform composer edits and actions to support no-ci
install-remote-codebase        push composer-fed, drop8-based no-ci code on remote sandbox
install-remote-cms             perform install using pushed codebase on remote sandbox
clean                          Returns repo to 'install-local-config' stage, syncs sandbox
distclean                      Returns repo to 'init' stage, syncs sandbox
clobber                        Removes source directory and deletes sandbox if present

gripes: github.com/slattery

This makefile is not a Pantheon product, they have no role or responsibility regarding this makefile.

THE MAKEFILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.

credits:

# pantheon-drops8ex-noci.mk
# Create new composer-managed d8 base sandboxes on Pantheon w/o CI
#
# Setup:
# - assumes user has php, git, terminus, GNU make, bash, sed, awk.
# - assumes user has Pantheon auth token and SSL key in profile.
#
# You will need to supply your Pantheon machine token in ENV under
# a PANTHEON_MACH_TOKEN variable, and optional sandbox name with a
# PANTHEON_SITE_NAME variable.
#
# Copy or rename this file to 'Makefile'. Place makefile in work
# directory, type 'make' to get help text to run parts separately.
# 'make all' will create a copy of the example-drops-8-composer
# repo in your directory, initializes a new sandbox on pantheon,
# and make edits proposed in the article:
# https://pantheon.io/docs/guides/drupal-8-composer-no-ci/
# Afterwards it runs composer, uploads the results to the sandbox,
# and installs to a new pantheon sandbox
#
# The local repo is committed after the configuration phase, then
# again after the composer phase, using tags. You can run make per
# stage, try various composer requires, etc. and run 'clean' recipes
# that use 'git reset --hard <stage-tag>' to rewind and make changes,
# and repeat stages and move toward upload.
#
# THE MAKEFILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, please
# use at your own risk.
MAKEFLAGS += --warn-undefined-variables
SHELL := $(shell which bash)
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR: ;
.SUFFIXES: ;
.ONESHELL: ; # recipes execute in same shell
.NOTPARALLEL: ; # wait for this target to finish
.EXPORT_ALL_VARIABLES: ; # send all vars to shell
.DEFAULT := .help
.DEFAULT_GOAL := .help
THIS = $(word 1,$(MAKEFILE_LIST))
GIT_SRC = https://github.com/pantheon-systems/example-drops-8-composer.git
ACCT_HASH = $(shell terminus auth:whoami --field=ID | cut -b 1-7)
# local editables (composer calls are at bottom of makefile)
DEFAULT_SITE_NAME = drops8ex-noci-${ACCT_HASH}
PANTHEON_SITE_NAME ?= $(DEFAULT_SITE_NAME)
CWD_DIRECTORY := $(CURDIR)
SRC_DIRECTORY = $(CWD_DIRECTORY)/$(PANTHEON_SITE_NAME)
GITNORE = $(SRC_DIRECTORY)/.gitignore
PANYAML = $(SRC_DIRECTORY)/pantheon.yml
# top line
.PHONY: all
all: init install-local-config install-local-composer install-remote-codebase install-remote-cms ## full cycle new sandbox creation
# secondary
.PHONY: init
init: init-session init-pantheon ## initialize pantheon drop8 codebase and companion sandbox
.PHONY: install-local-config
install-local-config: init-session install-gitignore-edits install-pantheon-yaml-edits ## perform edits to support no-ci config
@echo "committing changes to local repo after config edits."
@cd $(SRC_DIRECTORY) && git diff-index --quiet HEAD \
|| (git add -A && git commit -m "config updates for no-ci" \
&& cd $(CWD_DIRECTORY) && make -f $(THIS) -s .tagadd tag="noci-post-config")
.PHONY: install-local-composer
install-local-composer: init-session install-composer-mods ## perform composer edits and actions to support no-ci
@echo "committing changes to local repo after composer update"
@cd $(SRC_DIRECTORY) && git diff-index --quiet HEAD \
|| (git add -A && git commit -m "composer requirements for no-ci" \
&& cd $(CWD_DIRECTORY) && make -f $(THIS) -s .tagadd tag="noci-post-composer")
.PHONY: install-remote-codebase
install-remote-codebase: init-session install-codebase-to-remote ## push composer-fed, drop8-based no-ci code on remote sandbox
.PHONY: install-remote-cms
install-remote-cms: init-session install-cms-on-remote ## perform install using pushed codebase on remote sandbox
# tertiary
.PHONY: init-pantheon
init-pantheon: init-sandbox init-git-remote
# routines
.PHONY: check-remote-sandbox
# check existence of pantheon sandbox instance
check-remote-sandbox:
@$(eval SITEPLAN := $(shell terminus site:info --field=plan_name $(strip $(PANTHEON_SITE_NAME))))
@if [[ -z "$(SITEPLAN)" || "$(SITEPLAN)" = "" ]]; then \
echo -e " $(PANTHEON_SITE_NAME) sandbox does not exist for this account."; \
false; \
elif [ "$(SITEPLAN)" = "Sandbox" ]; then \
echo -e " $(PANTHEON_SITE_NAME) is a sandbox site, continuing..."; \
true; \
else \
echo -e " $(PANTHEON_SITE_NAME) is not a sandbox site."; \
false; \
fi
.PHONY: check-local-codebase
check-local-codebase:
@echo "Checking repo for uncommitted changes.."
@sleep 4
@cd $(SRC_DIRECTORY) && (git diff-index --quiet HEAD \
&& echo " [OK] Repo looks clean") \
|| (echo " [WARN] You have uncommitted changes:" && git status --porcelain \
&& echo -n "Do you want to push anyway? [y/N] " && read ans && [ $${ans:-N} = y ])
.PHONY: check-local-repo
check-local-repo:
@[ ! -d $(SRC_DIRECTORY) ] \
&& echo " [WARN] source directory not found." \
|| (echo -n "Are you sure you want to delete $(SRC_DIRECTORY)? [y/N] " && read ans && [ $${ans:-N} = y ])
.PHONY: init-session
init-session:
@echo "logging into Pantheon via terminus..."
@terminus auth:login --machine-token=$(PANTHEON_MACH_TOKEN)
.PHONY: init-sandbox
init-sandbox:
@make -f $(THIS) -s check-remote-sandbox \
&& echo -e " [OK] working with remote sandbox $(PANTHEON_SITE_NAME)" \
||( echo "Creating pantheon sandbox $(PANTHEON_SITE_NAME)..." \
&& terminus site:create $(PANTHEON_SITE_NAME) $(PANTHEON_SITE_NAME) empty \
&& echo -e " [OK] created remote sandbox $(PANTHEON_SITE_NAME)" \
|| make -f $(THIS) -s .exit text="Sandbox creation Failed" )
.PHONY: init-git-remote
# create repo from pantheon drop8 as base
init-git-remote: $(SRC_DIRECTORY)/.git
@echo "Checking remote pantheon git repo..."
@cd $(SRC_DIRECTORY) \
&& git remote set-url origin $(shell terminus connection:info $(strip $(PANTHEON_SITE_NAME)).dev --field=git_url) \
&& echo -e " [OK] set git origin to sandbox"
$(SRC_DIRECTORY)/.git:
@echo -e " [WARN] drupal source not found; cloning from $(GIT_SRC)..." \
&& ( git clone $(GIT_SRC) $(SRC_DIRECTORY) && cd $(SRC_DIRECTORY) && git tag noci-post-clone ) \
&& echo -e " [OK] $(PANTHEON_SITE_NAME) created" \
|| make -f $(THIS) -s .exit text="Git clone failed" \
.PHONY: install-gitignore-edits
# This assumes that the Pantheon-supplied .gitignore file still requires changes re:
# https://pantheon.io/docs/guides/drupal-8-composer-no-ci/
# Alter root .gitignorefile to remove CI support
install-gitignore-edits: install-gitignore-noci
.PHONY: install-gitignore-noci
# Extra hop here - needed to diff with other specialized makefiles under the
# install-gitignore-edits banner
# find ': cut :' line in file and decrement to keep that line, as it is meaningful to Pantheon
install-gitignore-noci:
@echo "Removing CI lines from drop8 gitignore..."
@$(eval CUTLINE := $(shell echo "$$((`grep -m 1 -in ': cut :' $(GITNORE) | awk -F: '{print $$1}'` -1 ))"))
@if test $(CUTLINE) -gt 0; then \
echo -e " [WARN] cutting lines in gitignore file up to line ${CUTLINE}"; \
sed -i.bak -e '1,${CUTLINE}d' "$(GITNORE)"; \
echo -e " [OK] Removed CI support lines from .gitignore"; \
fi;
.PHONY: install-pantheon-yaml-edits
install-pantheon-yaml-edits:
@echo "Removing sync lines from pantheon.yml..."
@sed -i.bak -e '/^workflows/,/push-back-to-github\.php/{/.*/d;}' "$(PANYAML)";
@grep -q "push-back-to-github" "$(PANYAML)" \
|| (echo -e " [OK] pantheon.yml edited" \
|| make -f $(THIS) -s .exit text="pantheon.yml edit failed")
.PHONY: install-codebase-to-remote
# Make sure out connection is in 'git push' mode
# Push changes made to drop8 that support no-ci to sandbox
install-codebase-to-remote: check-local-codebase check-remote-sandbox
@echo "Pushing local commited state upstream to sandbox"
@terminus connection:set $(PANTHEON_SITE_NAME).dev git
@sleep 2
@(cd $(SRC_DIRECTORY) && git push --force ) \
&& echo -e " [OK] git repo pushed to sandbox" \
|| make -f $(THIS) -s .exit text="git push failed"
.PHONY: install-cms-on-remote
# Make sure connection in 'sftp' mode so filesystem can be written to
# Run install on standard drops8ex profile
install-cms-on-remote:
@echo "Attempting d8-composer install on sandbox"
@echo "Setting connnection protocol to sftp for on-server edits"
@terminus connection:set $(PANTHEON_SITE_NAME).dev sftp \
&& echo -e " [OK] protocol changed to sftp" \
|| make -f $(THIS) -s .exit text="protocol change failed"
@terminus drush $(PANTHEON_SITE_NAME).dev -- site-install --verbose -y \
&& echo -e " [OK] finished on-server install. Please remember to copy down the admin password!" \
|| make -f $(THIS) -s .exit text="server install failed!"
.PHONY: uninstall
# Deletes sandbox if present
uninstall:
@echo "Preparing to remove sandbox..."
@make -f $(THIS) -s check-remote-sandbox \
&& terminus site:delete $(PANTHEON_SITE_NAME) \
|| make -f $(THIS) -s .exit text="Removal failed. Exiting."; \
.PHONY: clean
clean: ## Returns repo to 'install-local-config' stage, syncs sandbox
@echo "Bringing local repo back to edited state, 'install-local-config' stage..."
@cd $(SRC_DIRECTORY) \
&& git reset --hard noci-post-config \
&& cd $(CWD_DIRECTORY) \
&& make -f $(THIS) -s .tagrm tag="noci-post-composer" \
&& make -f $(THIS) -s install-codebase-to-remote
.PHONY: distclean
distclean: ## Returns repo to 'init' stage, syncs sandbox
@echo "Bringing local repo back to source state, 'init' stage..."
@cd $(SRC_DIRECTORY) \
&& git reset --hard noci-post-clone \
&& cd $(CWD_DIRECTORY) \
&& make -f $(THIS) -s .tagrm tag="noci-post-composer" \
&& make -f $(THIS) -s .tagrm tag="noci-post-config" \
&& make -f $(THIS) -s install-codebase-to-remote
.PHONY: clobber-local-repo
# wipes all code and directory for the named site
clobber-local-repo: check-local-repo
@echo "removing local repo..."
@rm -rf $(SRC_DIRECTORY) \
&& echo -e " [OK] repo for ${PANTHEON_SITE_NAME} deleted" \
|| make -f $(THIS) -s .exit text="repo delete failed!"
.PHONY: clobber
clobber: clobber-local-repo uninstall ## Removes source directory and deletes sandbox if present
.PHONY: .exit
.exit:
@(echo -e " [ERROR] $(text)";exit 1)
.PHONY: .help
.help:
@echo " # Pantheon no-CI sandbox installer"; \
echo " # alters composer config values and requirements following Pantheon advice:"; \
echo " # https://pantheon.io/docs/guides/drupal-8-composer-no-ci/"; \
echo " # The following ENV variables are important:"; \
echo " # PANTHEON_MACH_TOKEN=yourPantheonKey PANTHEON_SITE_NAME=your-new-noci-sitename"; \
echo " Usage: <option=pairs> make [TARGET|all]"; \
echo ""; \
grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: .tagrm
# preventing git tag from throwing errors, assuming repeated calls to blame
.tagrm:
@echo " Attempting to remove tag $(tag)..."
@cd $(SRC_DIRECTORY) \
&& ( git tag | grep "$(tag)" && git tag -d $(tag) )\
|| ( echo " [WARN] $(tag) not found, noci tag list to follow..." && (git tag | grep "noci"))
.PHONY: .tagadd
# preventing git tag from throwing errors, assuming repeated calls to blame
.tagadd:
@echo " Adding tag $(tag)..."
@cd $(SRC_DIRECTORY) \
&& ( git tag | grep "$(tag)" || git tag $(tag) )\
|| ( echo " [WARN] $(tag) already there, cannot add, noci tag list to follow..." && (git tag | grep "noci"))
.PHONY: install-composer-mods
# EDIT COMPOSER REMOVES/REQUIRES HERE
# using php below over jq assuming dev box must have php
install-composer-mods:
@echo "reconfiguring settings for no-ci support..."
@echo " removing unwanted directories..."
rm -rf $(SRC_DIRECTORY)/scripts/github
rm -rf $(SRC_DIRECTORY)/scripts/gitlab
rm -rf $(SRC_DIRECTORY)/.circleci
rm -rf $(SRC_DIRECTORY)/tests
@echo " removing unwanted dev and ci composer entries..."
@php -r '$$j=file_get_contents("${SRC_DIRECTORY}/composer.json");$$d=json_decode($$j,true);unset($$d["require-dev"],$$d["scripts"]["code-sniff"],$$d["scripts"]["unit-test"],$$d["scripts"]["lint"]);file_put_contents("${SRC_DIRECTORY}/composer.json",json_encode($$d,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));'
@echo " reconfiguring composer packages..."
@cd $(SRC_DIRECTORY) \
&& composer config name 'noorg/pantheon-drops-8-composer-contenta-noci' \
&& composer config description 'Pantheon drops-8 with Composer without CI on Pantheon. Based on pantheon-systems/example-drops-8-composer'
@echo " performing composer update and install..."
@cd $(SRC_DIRECTORY) \
&& composer update --with-dependencies --no-dev \
&& composer install --no-dev \
&& echo -e " [OK] composer reconfiguration completed" \
|| make -f $(THIS) -s .exit text="composer reconfiguration failed"
# gripes: github.com/slattery
# credits:
# https://github.com/pantheon-systems/example-drops-8-composer.git
# https://pantheon.io/docs/guides/drupal-8-composer-no-ci/
# https://github.com/Respect/Doc/blob/master/Makefile
# https://stackoverflow.com/a/47839479
# https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
# http://clarkgrubb.com/printer--friendly//makefile-style-guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment