Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rbf/370c5fd3bf1a78d7db2f to your computer and use it in GitHub Desktop.
Save rbf/370c5fd3bf1a78d7db2f to your computer and use it in GitHub Desktop.
For reference, this is the way I managed to setup TravisCI.org to automatically set the Heroku app on maintenance mode and capturing a backup before deploying to master (my production branch).

For reference, this is the way I managed to do it as of March 2016 starting with the link mentioned in this comment:

  1. Added HEROKU_API_LOGIN (from heroku auth:whoami) and HEROKU_API_KEY (from heroku auth:token) as protected ENV variables in the travis.org build settings. Alternatively, they can be added in the .travis.yml with:

    travis encrypt HEROKU_API_LOGIN ="$(heroku auth:whoami)" --add
    travis encrypt HEROKU_API_KEY="$(heroku auth:token)" --add
  2. Added following lines to the .travis.yml:

    sudo: required
    before_deploy:
      - ./.travis_before_deploy.sh
    after_deploy:
      - ./.travis_after_deploy.sh

    sudo: required is required since the use of container based build environments by default.

  3. Created the file .travis_before_deploy.sh with the following content:

    #!/bin/bash
    
    set -e -o pipefail
    
    if [ "${TRAVIS_BRANCH}" == "master" ]; then
    
      TARGET_APP_NAME="your_app_name"
    
      echo "Setting up '~/.ssh/config' to work with heroku."
      cat >> ~/.ssh/config <<EOF
    Host heroku.com
       StrictHostKeyChecking no
       CheckHostIP no
       UserKnownHostsFile=/dev/null
    EOF
    
      # DOC: https://devcenter.heroku.com/articles/authentication#usage-examples
      cat >> ~/.netrc <<EOF
    machine api.heroku.com
      login ${HEROKU_API_LOGIN}
      password ${HEROKU_API_KEY}
    EOF
      chmod 600 ~/.netrc
    
      echo "Installing heroku toolbelt"
      wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
    
      echo "Setting maintenance mode 'on' on heroku's app"
      heroku maintenance:on --app "${TARGET_APP_NAME}"
    
      echo "Caturing DB backup from heroku's app"
      # DOC: https://devcenter.heroku.com/articles/heroku-postgres-backups#creating-a-backup
      heroku pg:backups capture --app "${TARGET_APP_NAME}"
    fi
  4. Created the file .travis_after_deploy.sh with the following content:

    #!/bin/bash
    
    set -e -o pipefail
    
    if [ "${TRAVIS_BRANCH}" == "master" ]; then
    
      TARGET_APP_NAME="your_app_name"
    
      echo "Setting maintenance mode 'off' on heroku's app"
      heroku maintenance:off --app "${TARGET_APP_NAME}"
    
      echo "Application ready again. YAY!"
    fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment