Guest User

Untitled

a guest
Jan 23rd, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Pulls latest changes from pelican git repos in use and then publishes the resulting site if there are new commits
  4. # in either the theme or content. Also, runs a check against the last set of known future posts, and if they have
  5. # come due, do a deploy.
  6. #
  7. # Assumes that your theme and content exist in separate repositories. In the case of S3/CloudFront deployments,
  8. # assumes that you are using aws-cli to manage them. Update as needed if you do something else.
  9. #
  10. #
  11. # Leave these next three variables alone.
  12. PRETEND="$PRETEND" # Optionally, you can feed this environment variable to test the script without actually deploying.
  13. DEPLOY=0 # Will we deploy?
  14. LASTDATEEVAL="" # Used when reviewing scheduled posts.
  15. # ---------------------------------------------
  16. # Set the below variables according to your system needs.
  17. # ---------------------------------------------
  18. # Absolute path to your pelican theme directory
  19. THEMEDIR='/path/to/theme/repo'
  20. # Absolute path to your pelican content repo.
  21. CONTENTDIR='/path/to/pelican/repo'
  22. # Path to virtualenv where you have pelican installed.
  23. VIRTENVPATH='/path/to/pelican/virtualenv/bin/activate'
  24. # If using CloudFront and would like automatic invalidations on a deploy include the distribution id here.
  25. CFDISTID=""
  26. # Target you use for your deploy. So if you usually run 'make s3_upload', you should assign this variable to 's3_upload'.
  27. MAKETARGET='s3_upload'
  28.  
  29. # ----------------------------------------------
  30. # Here we go!
  31. # ----------------------------------------------
  32.  
  33. echo "Starting auto publish at $(date)"
  34.  
  35. if [ -n "$PRETEND" ]; then
  36. if [[ "$PRETEND" == "1" ]]; then
  37. 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."
  38. fi
  39. else
  40. PRETEND="0"
  41. fi
  42.  
  43. # ----------------------------------------------
  44. # Function for checking and storing next date for scheduled post in the future..
  45. # ----------------------------------------------
  46. check_for_posts_in_waiting () {
  47. LASTDATEEVAL="" #Setting this back to zero.
  48. IFS=$'\n'
  49. 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'`)
  50. num_posts=${#my_array[@]}
  51. echo "Retrieved $num_posts for evaluation..."
  52. for i in ${my_array[@]}; do
  53. if [[ $i > $(date +"%Y-%m-%d %H:%M:%S") ]]; then
  54. echo "We have a future date. Stashing it."
  55. LASTDATEEVAL="$i"
  56. else
  57. if [[ -n "$LASTDATEEVAL" ]]; then
  58. echo "Last future date found was $LASTDATEEVAL. Storing..."
  59. echo "$LASTDATEEVAL" > "$CONTENTDIR/.lastdateeval"
  60. else
  61. echo "No future posts found. Closing down."
  62. echo "" > "$CONTENTDIR/.lastdateeval"
  63. fi
  64. break
  65. fi
  66. done
  67. }
  68.  
  69.  
  70. # Check if we have a previous date eval record we can use.
  71. echo "Checking for previous date eval..."
  72. if [ -f ${CONTENTDIR}/.lastdateeval ]; then
  73. LASTDATEEVAL=`cat ${CONTENTDIR}/.lastdateeval`
  74. if [[ -z "$LASTDATEEVAL" ]]; then
  75. echo "There were no previously scheduled future posts to evaluate. Moving on."
  76. else
  77. echo "Loading up last date eval of '$LASTDATEEVAL' for comparison..."
  78. fi
  79. else
  80. echo "We've never done a date eval before, assume we're going to deploy."
  81. DEPLOY=1
  82. touch ${CONTENTDIR}/.lastdateeval
  83. fi
  84.  
  85. if [[ -n "$LASTDATEEVAL" ]]; then
  86. if [[ "$LASTDATEEVAL" < $(date +'%Y-%m-%d %H:%M:%S') ]]; then
  87. echo "We definitely have posts pending for deployment."
  88. DEPLOY=1
  89. fi
  90. fi
  91.  
  92.  
  93. echo "Fetching current state of theme repo..."
  94. cd ${THEMEDIR}
  95. FLEXREF=`git --no-pager log --oneline -n1`
  96. echo "Update theme..."
  97. echo "Pull updates for $(pwd)"
  98. git pull
  99. FLEXREFNEW=`git --no-pager log --oneline -n1`
  100. if [[ "$FLEXREF" != "$FLEXREFNEW" ]]; then
  101. echo "Found new commits in theme repo. Setting deploy mode to YES..."
  102. DEPLOY=1
  103. else
  104. echo "No new commits for theme."
  105. fi
  106. echo "Update content..."
  107. cd ${CONTENTDIR}
  108. BLOGREF=`git --no-pager log --oneline -n1`
  109. echo "Pull updates for $(pwd)"
  110. git pull
  111. BLOGREFNEW=`git --no-pager log --oneline -n1`
  112. if [[ "$BLOGREF" != "$BLOGREFNEW" ]]; then
  113. echo "Found new commits in content repo. Setting deploy mode to YES..."
  114. DEPLOY=1
  115. else
  116. echo "No new content."
  117. fi
  118.  
  119. # Set up last date eval record for next run.
  120. check_for_posts_in_waiting
  121.  
  122. if [[ "$DEPLOY" == 1 ]]; then
  123. echo "Site has new content or design. Beginning deploy process."
  124. echo "Activate virtualenv..."
  125. source ${VIRTENVPATH}
  126. echo "Publish content..."
  127. if [[ "$PRETEND" != "1" ]]; then
  128. echo "DEBUG: Trying to run make from `pwd`..."
  129. cd ${CONTENTDIR} && make ${MAKETARGET}
  130. if [ -n "$CFDISTID" ]; then
  131. echo "Sending invalidation to CF..."
  132. aws cloudfront create-invalidation --distribution-id ${CFDISTID} --paths "/*"
  133. fi
  134. else
  135. echo "Pretending to deploy..."
  136. fi
  137. echo "Cleaning up..."
  138. make clean
  139. echo "Deactivate virtualenv"
  140. deactivate
  141. else
  142. echo "No new content or design. Skipping deploy."
  143. fi
  144. echo "Done! Completed at $(date)"
  145. exit 0
Add Comment
Please, Sign In to add comment