Skip to content

Commit

Permalink
Update for CNAME and Private Repos
Browse files Browse the repository at this point in the history
  • Loading branch information
marksie1988 committed Jul 26, 2021
1 parent a888437 commit 8424730
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ to navigate to your GitHub Pages site.
Inputs
******

====================== ================================================================================================
====================== =============================================================
Directive Name Description (Docutils version added to, in [brackets])
====================== ================================================================================================
====================== =============================================================
token the `GITHUB_TOKEN` secret. This is mandatory unless
`build_only` is set to `true`.
sphinx_env The Sphinx environment to build (default to `production`)
Expand All @@ -139,7 +139,8 @@ build_only When set to `true`, the Sphinx site will be built but no
pre_build_commands Commands to run prior to build and deploy. Useful for
ensuring build dependencies are up to date or installing
new dependencies.
====================== ================================================================================================
cname Contents of CNAME file, this will be copied over.
====================== =============================================================


Example Usage:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ inputs:
new dependencies. For example, use `apk --update add
imagemagick` to install ImageMagick.
required: false
cname:
description: 'contents of the CNAME file'
required: false
runs:
using: 'docker'
image: 'Dockerfile'
Expand Down
50 changes: 44 additions & 6 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,53 @@ else
fi

if [ -n "${INPUT_TARGET_BRANCH}" ]; then
# If a target branch is provided, use it
remote_branch="${INPUT_TARGET_BRANCH}"
echo "::debug::target branch is set via input parameter"
else
response=$(curl -sH "Authorization: token ${INPUT_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pages")
remote_branch=$(echo "$response" | awk -F'"' '/\"branch\"/ { print $4 }')
if [ -z "${remote_branch}" ]; then
echo "::error::Cannot get GitHub Pages source branch via API."
echo "::error::${response}"
# Figure out the GH Pages branch. Only if its a public repo.
repo_details_response=$(curl -sH "Authorization: token ${INPUT_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}")
is_private=$(echo "$repo_details_response" | awk -F'[:,]' '/\"private\"/ { print $2 }')
echo "::debug::Is this repo private? ${is_private}"
if [ -z "${is_private}" ]; then
echo "::error::Cannot get repository visibility via API."
echo "::error::${repo_details_response}"
exit 1
elif [ $is_private = false ]; then
# This is a public repo. Getting the Pages settings
remote_branch_response=$(curl -sH "Authorization: token ${INPUT_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pages")
remote_branch=$(echo "$remote_branch_response" | awk -F'"' '/\"branch\"/ { print $4 }')
echo "::debug::Pages' branch from settings is ${remote_branch}"

if [ -z "${remote_branch}" ]; then
echo "::error::Cannot get GitHub Pages source branch via API."
echo "::error::${remote_branch_response}"
exit 1
fi
else
# This is a private repo.
# Is this a regular repo or an org.github.io type of repo
case "${GITHUB_REPOSITORY}" in
*.github.io)
default_branch=$(echo "$repo_details_response" | awk -F'"' '/\"default_branch\\"/ { print $4 }')
echo "::debug::Repo default branch is ${default_branch}"

if [ -z "${default_branch}" ]; then
echo "::error::Cannot get the default branch via API."
echo "::error::${default_branch_response}"
exit 1
fi
remote_branch=$default_branch ;;
*)
remote_branch="gh-pages" ;;
esac
fi
fi

echo "::debug::Remote branch is ${remote_branch}"

REMOTE_REPO="https://${GITHUB_ACTOR}:${INPUT_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
echo "::debug::Remote is ${REMOTE_REPO}"
BUILD_DIR="${GITHUB_WORKSPACE}/../sphinx_build"
Expand Down Expand Up @@ -111,6 +145,10 @@ fi

cd ${BUILD_DIR}

if [ -n "${INPUT_CNAME}" ]; then
echo $INPUT_CNAME > CNAME
fi

# No need to have GitHub Pages to run Jekyll
touch .nojekyll

Expand Down

0 comments on commit 8424730

Please sign in to comment.