github-pages-deploy-action/entrypoint.sh

93 lines
2.7 KiB
Bash
Raw Permalink Normal View History

2019-03-03 07:43:28 +08:00
#!/bin/sh -l
2019-03-04 05:12:26 +08:00
set -e
if [ -z "$ACCESS_TOKEN" ] && [ -z "$GITHUB_TOKEN" ]
2019-03-04 05:12:26 +08:00
then
echo "You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."
2019-03-04 05:12:26 +08:00
exit 1
fi
2019-03-03 07:43:28 +08:00
if [ -z "$BRANCH" ]
then
2019-03-04 00:26:20 +08:00
echo "You must provide the action with a branch name it should deploy to, for example gh-pages or docs."
2019-03-03 07:43:28 +08:00
exit 1
fi
if [ -z "$FOLDER" ]
then
2019-03-03 23:05:36 +08:00
echo "You must provide the action with the folder name in the repository where your compiled page lives."
2019-03-03 23:59:11 +08:00
exit 1
2019-03-03 07:43:28 +08:00
fi
case "$FOLDER" in /*|./*)
echo "The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly."
exit 1
esac
# Installs Git and jq.
apt-get update && \
apt-get install -y git && \
apt-get install -y jq && \
# Gets the commit email/name if it exists in the push event payload.
COMMIT_EMAIL=`jq '.pusher.email' ${GITHUB_EVENT_PATH}`
COMMIT_NAME=`jq '.pusher.name' ${GITHUB_EVENT_PATH}`
# If the commit email/name is not found in the event payload then it falls back to the actor.
2019-03-04 05:12:26 +08:00
if [ -z "$COMMIT_EMAIL" ]
2019-03-04 04:45:52 +08:00
then
COMMIT_EMAIL="${GITHUB_ACTOR:-github-pages-deploy-action}@users.noreply.github.com"
2019-03-04 04:45:52 +08:00
fi
2019-03-04 05:12:26 +08:00
if [ -z "$COMMIT_NAME" ]
then
COMMIT_NAME="${GITHUB_ACTOR:-GitHub Pages Deploy Action}"
2019-03-04 04:57:51 +08:00
fi
2019-03-04 05:12:26 +08:00
# Directs the action to the the Github workspace.
2019-03-03 07:43:28 +08:00
cd $GITHUB_WORKSPACE && \
# Configures Git.
2019-03-04 04:14:05 +08:00
git init && \
2019-03-04 05:12:26 +08:00
git config --global user.email "${COMMIT_EMAIL}" && \
git config --global user.name "${COMMIT_NAME}" && \
2019-03-04 23:19:38 +08:00
## Initializes the repository path using the access token.
REPOSITORY_PATH="https://${ACCESS_TOKEN:-"x-access-token:$GITHUB_TOKEN"}@github.com/${GITHUB_REPOSITORY}.git" && \
2019-03-04 23:19:38 +08:00
2019-03-04 23:07:24 +08:00
# Checks to see if the remote exists prior to deploying.
# If the branch doesn't exist it gets created here as an orphan.
2019-03-04 23:54:21 +08:00
if [ "$(git ls-remote --heads "$REPOSITORY_PATH" "$BRANCH" | wc -l)" -eq 0 ];
then
2019-03-04 21:41:33 +08:00
echo "Creating remote branch ${BRANCH} as it doesn't exist..."
2019-03-04 23:40:11 +08:00
git checkout "${BASE_BRANCH:-master}" && \
git checkout --orphan $BRANCH && \
git rm -rf . && \
touch README.md && \
git add README.md && \
git commit -m "Initial ${BRANCH} commit" && \
2019-03-04 23:19:38 +08:00
git push $REPOSITORY_PATH $BRANCH
fi
# Checks out the base branch to begin the deploy process.
2019-03-04 00:17:43 +08:00
git checkout "${BASE_BRANCH:-master}" && \
2019-03-03 07:43:28 +08:00
2019-03-04 05:12:26 +08:00
# Builds the project if a build script is provided.
2019-03-04 21:39:18 +08:00
echo "Running build scripts... $BUILD_SCRIPT" && \
eval "$BUILD_SCRIPT" && \
2019-03-03 07:43:28 +08:00
2019-03-18 11:50:57 +08:00
if [ "$CNAME" ]; then
2019-03-18 11:56:01 +08:00
echo "Generating a CNAME file in in the $FOLDER directory..."
2019-03-18 11:50:57 +08:00
echo $CNAME > $FOLDER/CNAME
fi
2019-03-03 07:43:28 +08:00
# Commits the data to Github.
2019-03-04 05:12:26 +08:00
echo "Deploying to GitHub..." && \
2019-03-03 23:26:44 +08:00
git add -f $FOLDER && \
2019-03-18 11:50:57 +08:00
2019-09-22 20:27:12 +08:00
git commit -m "Deploying to ${BRANCH} from ${BASE_BRANCH:-master} ${GITHUB_SHA}" --quiet && \
git push $REPOSITORY_PATH `git subtree split --prefix $FOLDER ${BASE_BRANCH:-master}`:$BRANCH --force && \
echo "Deployment succesful!"