Skip to content

Commit 63a4164

Browse files
authored
Publish hugo to GitHub pages (#524)
1 parent 21eb98d commit 63a4164

File tree

5 files changed

+258
-3
lines changed

5 files changed

+258
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: github-pages-deployment
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
types: [opened, synchronize, reopened]
8+
release:
9+
types:
10+
- published
11+
12+
defaults:
13+
run:
14+
shell: bash +e -x -o pipefail {0}
15+
16+
jobs:
17+
github-pages-deploy:
18+
name: "Build Hugo Site and Deploy to GitHub Pages"
19+
runs-on: ubuntu-latest
20+
env:
21+
GITHUB_PAGES_REPO: https://github.com/cloudposse/docs
22+
GITHUB_PAGES_BRANCH: production
23+
GITHUB_PAGES_DIRECTORY: github_pages
24+
steps:
25+
- name: "Checkout source code at current commit"
26+
uses: actions/checkout@v2
27+
- name: "Build Hugo Static Site"
28+
uses: cloudposse/docs/actions/publish-github-pages@publish-hugo-to-github-pages
29+
env:
30+
CONTENT:
31+
HUGO_URL: https://cloudposse.github.io/docs
32+
HUGO_PUBLISH_DIR: docs
33+
HUGO_CONFIG: hugo.config.new.yml # this variable is the output location, not the input template location
34+
HTMLTEST_CONFIG: .htmltest.new.yml # this variable is the output location, not the input template location
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
37+
- name: "Deploy Hugo Static Site to GitHub Pages"
38+
uses: cloudposse/actions/github/git-push@0.30.0
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
42+
GITHUB_REPOSITORY: env.GITHUB_PAGES_REPO
43+
GIT_DIRECTORY: ./${{ env.GITHUB_PAGES_DIRECTORY }}/
44+
GIT_BRANCH: ${{ env.GITHUB_PAGES_BRANCH }}

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export DOCKER_IMAGE_NAME ?= $(DOCKER_IMAGE):$(DOCKER_TAG)
2727
export DOCKER_BUILD_FLAGS =
2828

2929
ifeq ($(wildcard /.dockerenv),)
30-
export DOCKER_RUN_FLAGS ?= -it --rm
30+
export DOCKER_RUN_FLAGS ?= --rm
3131
export DOCKER_RUN ?= docker run $(DOCKER_RUN_FLAGS) -v $(CURDIR):/src -p $(HUGO_PORT):$(HUGO_PORT) -e YARN_BUILD_DISABLED -e GITHUB_BASIC_AUTH $(DOCKER_IMAGE_NAME)
3232
else
3333
export DOCKER_RUN ?=
@@ -101,9 +101,9 @@ release: DOCKER_RUN_FLAGS += -e HUGO_CONFIG -e HTLMTEST_CONFIG -e HUGO_URL -e HU
101101
release:
102102
@[ "$(HUGO_CONFIG)" != "config.yaml" ] || (echo "Cannot release with $(HUGO_CONFIG)"; exit 1)
103103
@[ "$(HTMLTEST_CONFIG)" != ".htmltest.yml" ] || (echo "Cannot release with $(HTMLTEST_CONFIG)"; exit 1)
104-
$(DOCKER_RUN) yq eval '.baseURL = env(HUGO_URL) | .publishDir = env(HUGO_PUBLISH_DIR) | .params.editURL = env(HUGO_EDIT_URL)' config.yaml > $(HUGO_CONFIG)
104+
$(DOCKER_RUN) yq -M eval '.baseURL = env(HUGO_URL) | .publishDir = env(HUGO_PUBLISH_DIR) | .params.editURL = env(HUGO_EDIT_URL)' config.yaml > $(HUGO_CONFIG)
105105
@echo "Wrote $(HUGO_CONFIG) for $(HUGO_URL)..."
106-
$(DOCKER_RUN) yq eval '.OutputDir = "/src/.htmltest"' .htmltest.yml > $(HTMLTEST_CONFIG)
106+
$(DOCKER_RUN) yq -M eval '.OutputDir = "/src/.htmltest"' .htmltest.yml > $(HTMLTEST_CONFIG)
107107
@echo "Wrote $(HTMLTEST_CONFIG) for github actions..."
108108

109109
## Deploy static site to S3
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM alpine:3.13
2+
3+
LABEL maintainer="Cloud Posse <hello@cloudposse.com>"
4+
5+
LABEL "com.github.actions.name"="Publish Hugo to GitHub Pages"
6+
LABEL "com.github.actions.description"="Build a Hugo website and publish it to GitHub Actions."
7+
LABEL "com.github.actions.icon"="activity"
8+
LABEL "com.github.actions.color"="blue"
9+
10+
RUN apk add --no-cache \
11+
bash \
12+
ca-certificates \
13+
curl \
14+
jq \
15+
git
16+
17+
COPY entrypoint.sh /
18+
19+
RUN chmod 755 /entrypoint.sh
20+
21+
ENTRYPOINT ["/entrypoint.sh"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: 'Publish GitHub Pages'
2+
description: 'Publish the Hugo-based CloudPosse docs website to GitHub Pages.'
3+
runs:
4+
# using: 'docker'
5+
# image: 'Dockerfile'
6+
using: 'composite'
7+
steps:
8+
- run: ${{ github.action_path }}/entrypoint.sh
9+
shell: bash
10+
# - run: ./actions/publish-github-pages/entrypoint.sh
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)