Fun with SnapCI API and IFTTT Do Button

Snap-CI updated its APIs a while ago and I didn't have chance to try them and recently I found IFTTT Do Button so I thought "What about a deploy button?" Well, here it is:

Some details

It's a sort of Goldberg Machine but was fun. Basically what happens is:

  • A script monitors a folder on Dropbox with fswatch
  • A recipe to create a file in Dropbox is triggered through Do Button.
  • The script detects the event and uses Snap-CI API to trigger a manual stage.

Here's the script:

#!/usr/bin/env bash

set -e

SNAP_KEY=$(keychain snap-api-key)
SNAP_API_ROOT_URL="https://api.snap-ci.com/project/mavcunha/blog/branch/master"
SNAP_LATEST="/pipelines/latest"

HTTP_OPTS="-u mavcunha:${SNAP_KEY} --silent -X"

function _latest_pipeline() {
  local snap_result=""
  snap_result=$(curl ${HTTP_OPTS} GET ${SNAP_API_ROOT_URL}${SNAP_LATEST} | jq ._links.redirect.href | tr -d '"' )
  echo ${snap_result##*/}
}

function _publish() {
  local pipeline=${1}
  [[ -z ${pipeline} ]] && exit 1
  curl ${HTTP_OPTS} POST ${SNAP_API_ROOT_URL}/trigger/${pipeline}/publish | jq .message
}

echo "Start listening for triggers"
while read -d "" event; do
  echo "Publishing!"
  _publish $(_latest_pipeline)
done < <(fswatch -0 ~/Dropbox/PublishBlog)

gist of this script

For this script to work you need fswatch, jq and cURL. Note that I save my API key in my keychain, if you're not using a Mac you need to figure a way to keep it secret.

Published in Jun 13, 2015