Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Pulls latest changes from pelican git repos in use and then publishes the resulting site if there are new commits
- # in either the theme or content. Also, runs a check against the last set of known future posts, and if they have
- # come due, do a deploy.
- #
- # Assumes that your theme and content exist in separate repositories. In the case of S3/CloudFront deployments,
- # assumes that you are using aws-cli to manage them. Update as needed if you do something else.
- #
- #
- # Leave these next three variables alone.
- PRETEND="$PRETEND" # Optionally, you can feed this environment variable to test the script without actually deploying.
- DEPLOY=0 # Will we deploy?
- LASTDATEEVAL="" # Used when reviewing scheduled posts.
- # ---------------------------------------------
- # Set the below variables according to your system needs.
- # ---------------------------------------------
- # Absolute path to your pelican theme directory
- THEMEDIR='/path/to/theme/repo'
- # Absolute path to your pelican content repo.
- CONTENTDIR='/path/to/pelican/repo'
- # Path to virtualenv where you have pelican installed.
- VIRTENVPATH='/path/to/pelican/virtualenv/bin/activate'
- # If using CloudFront and would like automatic invalidations on a deploy include the distribution id here.
- CFDISTID=""
- # Target you use for your deploy. So if you usually run 'make s3_upload', you should assign this variable to 's3_upload'.
- MAKETARGET='s3_upload'
- # ----------------------------------------------
- # Here we go!
- # ----------------------------------------------
- echo "Starting auto publish at $(date)"
- if [ -n "$PRETEND" ]; then
- if [[ "$PRETEND" == "1" ]]; then
- echo "Pretend mode activated. Will treat any actual deployment as a dry run. NOTE: You will need to manually deploy if you expected changes to be done on this run."
- fi
- else
- PRETEND="0"
- fi
- # ----------------------------------------------
- # Function for checking and storing next date for scheduled post in the future..
- # ----------------------------------------------
- check_for_posts_in_waiting () {
- LASTDATEEVAL="" #Setting this back to zero.
- IFS=$'\n'
- my_array=(`find $CONTENTDIR/content -name "*.md" | xargs grep -ihe "^date: " | sort --stable --reverse --key=2,3 | sed 's/date: //g' | sed 's/Date: //g'`)
- num_posts=${#my_array[@]}
- echo "Retrieved $num_posts for evaluation..."
- for i in ${my_array[@]}; do
- if [[ $i > $(date +"%Y-%m-%d %H:%M:%S") ]]; then
- echo "We have a future date. Stashing it."
- LASTDATEEVAL="$i"
- else
- if [[ -n "$LASTDATEEVAL" ]]; then
- echo "Last future date found was $LASTDATEEVAL. Storing..."
- echo "$LASTDATEEVAL" > "$CONTENTDIR/.lastdateeval"
- else
- echo "No future posts found. Closing down."
- echo "" > "$CONTENTDIR/.lastdateeval"
- fi
- break
- fi
- done
- }
- # Check if we have a previous date eval record we can use.
- echo "Checking for previous date eval..."
- if [ -f ${CONTENTDIR}/.lastdateeval ]; then
- LASTDATEEVAL=`cat ${CONTENTDIR}/.lastdateeval`
- if [[ -z "$LASTDATEEVAL" ]]; then
- echo "There were no previously scheduled future posts to evaluate. Moving on."
- else
- echo "Loading up last date eval of '$LASTDATEEVAL' for comparison..."
- fi
- else
- echo "We've never done a date eval before, assume we're going to deploy."
- DEPLOY=1
- touch ${CONTENTDIR}/.lastdateeval
- fi
- if [[ -n "$LASTDATEEVAL" ]]; then
- if [[ "$LASTDATEEVAL" < $(date +'%Y-%m-%d %H:%M:%S') ]]; then
- echo "We definitely have posts pending for deployment."
- DEPLOY=1
- fi
- fi
- echo "Fetching current state of theme repo..."
- cd ${THEMEDIR}
- FLEXREF=`git --no-pager log --oneline -n1`
- echo "Update theme..."
- echo "Pull updates for $(pwd)"
- git pull
- FLEXREFNEW=`git --no-pager log --oneline -n1`
- if [[ "$FLEXREF" != "$FLEXREFNEW" ]]; then
- echo "Found new commits in theme repo. Setting deploy mode to YES..."
- DEPLOY=1
- else
- echo "No new commits for theme."
- fi
- echo "Update content..."
- cd ${CONTENTDIR}
- BLOGREF=`git --no-pager log --oneline -n1`
- echo "Pull updates for $(pwd)"
- git pull
- BLOGREFNEW=`git --no-pager log --oneline -n1`
- if [[ "$BLOGREF" != "$BLOGREFNEW" ]]; then
- echo "Found new commits in content repo. Setting deploy mode to YES..."
- DEPLOY=1
- else
- echo "No new content."
- fi
- # Set up last date eval record for next run.
- check_for_posts_in_waiting
- if [[ "$DEPLOY" == 1 ]]; then
- echo "Site has new content or design. Beginning deploy process."
- echo "Activate virtualenv..."
- source ${VIRTENVPATH}
- echo "Publish content..."
- if [[ "$PRETEND" != "1" ]]; then
- echo "DEBUG: Trying to run make from `pwd`..."
- cd ${CONTENTDIR} && make ${MAKETARGET}
- if [ -n "$CFDISTID" ]; then
- echo "Sending invalidation to CF..."
- aws cloudfront create-invalidation --distribution-id ${CFDISTID} --paths "/*"
- fi
- else
- echo "Pretending to deploy..."
- fi
- echo "Cleaning up..."
- make clean
- echo "Deactivate virtualenv"
- deactivate
- else
- echo "No new content or design. Skipping deploy."
- fi
- echo "Done! Completed at $(date)"
- exit 0
Add Comment
Please, Sign In to add comment