-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
entrypoint.sh
executable file
·160 lines (132 loc) · 4.24 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
set -e
# Get script directory
SCRIPT_DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
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}
RUBY_VER=${INPUT_RUBY_VER:=3.2.0}
BUNDLER_VER=${INPUT_BUNDLER_VER:=~>2.5.0}
JEKYLL_SRC=${INPUT_JEKYLL_SRC:=./}
JEKYLL_CFG=${INPUT_JEKYLL_CFG:=./_config.yml}
JEKYLL_BASEURL=${INPUT_JEKYLL_BASEURL:=}
PRE_BUILD_COMMANDS=${INPUT_PRE_BUILD_COMMANDS:=}
# Set default bundle path and cache
BUNDLE_PATH=${WORKING_DIR}/vendor/bundle
echo "Starting the Jekyll Deploy Action"
if [[ "${PROVIDER}" != "test" ]]; then
# Check if the token or SSH private key is set
if [[ -z "${TOKEN}" && -z "${SSH_PRIVATE_KEY}" ]]; then
echo "Please set the TOKEN or SSH_PRIVATE_KEY environment variable."
exit 1
fi
fi
# Check parameters and assign default values
if [[ "${PROVIDER}" == "github" ]]; then
: ${ACTOR:=${GITHUB_ACTOR}}
: ${REPOSITORY:=${GITHUB_REPOSITORY}}
: ${BRANCH:=gh-pages}
# Check if repository is available
if ! echo "${REPOSITORY}" | grep -Eq ".+/.+"; then
echo "The repository ${REPOSITORY} doesn't match the pattern <author>/<repos>"
exit 1
fi
# Fix Github API metadata warnings
export JEKYLL_GITHUB_TOKEN=${TOKEN}
fi
# Initialize environment
echo "Initialize environment"
source ${SCRIPT_DIR}/script/init_environment.sh
cd ${JEKYLL_SRC}
# Check and execute pre_build_commands commands
if [[ ${PRE_BUILD_COMMANDS} ]]; then
echo "Executing pre-build commands"
eval "${PRE_BUILD_COMMANDS}"
fi
echo "Check bundler version from Gemfile.lock"
GEMFILE_LOCK_DIR="${PWD}"
while [[ "${GEMFILE_LOCK_DIR}" != "/" ]] &&
[[ ! -f "${GEMFILE_LOCK_DIR}/Gemfile.lock" ]]; do
GEMFILE_LOCK_DIR="$(dirname "${GEMFILE_LOCK_DIR}")"
done
if [[ -f "${GEMFILE_LOCK_DIR}/Gemfile.lock" ]]; then
BUNDLER_VER="$( \
grep -A 1 'BUNDLED WITH' "${GEMFILE_LOCK_DIR}/Gemfile.lock" | \
tail -n 1 | xargs)"
echo "Bundler version ${BUNDLER_VER} is required by your Gemfile.lock!"
fi
echo "Initial comptible bundler"
${SCRIPT_DIR}/script/cleanup_bundler.sh
gem install bundler -v "${BUNDLER_VER}"
CLEANUP_BUNDLER_CACHE_DONE=false
# Clean up bundler cache
cleanup_bundler_cache() {
echo "Cleaning up incompatible bundler cache"
rm -rf ${BUNDLE_PATH}
mkdir -p ${BUNDLE_PATH}
CLEANUP_BUNDLER_CACHE_DONE=true
}
# If the vendor/bundle folder is cached in a differnt OS (e.g. Ubuntu),
# it would cause `jekyll build` failed, we should clean up the uncompatible
# cache firstly.
OS_NAME_FILE=${BUNDLE_PATH}/os-name
os_name=$(cat /etc/os-release | grep '^NAME=')
os_name=${os_name:6:-1}
if [[ "$os_name" != "$(cat $OS_NAME_FILE 2>/dev/null)" ]]; then
cleanup_bundler_cache
echo $os_name > $OS_NAME_FILE
fi
echo "Starting bundle install"
bundle config cach_all true
bundle config path $BUNDLE_PATH
bundle install
# Pre-handle Jekyll baseurl
if [[ -n "${JEKYLL_BASEURL-}" ]]; then
JEKYLL_BASEURL="--baseurl ${JEKYLL_BASEURL}"
fi
build_jekyll() {
echo "Starting jekyll build"
JEKYLL_ENV=production bundle exec jekyll build \
${JEKYLL_BASEURL} \
-c ${JEKYLL_CFG} \
-d ${WORKING_DIR}/build
}
build_jekyll || {
$CLEANUP_BUNDLER_CACHE_DONE && exit -1
echo "Rebuild all gems and try to build again"
cleanup_bundler_cache
bundle install
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 [[ -f "${SCRIPT_DIR}/providers/${PROVIDER}.sh" ]]; then
source "${SCRIPT_DIR}/providers/${PROVIDER}.sh"
else
echo "${PROVIDER} is an unsupported provider."
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
# Update cache key
echo "Update cache key..."
source ${SCRIPT_DIR}/script/update_cache_key.sh
exit ${PROVIDER_EXIT_CODE}