From 76d9ea14519f36d8eabc06a7150d0bc6d7fdb65c Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Mon, 2 Sep 2019 01:47:55 -0700 Subject: [PATCH] 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 --- Dockerfile | 10 ++++++---- entrypoint.sh | 49 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6f8b6bb..c4e85dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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.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 "maintainer"="peaceiris" -RUN apk add --no-cache \ +RUN apt-get update && apt-get install -y --no-install-recommends \ 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" ] diff --git a/entrypoint.sh b/entrypoint.sh index 2314d36..643c144 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 if [ -z "${GITHUB_TOKEN}" ]; then - echo "error: not found GITHUB_TOKEN" + print_error "not found GITHUB_TOKEN" exit 1 fi if [ -z "${PUBLISH_BRANCH}" ]; then - echo "error: not found PUBLISH_BRANCH" + print_error "not found PUBLISH_BRANCH" exit 1 fi if [ -z "${PUBLISH_DIR}" ]; then - echo "error: not found PUBLISH_DIR" + print_error "not found PUBLISH_DIR" exit 1 fi -cd "${PUBLISH_DIR}" || exit 1 -# initialize git remote_repo="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" remote_branch="${PUBLISH_BRANCH}" -git init -git config user.name "${GITHUB_ACTOR}" -git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" -git remote add origin "${remote_repo}" + +local_dir="${HOME}/$(tr -cd 'a-f0-9' < /dev/urandom | head -c 32)" +if git clone --depth=1 --single-branch --branch "${remote_branch}" "${remote_repo}" "${local_dir}"; then + 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 -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 -timestamp=$(date -u) -git commit -m "Automated deployment: ${timestamp} ${GITHUB_SHA}" -git push origin "${remote_branch}" --force +git commit --allow-empty -m "Automated deployment: $(date -u) ${GITHUB_SHA}" +git push origin "${remote_branch}" +print_info "${GITHUB_SHA} was successfully deployed"