|
1 | 1 | #!/bin/bash |
2 | | - |
3 | | -# Many aspsects of this came from https://gist.github.com/domenic/ec8b0fc8ab45f39403dd |
| 2 | + |
| 3 | +# Many aspects of this came from https://gist.github.com/domenic/ec8b0fc8ab45f39403dd |
4 | 4 | # Significant alterations |
5 | | -# - Avoid pulling all history for cloned repo |
6 | | -# - Support for multiple copies of documenation, |
7 | | -# - only clearing out latest |
8 | | -# - doc-history.md logging doc history |
9 | | -# - Wrapped calls with actual encrypt keys with > /dev/null 2> /dev/null4 |
| 5 | +# - Support for multiple copies of documentation, |
| 6 | +# - only clearing out develop |
| 7 | +# - index.md logging doc history |
10 | 8 |
|
11 | | -# How to run: |
| 9 | +# How to run: |
12 | 10 | # - From repository root .travis/push_docs_to_gh_pages.sh |
13 | 11 |
|
14 | 12 | # Required files / directories (relative from repo root) |
15 | | -# - Folder : "site" with that contains latest docs |
16 | | -# - File : ".travis/deploy_key.enc" SSH deployment key encrypted by Travis command line tool |
| 13 | +# - File: "docs/index.md" with that contains develop docs |
17 | 14 |
|
18 | 15 | # Required ENV Variables |
19 | | -# ENCRYPTION_LABEL - Manually Set in travis web settings. Value can be displayed in LOG |
20 | | -# encrypted_${ENCRYPTION_LABEL}_key - Set in web settings using travis cmdline encryption |
21 | | -# encrypted_${ENCRYPTION_LABEL}_iv - Set in web settings using travis cmdline encryption |
22 | | -# PAGES_TARGET_BRANCH="gh-pages" - Set in .travis.yml, branch were pages will be deployed |
23 | | -# PAGES_VERSION_BASE="version3" -Set in .travis.yml, directory for pages deployment |
| 16 | +PAGES_TARGET_BRANCH="gh-pages" |
| 17 | +LATEST_DOCS_BRANCH="develop" |
24 | 18 | # TRAVIS_* variables are set by travis directly and only need to be if testing externally |
25 | 19 |
|
26 | | -# Pull requests are special... |
27 | | -# They won't have acceess to the encrypted variables. |
28 | | -# This prevent access to the Encrypted SSH Key |
29 | | -# Regardless we don't want a pull request automatically updating the repository |
30 | | -if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then |
| 20 | +# Since we are running job matrix, only thie first job slave will need to do the work |
| 21 | +if [[ "${TRAVIS_JOB_NUMBER}" =~ \.1$ ]]; then |
| 22 | + # We don't want a pull request automatically updating the repository |
| 23 | + if [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "${CURRENT_BRANCH}" == "${LATEST_DOCS_BRANCH}" ]; then |
31 | 24 |
|
32 | | - # ENV Variable checks are to help with configuration troubleshooting |
33 | | - # they silently exit with unique message. Anyone one of them not set |
34 | | - # can be used to turn off this functionality. |
35 | | - # For example a feature branch in the master repository that we don't want docs produced for yet. |
36 | | - [[ -n "$PAGES_VERSION_BASE" ]] || { echo "PAGES_VERSION_BASE Missing"; exit 0; } |
37 | | - [[ -n "$PAGES_TARGET_BRANCH" ]] || { echo "PAGES_TARGET_BRANCH Missing"; exit 0; } |
38 | | - [[ -n "$ENCRYPTION_LABEL" ]] || { echo "ENCRYPTION_LABEL Missing"; exit 0; } |
39 | | - |
40 | | - # Fail because required script to generate documenation must have not been run, or was changed. |
41 | | - [[ -d ./site ]] || { echo "site directory not found"; exit 1; } |
42 | | - |
43 | | - # Save some useful information |
44 | | - REPO=`git config remote.origin.url` |
45 | | - SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:} |
46 | | - SHA=`git rev-parse --verify HEAD` |
| 25 | + # ENV Variable checks are to help with configuration troubleshooting, they silently exit with unique message. |
| 26 | + # Anyone one of them not set can be used to turn off this functionality. |
47 | 27 |
|
48 | | - # shallow clone the repostoriy and switch to PAGES_TARGET_BRANCH branch |
49 | | - mkdir pages |
50 | | - cd pages |
51 | | - git clone --depth 1 $REPO . |
52 | | - PAGES_BRANCH_EXISTS=$(git ls-remote --heads $REPO $PAGES_TARGET_BRANCH) |
53 | | - |
54 | | - if [ -n "$PAGES_BRANCH_EXISTS" ] ; then |
55 | | - echo "Pages Branch Found" |
56 | | - echo "-$PAGES_BRANCH_EXISTS-" |
57 | | - git remote set-branches origin $PAGES_TARGET_BRANCH |
58 | | - git fetch --depth 1 origin $PAGES_TARGET_BRANCH |
59 | | - git checkout $PAGES_TARGET_BRANCH |
60 | | - else |
61 | | - echo "Creating Pages Branch" |
62 | | - git checkout --orphan $PAGES_TARGET_BRANCH |
63 | | - git rm -rf . |
64 | | - fi |
65 | | - |
66 | | - #clear out latest and copy site contents to it. |
67 | | - echo "updating $VERSION_BASE/latest" |
68 | | - mkdir -p $PAGES_VERSION_BASE/latest |
69 | | - rm -rf $PAGES_VERSION_BASE/latest/**./* || exit 0 |
70 | | - cp -a ../site/. $PAGES_VERSION_BASE/latest |
71 | | - |
72 | | - # If a Tagged Build then copy to it's own directory as well. |
73 | | - if [ -n "$TRAVIS_TAG" ]; then |
74 | | - echo "Creating $PAGES_VERSION_BASE/$TRAVIS_TAG" |
75 | | - mkdir -p $PAGES_VERSION_BASE/$TRAVIS_TAG || exit 0 |
76 | | - cp -a ../site/. $PAGES_VERSION_BASE/$TRAVIS_TAG |
77 | | - fi |
78 | | - |
79 | | - #Check if there are doc changes, if none exit the script |
80 | | - if [[ -z `git diff --exit-code` ]] && [ -n "$PAGES_BRANCH_EXISTS" ] ; then |
81 | | - echo "No changes to docs detected." |
82 | | - exit 0 |
83 | | - fi |
| 28 | + # If a version of the project is not defined |
| 29 | + [[ -n "${UTPLSQL_VERSION}" ]] || { echo "variable UTPLSQL_VERSION is not defines or missing value"; exit 0; } |
| 30 | + # Fail if the markdown documentation is not present. |
| 31 | + [[ -f ./docs/index.md ]] || { echo "file docs/index.md not found"; exit 1; } |
84 | 32 |
|
85 | | - #Chganges where detected, so it's safe to write to log. |
86 | | - now=$(date +"%d %b %Y - %r") |
87 | | - export latest=" - [Latest Doc Change]($PAGES_VERSION_BASE/latest/) - Created $now" |
88 | | - if [ ! -f doc-history.md ]; then |
89 | | - echo "<!-- Auto generated in .travis/push_docs_to_gh_pages.sh -->" >doc-history.md |
90 | | - echo "#Doc Generation Log" >>doc-history.md |
91 | | - echo "" >>doc-history.md |
92 | | - echo "- 4th line placeholder see below" >>doc-history.md |
93 | | - echo "" >>doc-history.md |
94 | | - echo "##Released Version Doc History" >>doc-history.md |
95 | | - echo "" >>doc-history.md |
96 | | - fi |
97 | | - if [ -n "$TRAVIS_TAG" ]; then |
98 | | - echo " - [$TRAVIS_TAG]($PAGES_VERSION_BASE/$TRAVIS_TAG/) - Created $now" >>doc-history.md |
99 | | - fi |
100 | | - |
101 | | - #replace 4th line in log |
102 | | - sed -i '4s@.*@'"$latest"'@' doc-history.md |
| 33 | + # Save some useful information |
| 34 | + SHA=`git rev-parse --verify HEAD` |
103 | 35 |
|
104 | | - #Setup Git with Commiter info |
105 | | - git config user.name "Travis CI" |
106 | | - git config user.email "[email protected]" |
| 36 | + # clone the repository and switch to PAGES_TARGET_BRANCH branch |
| 37 | + mkdir pages |
| 38 | + cd pages |
| 39 | + git clone https://${github_api_token}@github.com/${UTPLSQL_REPO} . |
107 | 40 |
|
108 | | - #Add and Commit the changes back to pages repo. |
109 | | - git add . |
110 | | - git commit -m "Deploy to GitHub Pages: ${SHA}" |
111 | | - |
112 | | - #unencrypt and configure deployment key |
113 | | - [[ -e ../.travis/deploy_key.enc ]] || { echo ".travis/deploy_key.enc file not found"; exit 1; } |
114 | | - ENCRYPTED_KEY_VAR="encrypted_${ENCRYPTION_LABEL}_key" |
115 | | - ENCRYPTED_IV_VAR="encrypted_${ENCRYPTION_LABEL}_iv" |
116 | | - ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR} > /dev/null 2> /dev/null |
117 | | - ENCRYPTED_IV=${!ENCRYPTED_IV_VAR} > /dev/null 2> /dev/null |
118 | | - openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in ../.travis/deploy_key.enc -out ../.travis/deploy_key -d > /dev/null 2> /dev/null |
119 | | - chmod 600 ../.travis/deploy_key |
120 | | - eval `ssh-agent -s` |
121 | | - ssh-add ../.travis/deploy_key |
122 | | - |
123 | | - # Now that we're all set up, we can push. |
124 | | - echo "git push $SSH_REPO $PAGES_TARGET_BRANCH" |
125 | | - git push $SSH_REPO $PAGES_TARGET_BRANCH |
| 41 | + PAGES_BRANCH_EXISTS=$(git ls-remote --heads origin ${PAGES_TARGET_BRANCH}) |
| 42 | + |
| 43 | + if [ -n "$PAGES_BRANCH_EXISTS" ] ; then |
| 44 | + echo "Pages Branch Found" |
| 45 | + git checkout ${PAGES_TARGET_BRANCH} |
| 46 | + else |
| 47 | + echo "Creating Pages Branch" |
| 48 | + git checkout --orphan ${PAGES_TARGET_BRANCH} |
| 49 | + git rm -rf . |
| 50 | + fi |
| 51 | + #clear out develop documentation directory and copy docs contents to it. |
| 52 | + echo "updating 'develop' directory" |
| 53 | + mkdir -p develop |
| 54 | + rm -rf develop/**./* || exit 0 |
| 55 | + cp -a ../docs/. ./develop |
| 56 | + # If a Tagged Build then copy to it's own directory as well. |
| 57 | + if [ -n "$TRAVIS_TAG" ]; then |
| 58 | + echo "Creating ${UTPLSQL_VERSION}" |
| 59 | + mkdir -p ${UTPLSQL_VERSION} |
| 60 | + rm -rf ${UTPLSQL_VERSION}/**./* || exit 0 |
| 61 | + cp -a ../docs/. ${UTPLSQL_VERSION} |
| 62 | + fi |
| 63 | + # Stage changes for commit |
| 64 | + git add . |
| 65 | + #Check if there are doc changes, if none exit the script |
| 66 | + if [[ -z `git diff HEAD --exit-code` ]] && [ -n "${PAGES_BRANCH_EXISTS}" ] ; then |
| 67 | + echo "No changes to docs detected." |
| 68 | + exit 0 |
| 69 | + fi |
| 70 | + #Changes where detected, so we need to update the version log. |
| 71 | + now=$(date +"%d %b %Y - %r") |
| 72 | + export latest=" - [Latest development version](develop/) - Created $now" |
| 73 | + if [ ! -f index.md ]; then |
| 74 | + echo "# Documentation versions" >>index.md |
| 75 | + echo "<!-- Auto generated from .travis/push_docs_to_gh_pages.sh -->" >index.md |
| 76 | + echo "" >>index.md |
| 77 | + echo "- 4th line placeholder see below" >>index.md |
| 78 | + echo "- 5th line placeholder see below" >>index.md |
| 79 | + echo "" >>index.md |
| 80 | + echo "## Released Version Doc History" >>index.md |
| 81 | + echo "" >>index.md |
| 82 | + fi |
| 83 | + #If build running on a TAG - it's a new release - need to add it to documentation |
| 84 | + if [ -n "${TRAVIS_TAG}" ]; then |
| 85 | + latest_release=" - [${TRAVIS_TAG} documentation](${UTPLSQL_VERSION}/) - Created $now" |
| 86 | + sed -i '5s@.*@'"Latest release ${latest_release}"'@' index.md |
| 87 | + fi |
| 88 | + #replace 4th line in log |
| 89 | + sed -i '5s@.*@'"${latest}"'@' index.md |
| 90 | + #Add and Commit the changes back to pages repo. |
| 91 | + git add . |
| 92 | + git commit -m "Deploy to gh-pages branch: base commit ${SHA}" |
| 93 | + # Now that we're all set up, we can push. |
| 94 | + git push --quiet origin HEAD:${PAGES_TARGET_BRANCH} |
| 95 | + fi |
126 | 96 | fi |
0 commit comments