|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +# |
| 4 | +# entrypoint.sh |
| 5 | +# |
| 6 | +# #### PARAMETERS #### |
| 7 | +# Input parameters: |
| 8 | +# GITHUB_PAGES_REPO - customer's repo containing documentation to be deployed to GitHub Pages |
| 9 | +# GITHUB_PAGES_BRANCH - the branch of the customer's repo which GitHub Pages will deploy from |
| 10 | +# CONTENT - comma-separated list of directories in the top level of the customer's repo that contain documentation |
| 11 | +# HUGO_URL - URL of the Hugo site after deployment |
| 12 | +# HUGO_PUBLISH_DIR - directory in the customer's repo that GitHub Pages will deploy from |
| 13 | +# HUGO_REPO - CloudPosse repository containing Hugo infrastructure |
| 14 | +# HUGO_CONFIG - location of to-be-written Hugo config file (actual location not important) |
| 15 | +# HTMLTEST_CONFIG - location of to-be-written htmltest config file (actual location not important) |
| 16 | + |
| 17 | +# Example parameter values: |
| 18 | +# GITHUB_PAGES_REPO=https://github.com/cloudposse/docs # or for customer github.com/customer/infrastructure.git |
| 19 | +# GITHUB_PAGES_BRANCH=production # or for customer, it will be docs |
| 20 | +# CONTENT=docs,content |
| 21 | +# HUGO_URL=cloudposse.github.io/docs |
| 22 | +# HUGO_PUBLISH_DIR=public |
| 23 | +# HUGO_REPO=https://github.com/cloudposse/docs |
| 24 | +# HUGO_CONFIG=hugo.config.new |
| 25 | +# HTMLTEST_CONFIG=.htmltest.config.new |
| 26 | + |
| 27 | +# Parameter with default: |
| 28 | +HUGO_REPO=${HUGO_REPO:-https://github.com/cloudposse/docs} |
| 29 | + |
| 30 | +# Hardcoded parameters: |
| 31 | +GITHUB_PAGES_PULL_PATH=/tmp/master/ # This will contain the master branch of GITHUB_PAGES_REPO. |
| 32 | +GITHUB_PAGES_HUGO_PATH=/tmp/hugo/ # This will contain the generic infrastructure needed to build the GitHub Pages site. |
| 33 | +GITHUB_PAGES_PUSH_PATH=$(pwd)/${GITHUB_PAGES_DIRECTORY}/ # This will contain the GitHub Pages deployment branch of GITHUB_PAGES_REPO. |
| 34 | +GIT_USER_EMAIL=github-actions-runner@cloudposse.com |
| 35 | +GIT_USER_NAME=github-actions-runner |
| 36 | +STAGING_DIR=/tmp/staging/ # Staging directory used for preparing files before hugo generation |
| 37 | + |
| 38 | +# #### PROGRAM LOGIC #### |
| 39 | +main() { |
| 40 | + # Check out |
| 41 | + # 1) Essential Hugo build tools |
| 42 | + git clone $HUGO_REPO $GITHUB_PAGES_HUGO_PATH |
| 43 | + # 2) Site-specific documentation |
| 44 | + git clone $GITHUB_PAGES_REPO $GITHUB_PAGES_PULL_PATH |
| 45 | + # 3) The GitHub Pages deployment branch for this site |
| 46 | + git clone --branch $GITHUB_PAGES_BRANCH $GITHUB_PAGES_REPO $GITHUB_PAGES_PUSH_PATH |
| 47 | + |
| 48 | + if [[ -n $DEBUG ]]; then |
| 49 | + echo "Repo: ${HUGO_REPO}" |
| 50 | + echo "Branch: master" |
| 51 | + echo "${GITHUB_PAGES_HUGO_PATH}" |
| 52 | + ls -lhat ${GITHUB_PAGES_HUGO_PATH} |
| 53 | + echo "Repo: ${GITHUB_PAGES_REPO}" |
| 54 | + echo "Branch: master" |
| 55 | + echo "${GITHUB_PAGES_PULL_PATH}" |
| 56 | + ls -lhat ${GITHUB_PAGES_PULL_PATH} |
| 57 | + echo "Repo: ${GITHUB_PAGES_REPO}" |
| 58 | + echo "Branch: ${GITHUB_PAGES_BRANCH}" |
| 59 | + echo "${GITHUB_PAGES_PUSH_PATH}" |
| 60 | + ls -lhat ${GITHUB_PAGES_PUSH_PATH} |
| 61 | + echo "${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR}" |
| 62 | + ls -lhat ${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR} |
| 63 | + fi |
| 64 | + |
| 65 | + # Create a separate build folder, ${STAGING_DIR}, and populate it with the essential files from HUGO_REPO |
| 66 | + # (The rest of this script assumes HUGO_REPO=https://github.com/cloudposse/docs.) |
| 67 | + mkdir ${STAGING_DIR} |
| 68 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/tasks/ ${STAGING_DIR} |
| 69 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/themes/ ${STAGING_DIR} |
| 70 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/static/ ${STAGING_DIR} |
| 71 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/layouts/ ${STAGING_DIR} |
| 72 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/content/ ${STAGING_DIR} |
| 73 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/Dockerfile ${STAGING_DIR} |
| 74 | + cp -r ${GITHUB_PAGES_HUGO_PATH}/.gitignore ${STAGING_DIR} |
| 75 | + cp ${GITHUB_PAGES_HUGO_PATH}/config.yaml ${STAGING_DIR} |
| 76 | + cp ${GITHUB_PAGES_HUGO_PATH}/.htmltest.yml ${STAGING_DIR} |
| 77 | + cp ${GITHUB_PAGES_HUGO_PATH}/Makefile ${STAGING_DIR} |
| 78 | + # The following two lines can be removed once this branch is merged into master |
| 79 | + sed -i 's/yq eval/yq -M eval/' ${STAGING_DIR}Makefile # this can be removed once this branch is merged into master |
| 80 | + sed -i 's/^export DOCKER_RUN_FLAGS ?= -it --rm$/export DOCKER_RUN_FLAGS ?= --rm/' ${STAGING_DIR}Makefile |
| 81 | + |
| 82 | + # copy all customer documentation into the build folder |
| 83 | + if [[ -n $DEBUG ]]; then |
| 84 | + echo "${STAGING_DIR}" |
| 85 | + ls -laht ${STAGING_DIR} |
| 86 | + echo "${STAGING_DIR}/content" |
| 87 | + ls -laht ${STAGING_DIR}/content |
| 88 | + echo "${STAGING_DIR}/content/reference" |
| 89 | + ls -laht ${STAGING_DIR}/content/reference |
| 90 | + fi |
| 91 | + IFS="," read -r -a CONTENT_ARRAY <<< "$CONTENT" |
| 92 | + for content in "${CONTENT_ARRAY[@]}"; do |
| 93 | + # clear files needed for storing intermediate variables |
| 94 | + rm -f file_origins.txt file_destinations.txt |
| 95 | + # rename all `README.md` to `_index.md` |
| 96 | + find ${GITHUB_PAGES_PULL_PATH}${content} -type f -name README.md -print0 | xargs --null -I{} bash -c 'mv {} $(dirname {})/_index.md' |
| 97 | + # categories with no subfolders, and only a single `_index.md`: `mv foobar/_index.md foobar.md` |
| 98 | + if [[ -n $DEBUG ]]; then |
| 99 | + ls -R ${GITHUB_PAGES_PULL_PATH}${content} |
| 100 | + fi |
| 101 | + find ${GITHUB_PAGES_PULL_PATH}${content} -type f -name _index.md > files.txt |
| 102 | + readarray -t FILES < files.txt |
| 103 | + for i in "${!FILES[@]}"; do |
| 104 | + dirname_result=$(dirname ${FILES[i]}) |
| 105 | + echo "dirname_result: $dirname_result" |
| 106 | + test_condition=$(ls -1q ${dirname_result}/*.md | wc -l) |
| 107 | + echo "test_condition: $test_condition" |
| 108 | + if [ "$test_condition" == "1" ]; then |
| 109 | + echo "$(dirname ${FILES[i]})" |
| 110 | + mv ${FILES[i]} $(dirname ${dirname_result})/$(basename ${dirname_result}).md |
| 111 | + rm -r ${dirname_result} |
| 112 | + ls $(dirname ${dirname_result}) |
| 113 | + fi |
| 114 | + done |
| 115 | + if [[ -n $DEBUG ]]; then |
| 116 | + ls -R ${GITHUB_PAGES_PULL_PATH}${content} |
| 117 | + fi |
| 118 | + # install the customer docs (.md pages) to the content folder |
| 119 | + find ${GITHUB_PAGES_PULL_PATH}${content} -type f -name "*.md" >> file_origins.txt |
| 120 | + readarray -t FILE_ORIGINS < file_origins.txt |
| 121 | + presence_of_subdirs=$(find ${GITHUB_PAGES_PULL_PATH}${content} -type d | wc -l) |
| 122 | + echo "dir: $content" |
| 123 | + echo "presence_of_subdirs: $presence_of_subdirs" |
| 124 | + if [[ "${presence_of_subdirs}" == "1" ]]; then |
| 125 | + # there are no subdirs, so every .md file in this folder becomes a top-level object |
| 126 | + for i in "${!FILE_ORIGINS[@]}"; do |
| 127 | + echo "${STAGING_DIR}content/$(basename ${FILE_ORIGINS[i]})" | sed -e "s|$GITHUB_PAGES_PULL_PATH||" >> file_destinations.txt |
| 128 | + done |
| 129 | + else |
| 130 | + # there are subdirs, so we're gonna preserve the file heirarchy |
| 131 | + for i in "${!FILE_ORIGINS[@]}"; do |
| 132 | + echo "${STAGING_DIR}content/${FILE_ORIGINS[i]}" | sed -e "s|$GITHUB_PAGES_PULL_PATH||" >> file_destinations.txt |
| 133 | + done |
| 134 | + fi |
| 135 | + readarray -t FILE_DESTINATIONS < file_destinations.txt |
| 136 | + for i in "${!FILE_ORIGINS[@]}"; do |
| 137 | + echo "Copying ${FILE_ORIGINS[i]} to ${FILE_DESTINATIONS[i]}." |
| 138 | + mkdir -p ${FILE_DESTINATIONS[i]} && cp ${FILE_ORIGINS[i]} ${FILE_DESTINATIONS[i]} |
| 139 | + done |
| 140 | + done |
| 141 | + |
| 142 | + if [[ -n $DEBUG ]]; then |
| 143 | + echo "${STAGING_DIR}" |
| 144 | + ls -laht ${STAGING_DIR} |
| 145 | + echo "${STAGING_DIR}/content" |
| 146 | + ls -laht ${STAGING_DIR}/content |
| 147 | + echo "${STAGING_DIR}/content/reference" |
| 148 | + ls -laht ${STAGING_DIR}/content/reference |
| 149 | + fi |
| 150 | + |
| 151 | + # Build Docker image needed to build the Hugo site |
| 152 | + cd ${STAGING_DIR} |
| 153 | + docker build -t cloudposse/docs . |
| 154 | + |
| 155 | + # publish the Hugo-generated HTML to $GITHUB_PAGES_PUSH_PATH |
| 156 | + make release |
| 157 | + make real-clean hugo/build |
| 158 | + cp -r ${HUGO_PUBLISH_DIR} ${GITHUB_PAGES_PUSH_PATH} |
| 159 | + |
| 160 | + if [[ -n $DEBUG ]]; then |
| 161 | + echo "Repo: ${HUGO_REPO}" |
| 162 | + echo "Branch: master" |
| 163 | + echo "${GITHUB_PAGES_HUGO_PATH}" |
| 164 | + ls -lhat ${GITHUB_PAGES_HUGO_PATH} |
| 165 | + echo "Repo: ${GITHUB_PAGES_REPO}" |
| 166 | + echo "Branch: master" |
| 167 | + echo "${GITHUB_PAGES_PULL_PATH}" |
| 168 | + ls -lhat ${GITHUB_PAGES_PULL_PATH} |
| 169 | + echo "Repo: ${GITHUB_PAGES_REPO}" |
| 170 | + echo "Branch: ${GITHUB_PAGES_BRANCH}" |
| 171 | + echo "${GITHUB_PAGES_PUSH_PATH}" |
| 172 | + ls -lhat ${GITHUB_PAGES_PUSH_PATH} |
| 173 | + echo "${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR}" |
| 174 | + ls -lhat ${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR} |
| 175 | + echo "${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR}/reference" |
| 176 | + ls -lhat ${GITHUB_PAGES_PUSH_PATH}/${HUGO_PUBLISH_DIR}/reference |
| 177 | + fi |
| 178 | +} |
| 179 | + |
| 180 | +main |
0 commit comments