Post-Build HooksΒΆ

Solano CI supports running a post-build task after the entire build process has completed. Once the tests in a build have run to completion, Solano CI automatically elects a master worker. The master worker runs the post_build setup hook if it is defined in the solano.yml configuration file, or if no setup hooks are defined it will run an existing tddium:post_build_hook rake task.

Solano CI will export the status of the build in the TDDIUM_BUILD_STATUS environment variable which will have one one of the values: passed, failed, or error.

The status of the previous build in the same suite is available in the TDDIUM_LAST_BUILD_STATUS environment variable.

The build mode (“cli”, or “ci”) is available in TDDIUM_MODE.

The task has access to the configured SSH deploy key which you can use to authenticate a git push, Capistrano deploy, or an arbitrary script. Additional environment variables set in the configuration file, set with the solano-cli, or automatically set on the worker can be used by deployment scripts.

An example post_build hook deploy configuration:

# solano.yml
hooks:
  post_build: ./bin/deploy.sh
environment:
  'DEPLOY_APP_NAME': 'Example App'
  'DEPLOY_USERNAME': 'me@example.com'
  'DEPLOY_REGION':   'datacenter_east'
  # For sensitive values, use `solano config:add repo <key> <value>` from the cli
  # See: http://docs.solanolabs.com/Setup/setting-environment-variables/#via-config-variables
  # 'DEPLOY_PASSWORD': '<set-with-cli>'

An example generic deploy script:

#!/usr/bin/env bash
# bin/deploy.sh
# Solano CI post_build hook (http://docs.solanolabs.com/Setup/setup-hooks/)

### Ensure failed commands result in a failed Solano CI build ###
set -o errexit -o pipefail

### Should this build trigger a deploy? ###
# Only deploy if all tests have passed
if [[ "passed" != "$TDDIUM_BUILD_STATUS" ]]; then
  echo "\$TDDIUM_BUILD_STATUS = $TDDIUM_BUILD_STATUS"
  echo "Will only deploy on passed builds"
  exit
fi

# Only deploy on 'master' branch
if [[ "master" != "$TDDIUM_CURRENT_BRANCH" ]]; then
  echo "\$TDDIUM_CURRENT_BRANCH = $TDDIUM_CURRENT_BRANCH"
  echo "Will only depoloy on master branch"
  exit
fi

# Only deploy on webhook-triggered builds from SCM server
if [[ "ci" != "$TDDIUM_MODE" ]]; then
  echo "\$TDDIUM_MODE = $TDDIUM_MODE"
  echo "Will on deploy on ci initiated builds."
  exit
fi

### Are all of the necessary environment variables set? ###
required_vars="DEPLOY_APP_NAME DEPLOY_USERNAME DEPLOY_REGION DEPLOY_PASSWORD"
vars_set=true
for var in $required_vars; do
  eval val=\""\$$var"\"
  if [ "$val" == "" ]; then
    echo "ERROR: $var is not set"
    vars_set=false
  fi
done
if [ "$vars_set" == "false" ]; then
  echo "ERROR: Not all required environemnt variables are set"
  echo 'See: http://docs.solanolabs.com/Setup/setting-environment-variables/#via-config-variables'
  echo 'Please use `solano config:add <scope> <key> <value>` to set sensitive values'
  exit 1
fi

### Ensure any needed executables are installed ###
if [[ ! -f "$HOME/bin/deploy_command" ]]; then
  wget -O $HOME/bin/deploy_command https://example.com/path/to/deploy_command
  chmod +x $HOME/bin/deploy_command
fi

### Run any preparatory tasks needed for deployment ###
# Set version in manifest file
sed -e "s;%VERSION_PLACEHOLDER%;$TDDIUM_SESSION_ID;g" config/deploy.json.template > deploy.json

# Create configuration file
echo "[config]" > $HOME/.deploy_config
echo "region: ${DEPLOY_REGION}" >> $HOME/.deploy_config

### Deploy ###
# Authenticate with deploy target
deploy_command login # Presumes $DEPLOY_USERNAME and $DEPLOY_PASSWORD are pulled from environment

# Execute deploy command
deploy_command --app-name="${DEPLOY_APP_NAME}" --manifest-file=deploy.json