2019-09-02 01:47:55 -07:00
|
|
|
#!/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"
|
|
|
|
}
|
2019-05-22 06:53:54 +09:00
|
|
|
|
2019-09-23 14:00:10 +09:00
|
|
|
function skip() {
|
|
|
|
print_info "No changes detected, skipping deployment"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:43:26 +09:00
|
|
|
# check values
|
2019-10-06 18:41:46 +08:00
|
|
|
if [ -n "${EXTERNAL_REPOSITORY}" ]; then
|
|
|
|
PUBLISH_REPOSITORY=${EXTERNAL_REPOSITORY}
|
|
|
|
else
|
|
|
|
PUBLISH_REPOSITORY=${GITHUB_REPOSITORY}
|
|
|
|
fi
|
|
|
|
print_info "Deploy to ${PUBLISH_REPOSITORY}"
|
|
|
|
|
2019-09-05 23:07:49 +09:00
|
|
|
if [ -n "${ACTIONS_DEPLOY_KEY}" ]; then
|
|
|
|
|
|
|
|
print_info "setup with ACTIONS_DEPLOY_KEY"
|
|
|
|
|
2019-10-06 23:53:36 +09:00
|
|
|
if [ -n "${SCRIPT_MODE}" ]; then
|
|
|
|
print_info "run as SCRIPT_MODE"
|
|
|
|
SSH_DIR="${HOME}/.ssh"
|
|
|
|
else
|
|
|
|
SSH_DIR="/root/.ssh"
|
|
|
|
fi
|
|
|
|
mkdir "${SSH_DIR}"
|
|
|
|
ssh-keyscan -t rsa github.com > "${SSH_DIR}/known_hosts"
|
|
|
|
echo "${ACTIONS_DEPLOY_KEY}" > "${SSH_DIR}/id_rsa"
|
|
|
|
chmod 400 "${SSH_DIR}/id_rsa"
|
2019-09-05 23:07:49 +09:00
|
|
|
|
2019-10-06 18:41:46 +08:00
|
|
|
remote_repo="git@github.com:${PUBLISH_REPOSITORY}.git"
|
2019-09-05 23:07:49 +09:00
|
|
|
|
2019-09-08 04:24:03 +09:00
|
|
|
elif [ -n "${PERSONAL_TOKEN}" ]; then
|
|
|
|
|
|
|
|
print_info "setup with PERSONAL_TOKEN"
|
|
|
|
|
2019-10-06 18:41:46 +08:00
|
|
|
remote_repo="https://x-access-token:${PERSONAL_TOKEN}@github.com/${PUBLISH_REPOSITORY}.git"
|
2019-09-08 04:24:03 +09:00
|
|
|
|
2019-09-05 23:07:49 +09:00
|
|
|
elif [ -n "${GITHUB_TOKEN}" ]; then
|
|
|
|
|
|
|
|
print_info "setup with GITHUB_TOKEN"
|
2019-10-06 23:53:36 +09:00
|
|
|
print_error "GITHUB_TOKEN works only private repo, See #9"
|
2019-09-05 23:07:49 +09:00
|
|
|
|
2019-10-06 18:41:46 +08:00
|
|
|
if [ -n "${EXTERNAL_REPOSITORY}" ]; then
|
2020-01-19 00:07:44 +09:00
|
|
|
print_error "can not use GITHUB_TOKEN to deploy to an external repository"
|
2019-10-06 18:41:46 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
remote_repo="https://x-access-token:${GITHUB_TOKEN}@github.com/${PUBLISH_REPOSITORY}.git"
|
2019-09-05 23:07:49 +09:00
|
|
|
|
|
|
|
else
|
2019-09-08 04:24:03 +09:00
|
|
|
print_error "not found ACTIONS_DEPLOY_KEY, PERSONAL_TOKEN, or GITHUB_TOKEN"
|
2019-08-07 22:43:26 +09:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${PUBLISH_BRANCH}" ]; then
|
2019-09-02 01:47:55 -07:00
|
|
|
print_error "not found PUBLISH_BRANCH"
|
2019-05-22 06:53:54 +09:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-05-26 22:37:08 +09:00
|
|
|
if [ -z "${PUBLISH_DIR}" ]; then
|
2019-09-02 01:47:55 -07:00
|
|
|
print_error "not found PUBLISH_DIR"
|
2019-05-22 06:53:54 +09:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-08-07 22:43:26 +09:00
|
|
|
|
2019-05-22 06:53:54 +09:00
|
|
|
remote_branch="${PUBLISH_BRANCH}"
|
2019-09-02 01:47:55 -07:00
|
|
|
|
2019-10-06 23:53:36 +09:00
|
|
|
local_dir="${HOME}/ghpages_${RANDOM}"
|
2019-12-23 05:49:21 +09:00
|
|
|
|
|
|
|
if [[ "${INPUT_FORCEORPHAN}" == "true" ]]; then
|
|
|
|
print_info "force ophan: ${INPUT_FORCEORPHAN}"
|
|
|
|
cd "${PUBLISH_DIR}"
|
|
|
|
git init
|
|
|
|
git checkout --orphan "${remote_branch}"
|
|
|
|
elif git clone --depth=1 --single-branch --branch "${remote_branch}" "${remote_repo}" "${local_dir}"; then
|
2019-09-02 01:47:55 -07:00
|
|
|
cd "${local_dir}"
|
2019-09-24 19:41:21 +02:00
|
|
|
|
2019-09-25 03:00:25 +09:00
|
|
|
if [[ ${INPUT_KEEPFILES} == "true" ]]; then
|
|
|
|
print_info "Keeping existing files: ${INPUT_KEEPFILES}"
|
|
|
|
else
|
2019-09-24 19:41:21 +02:00
|
|
|
git rm -r --ignore-unmatch '*'
|
|
|
|
fi
|
|
|
|
|
2019-12-24 15:02:00 +09:00
|
|
|
find "${GITHUB_WORKSPACE}/${PUBLISH_DIR}" -maxdepth 1 -not -name ".git" -not -name ".github" | \
|
2019-09-02 01:47:55 -07:00
|
|
|
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
|
2019-12-24 15:22:01 +09:00
|
|
|
if [[ -n "${INPUT_USERNAME}" ]]; then
|
|
|
|
git config user.name "${INPUT_USERNAME}"
|
|
|
|
else
|
|
|
|
git config user.name "${GITHUB_ACTOR}"
|
|
|
|
fi
|
|
|
|
if [[ -n "${INPUT_USEREMAIL}" ]]; then
|
|
|
|
git config user.email "${INPUT_USEREMAIL}"
|
|
|
|
else
|
|
|
|
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
|
|
|
|
fi
|
2019-09-02 01:47:55 -07:00
|
|
|
git remote rm origin || true
|
2019-05-22 06:53:54 +09:00
|
|
|
git remote add origin "${remote_repo}"
|
|
|
|
git add --all
|
2019-09-14 13:49:20 -05:00
|
|
|
|
|
|
|
print_info "Allowing empty commits: ${INPUT_EMPTYCOMMITS}"
|
2020-01-06 23:10:35 +09:00
|
|
|
|
2020-01-06 23:46:48 +09:00
|
|
|
if [ -n "${INPUT_COMMITMESSAGE}" ]; then
|
|
|
|
BASE_COMMIT_MESSAGE="${INPUT_COMMITMESSAGE}"
|
|
|
|
else
|
|
|
|
BASE_COMMIT_MESSAGE="Automated deployment: $(date -u)"
|
|
|
|
fi
|
|
|
|
|
2020-01-06 23:10:35 +09:00
|
|
|
if [ -n "${EXTERNAL_REPOSITORY}" ]; then
|
2020-01-06 23:46:48 +09:00
|
|
|
COMMIT_MESSAGE="${BASE_COMMIT_MESSAGE} ${GITHUB_REPOSITORY}@${GITHUB_SHA}"
|
2020-01-06 23:10:35 +09:00
|
|
|
else
|
2020-01-06 23:46:48 +09:00
|
|
|
COMMIT_MESSAGE="${BASE_COMMIT_MESSAGE} ${GITHUB_SHA}"
|
2020-01-06 23:10:35 +09:00
|
|
|
fi
|
|
|
|
|
2019-09-23 14:00:10 +09:00
|
|
|
if [[ ${INPUT_EMPTYCOMMITS} == "false" ]]; then
|
|
|
|
git commit -m "${COMMIT_MESSAGE}" || skip
|
2019-09-14 13:49:20 -05:00
|
|
|
else
|
2019-09-23 14:00:10 +09:00
|
|
|
git commit --allow-empty -m "${COMMIT_MESSAGE}"
|
2019-09-14 13:49:20 -05:00
|
|
|
fi
|
|
|
|
|
2019-12-23 05:49:21 +09:00
|
|
|
if [[ ${INPUT_FORCEORPHAN} == "true" ]]; then
|
|
|
|
git push origin --force "${remote_branch}"
|
|
|
|
else
|
|
|
|
git push origin "${remote_branch}"
|
|
|
|
fi
|
|
|
|
|
2020-01-18 12:55:18 +09:00
|
|
|
if [[ -n "${INPUT_TAGNAME}" ]]; then
|
|
|
|
print_info "Tag name: ${INPUT_TAGNAME}"
|
|
|
|
print_info "Tag message: ${INPUT_TAGMESSAGE}"
|
|
|
|
print_info "Tag overwrite: ${INPUT_TAGOVERWRITE}"
|
|
|
|
if [[ -n "${INPUT_TAGMESSAGE}" ]]; then
|
|
|
|
GIT_TAG_MESSAGE="${INPUT_TAGMESSAGE}"
|
|
|
|
else
|
|
|
|
GIT_TAG_MESSAGE="Deployment ${INPUT_TAGNAME}"
|
|
|
|
fi
|
|
|
|
if [[ "${INPUT_TAGOVERWRITE}" == "true" ]]; then
|
|
|
|
GIT_TAG_OPTION="--force"
|
|
|
|
else
|
|
|
|
GIT_TAG_OPTION=""
|
|
|
|
fi
|
|
|
|
git tag "${GIT_TAG_OPTION}" -a "${INPUT_TAGNAME}" -m "${GIT_TAG_MESSAGE}"
|
|
|
|
git push "${GIT_TAG_OPTION}" origin "${INPUT_TAGNAME}"
|
|
|
|
fi
|
|
|
|
|
2019-09-02 01:47:55 -07:00
|
|
|
print_info "${GITHUB_SHA} was successfully deployed"
|