2019-09-02 08:47:55 +00: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-21 21:53:54 +00:00
|
|
|
|
2019-08-07 13:43:26 +00:00
|
|
|
# check values
|
2019-09-05 14:07:49 +00:00
|
|
|
if [ -n "${ACTIONS_DEPLOY_KEY}" ]; then
|
|
|
|
|
|
|
|
print_info "setup with ACTIONS_DEPLOY_KEY"
|
|
|
|
|
|
|
|
mkdir /root/.ssh
|
|
|
|
ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts
|
|
|
|
echo "${ACTIONS_DEPLOY_KEY}" > /root/.ssh/id_rsa
|
|
|
|
chmod 400 /root/.ssh/id_rsa
|
|
|
|
|
|
|
|
remote_repo="git@github.com:${GITHUB_REPOSITORY}.git"
|
|
|
|
|
|
|
|
elif [ -n "${GITHUB_TOKEN}" ]; then
|
|
|
|
|
|
|
|
print_info "setup with GITHUB_TOKEN"
|
|
|
|
|
|
|
|
remote_repo="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
|
|
|
|
|
|
|
|
else
|
|
|
|
print_error "not found ACTIONS_DEPLOY_KEY or GITHUB_TOKEN"
|
2019-08-07 13:43:26 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${PUBLISH_BRANCH}" ]; then
|
2019-09-02 08:47:55 +00:00
|
|
|
print_error "not found PUBLISH_BRANCH"
|
2019-05-21 21:53:54 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-05-26 13:37:08 +00:00
|
|
|
if [ -z "${PUBLISH_DIR}" ]; then
|
2019-09-02 08:47:55 +00:00
|
|
|
print_error "not found PUBLISH_DIR"
|
2019-05-21 21:53:54 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-08-07 13:43:26 +00:00
|
|
|
|
2019-05-21 21:53:54 +00:00
|
|
|
remote_branch="${PUBLISH_BRANCH}"
|
2019-09-02 08:47:55 +00:00
|
|
|
|
|
|
|
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
|
2019-05-21 21:53:54 +00:00
|
|
|
git config user.name "${GITHUB_ACTOR}"
|
|
|
|
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
|
2019-09-02 08:47:55 +00:00
|
|
|
git remote rm origin || true
|
2019-05-21 21:53:54 +00:00
|
|
|
git remote add origin "${remote_repo}"
|
|
|
|
git add --all
|
2019-09-02 08:47:55 +00:00
|
|
|
git commit --allow-empty -m "Automated deployment: $(date -u) ${GITHUB_SHA}"
|
|
|
|
git push origin "${remote_branch}"
|
|
|
|
print_info "${GITHUB_SHA} was successfully deployed"
|