Fetch remote before testing whether the publish branch exists (#7)

This re-uses the existing branch rather than always overwriting it with
a new branch with a single commit.

Signed-off-by: Michael Smith <michael.smith@puppet.com>
This commit is contained in:
Michael Smith 2019-09-02 01:47:55 -07:00 committed by Shohei Ueda
parent bb5d194848
commit 76d9ea1451
2 changed files with 41 additions and 18 deletions

View File

@ -1,4 +1,4 @@
FROM alpine:3.9 FROM ubuntu:18.04
LABEL "com.github.actions.name"="Deploy to GitHub Pages for Static Site Generator" LABEL "com.github.actions.name"="Deploy to GitHub Pages for Static Site Generator"
LABEL "com.github.actions.description"="A GitHub Action to deploy your static site to GitHub Pages with Static Site Generator" LABEL "com.github.actions.description"="A GitHub Action to deploy your static site to GitHub Pages with Static Site Generator"
@ -9,9 +9,11 @@ LABEL "repository"="https://github.com/peaceiris/actions-gh-pages"
LABEL "homepage"="https://github.com/peaceiris/actions-gh-pages" LABEL "homepage"="https://github.com/peaceiris/actions-gh-pages"
LABEL "maintainer"="peaceiris" LABEL "maintainer"="peaceiris"
RUN apk add --no-cache \ RUN apt-get update && apt-get install -y --no-install-recommends \
git \ git \
openssh-client openssh-client \
ca-certificates && \
rm -rf /var/lib/apt/lists/*
ADD entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ] ENTRYPOINT [ "/entrypoint.sh" ]

View File

@ -1,33 +1,54 @@
#!/bin/sh #!/bin/bash
set -e
# set -ex
function print_error() {
echo -e "\e[31mERROR: ${1}\e[m"
}
function print_info() {
echo -e "\e[36mINFO: ${1}\e[m"
}
# check values # check values
if [ -z "${GITHUB_TOKEN}" ]; then if [ -z "${GITHUB_TOKEN}" ]; then
echo "error: not found GITHUB_TOKEN" print_error "not found GITHUB_TOKEN"
exit 1 exit 1
fi fi
if [ -z "${PUBLISH_BRANCH}" ]; then if [ -z "${PUBLISH_BRANCH}" ]; then
echo "error: not found PUBLISH_BRANCH" print_error "not found PUBLISH_BRANCH"
exit 1 exit 1
fi fi
if [ -z "${PUBLISH_DIR}" ]; then if [ -z "${PUBLISH_DIR}" ]; then
echo "error: not found PUBLISH_DIR" print_error "not found PUBLISH_DIR"
exit 1 exit 1
fi fi
cd "${PUBLISH_DIR}" || exit 1
# initialize git
remote_repo="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" remote_repo="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
remote_branch="${PUBLISH_BRANCH}" remote_branch="${PUBLISH_BRANCH}"
git init
git config user.name "${GITHUB_ACTOR}" local_dir="${HOME}/$(tr -cd 'a-f0-9' < /dev/urandom | head -c 32)"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" if git clone --depth=1 --single-branch --branch "${remote_branch}" "${remote_repo}" "${local_dir}"; then
git remote add origin "${remote_repo}" cd "${local_dir}"
git rm -r '*'
find "${GITHUB_WORKSPACE}/${PUBLISH_DIR}" -maxdepth 1 | \
tail -n +2 | \
xargs -I % cp -rf % "${local_dir}/"
else
cd "${PUBLISH_DIR}"
git init
git checkout --orphan "${remote_branch}"
fi
# push to publishing branch # push to publishing branch
git checkout "${remote_branch}" || git checkout --orphan "${remote_branch}" git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git remote rm origin || true
git remote add origin "${remote_repo}"
git add --all git add --all
timestamp=$(date -u) git commit --allow-empty -m "Automated deployment: $(date -u) ${GITHUB_SHA}"
git commit -m "Automated deployment: ${timestamp} ${GITHUB_SHA}" git push origin "${remote_branch}"
git push origin "${remote_branch}" --force print_info "${GITHUB_SHA} was successfully deployed"