Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreytse committed Jul 17, 2023
1 parent d90a835 commit 6a28d87
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ jobs:
with:
provider: 'github'
token: ${{ secrets.GITHUB_TOKEN }}
ssh_private_key: ''
jekyll_src: './test_site'
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
token:
description: 'The deploy token'
required: false
ssh_private_key:
description: 'The SSH private key for deployment'
required: false
repository:
description: 'The deploy repository'
required: false
Expand Down
22 changes: 20 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ WORKING_DIR=${PWD}
# Initial default value
PROVIDER=${INPUT_PROVIDER:=github}
TOKEN=${INPUT_TOKEN}
SSH_PRIVATE_KEY=${INPUT_SSH_PRIVATE_KEY}
ACTOR=${INPUT_ACTOR}
REPOSITORY=${INPUT_REPOSITORY}
BRANCH=${INPUT_BRANCH}
Expand Down Expand Up @@ -121,14 +122,31 @@ build_jekyll || {
build_jekyll
}

# Pre-handle SSH private key
if [[ -n "${SSH_PRIVATE_KEY}" ]]; then
echo "Pre-handle SSH private key file"
SSH_PRIVATE_KEY_PATH=$(mktemp /tmp/ssh-priv-key.XXXXXX)
echo "${SSH_PRIVATE_KEY}" > ${SSH_PRIVATE_KEY_PATH}
# To prevent from permissions are too open issue, the key can be
# only readable by self
chmod 400 ${SSH_PRIVATE_KEY_PATH}
fi

cd ${WORKING_DIR}/build

# Check if deploy on the same repository branch
PROVIDER_EXIT_CODE=0
if [[ "${PROVIDER}" == "github" ]]; then
source "${SCRIPT_DIR}/providers/github.sh"
else
echo "${PROVIDER} is an unsupported provider."
exit 1
PROVIDER_EXIT_CODE=1
fi

# Post-handle SSH private key
if [[ -n "${SSH_PRIVATE_KEY}" ]]; then
echo "Post-handle SSH private key file"
rm -f ${SSH_PRIVATE_KEY_PATH}
fi

exit $?
exit ${PROVIDER_EXIT_CODE}
28 changes: 18 additions & 10 deletions providers/github.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
#!/bin/sh
#!/bin/bash
set -e

# Check if deploy to same branch
if [ "${REPOSITORY}" = "${GITHUB_REPOSITORY}" ]; then
if [ "${GITHUB_REF}" = "refs/heads/${BRANCH}" ]; then
if [[ "${REPOSITORY}" = "${GITHUB_REPOSITORY}" ]]; then
if [[ "${GITHUB_REF}" = "refs/heads/${BRANCH}" ]]; then
echo "It's conflicted to deploy on same branch ${BRANCH}"
exit 1
fi
fi

# Tell GitHub Pages not to run Jekyll
touch .nojekyll
[ -n "$INPUT_CNAME" ] && echo "$INPUT_CNAME" > CNAME
[[ -n "$INPUT_CNAME" ]] && echo "$INPUT_CNAME" > CNAME

# Prefer to use SSH approach when SSH private key is provided
if [[ -n "${SSH_PRIVATE_KEY}" ]]; then
export GIT_SSH_COMMAND="ssh -i ${SSH_PRIVATE_KEY_PATH} \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null"
REMOTE_REPO="git@github.com:${REPOSITORY}.git"
else
REMOTE_REPO="https://${ACTOR}:${TOKEN}@github.com/${REPOSITORY}.git"
fi

echo "Deploying to ${REPOSITORY} on branch ${BRANCH}"
echo "Deploying to https://${ACTOR}:${TOKEN}@github.com/${REPOSITORY}.git"
echo "Deploying to ${REMOTE_REPO}"

REMOTE_REPO="https://${ACTOR}:${TOKEN}@github.com/${REPOSITORY}.git" && \
git config --global http.postBuffer 524288000 && \
git config --global init.defaultBranch main && \
git config --global init.defaultBranch main && \
git init && \
git config user.name "${ACTOR}" && \
git config user.email "${ACTOR}@users.noreply.github.com" && \
git add . && \
git commit -m "jekyll build from Action ${GITHUB_SHA}" && \
git push --force $REMOTE_REPO main:$BRANCH && \
fuser -k .git || rm -rf .git && \
(fuser -k .git || rm -rf .git) && \
cd ..

exit $?
PROVIDER_EXIT_CODE=$?
3 changes: 3 additions & 0 deletions script/init_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pacman -Syu --noconfirm
# Installing git package
pacman -S --noconfirm git

# Installing openssh package
pacman -S --noconfirm openssh

# Installing ruby libraries
pacman -S --noconfirm ruby2.7 ruby-bundler

Expand Down

0 comments on commit 6a28d87

Please sign in to comment.