Skip to content

Instantly share code, notes, and snippets.

@mirzu
Last active June 24, 2017 17:57
Show Gist options
  • Save mirzu/ceace699d037ec14100b6e2ed74ce2e6 to your computer and use it in GitHub Desktop.
Save mirzu/ceace699d037ec14100b6e2ed74ce2e6 to your computer and use it in GitHub Desktop.
CircleCi 2.0 config for creating Multidev enviroments from a Github PR
version: 2
jobs:
build:
docker:
- image: notnoopci/php:7.1.5-browsers
working_directory: ~/wordpress
steps:
- add_ssh_keys
- run:
name: Install Terminus
command: composer global require pantheon-systems/terminus "^1"
- run: echo 'export PATH=/home/circleci/.composer/vendor/bin:$PATH' >> $BASH_ENV
- run: echo "export ENV_NAME=$(echo $CIRCLE_BRANCH | tr '[:upper:]' '[:lower:]' | sed 's/[^0-9a-z-]//g' | cut -c -11 | sed 's/-$//')" >> $BASH_ENV
- run:
name: Login to Terminus
command: terminus auth:login --machine-token=$TERMINUS_TOKEN
- checkout
- run: echo "StrictHostKeyChecking no" >~/.ssh/config
- run: git remote add pantheon $PANTHEON_GIT_URL
- run: git fetch pantheon
- run:
name: Multidev stuff
command: |
if [ "${CIRCLE_BRANCH}" != "master" ]; then
./bin/multidev.sh
fi
- deploy:
name: Deploy
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
git push pantheon master
fi
#!/bin/bash
GITHUB_API_URL="https://api.github.com"
if [ "$PANTHEON_GIT_URL" != 0 ]
then
PANTHEON_GIT_URL="$(terminus connection:info $SITE_NAME.dev --fields=git_url --format=string)"
fi
echo "PANTHEON_GIT_URL is $PANTHEON_GIT_URL"
# Add a remote if it doesn't exist.
git ls-remote --exit-code pantheon >/dev/null 2>&1
if [ $? -ne 0 ]
then
git remote add pantheon $PANTHEON_GIT_URL
fi
PANTHEON_ENVS="$(terminus multidev:list $SITE_NAME --fields=Name --format=string)"
MULTIDEV_FOUND=0
while read -r line; do
if [[ "${line}" == "${ENV_NAME}" ]]
then
MULTIDEV_FOUND=1
fi
done <<< "$PANTHEON_ENVS"
## If the multidev for this branch is found
if [[ "$MULTIDEV_FOUND" -eq 1 ]]
then
# Simply push the the branch on Pantheon.
echo -e "\n${txtylw}Multidev found! Pushing to Pantheon.${txtrst}"
git push pantheon $CIRCLE_BRANCH:$ENV_NAME
else
# otherwise, create the multidev branch
echo -e "\n${txtylw}Multidev not found, creating the multidev branch ${ENV_NAME} via Terminus ${txtrst}"
terminus multidev:create $SITE_NAME.live "$ENV_NAME" --yes
MULTDEV_LINK="http://$ENV_NAME-$SITE_NAME.pantheonsite.io/"
PR_NUMBER=${CIRCLE_PULL_REQUEST##*/}
curl -i -u "$GITHUB_USER:$GITHUB_TOKEN" -d "{\"body\": \"Multidev `$ENV_NAME` created successfully! [$MULTDEV_LINK]($MULTDEV_LINK)\"}" $GITHUB_API_URL/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/issues/$PR_NUMBER/comments
echo $MULTIDEV_LINK
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment