johnpfeiffer
  • Home
  • Engineering (People) Managers
  • John Likes
  • Software Engineer Favorites
  • Categories
  • Tags
  • Archives

Publish a pelican blog using a Bitbucket POST Webhook

Contents

  • Create the Bitbucket Webhook and Setup a Server to Receive the Webhook
  • Flask and Bash source code to publish a pelican static web site
    • vi mypublish.py
    • vi mypublish.sh
    • vi clean-output.sh
  • More info

Webhooks are an incredibly useful way to tie together disparate network parts, WHEN something happens in one place, it sends a POST HTTP request to another place.

Create the Bitbucket Webhook and Setup a Server to Receive the Webhook

  1. Log in to the Bitbucket WebUI
  2. Choose the repository
  3. Choose to administer the repository (gear symbol) -> Hooks (left menu) , or simply https://bitbucket.org/username/reponame/admin/hooks
  4. Select Hook Type (dropdown) , POST , Add Hook (Button)
  5. Enter your target URL, SAVE
  6. Setup a webserver (easiest might be Bamboo or Jenkins) somewhere
  7. Ensure there is a URL that accepts POST requests
  8. Ensure that when the POST is received it runs the pelican content generation commands to make the new output
  9. Ensure new output is visible

You may notice any existing POST webhooks, i.e. a HipChat notification add-on, listed: https://hipchat-bitbucket.herokuapp.com/commit?client_id=f955ddb5

Flask and Bash source code to publish a pelican static web site

This custom solution requires running that flask app manually, i.e. python mypublish.py It also requires having two repositories, one for the pelican source content, the other repo (i.e. a bitbucket static web site) will only contain the output (.html files)

vi mypublish.py

from flask import Flask
import os
from subprocess import Popen, PIPE

app = Flask(__name__)

@app.route('/someuniquekeyhere', methods=['GET', 'POST'])
def mypublish():
  try:
      output = Popen(["./mypublish.sh"], stdout=PIPE).communicate()[0]
  except Exception as error:
      return str(error)
  return output


if __name__ == '__main__':
    app.run('0.0.0.0', 8443, use_reloader=True)

vi mypublish.sh

#!/bin/bash
git pull
GITMESSAGE=$(git log -n 1)
OUTPUT="../outputreponame.bitbucket.io"
./clean-output.sh "../sourcereponame.bitbucket.io"  # removes all of the old content
echo "$GITMESSAGE"
pelican content
cp -a ./output/* $OUTPUT

rm -rf ./output
rm -rf ./cache
rm -f *.pyc

cd "$OUTPUT"
git add --all ./content
git commit -m "source $GITMESSAGE"
git push

vi clean-output.sh

#!/bin/bash

rm -rf ./output
rm -rf ./cache
rm -f *.pyc

for ITEM in $SOURCE/*
do
  if [ -d "$ITEM" ]; then
    rm -rf "$ITEM"
  else
    rm -f "$ITEM"
  fi
done

More info

  • https://read-the-docs.readthedocs.org/en/latest/webhooks.html
  • https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html

  • « Using Vagrant to deploy instances on AWS
  • Intro to Amazon AWS Elastic Beanstalk »

Published

Aug 8, 2014

Category

programming

~304 words

Tags

  • bitbucket 3
  • flash 1
  • pelican 3
  • webhook 1