/
entrypoint.sh
executable file
·194 lines (157 loc) · 5.64 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env bash
set -e
export WORKING_DIR=${PWD}
chown -R $(whoami) ${WORKING_DIR}
chmod -R +x /maps/Journal/Scripts
export hr=$(printf '=%.0s' {1..80})
# Initial default value
export TOKEN=${INPUT_TOKEN}
export ACTOR=${GITHUB_ACTOR}
export REPOSITORY=${INPUT_REPOSITORY}
export OWNER=${GITHUB_REPOSITORY_OWNER}
export BRANCH=${INPUT_BRANCH:=gh-pages}
export PROVIDER=${INPUT_PROVIDER:=github}
export BUNDLER_VER=${INPUT_BUNDLER_VER:=>=0}
export JEKYLL_BASEURL=${INPUT_JEKYLL_BASEURL:=}
export PRE_BUILD_COMMANDS=${INPUT_PRE_BUILD_COMMANDS:=}
# https://stackoverflow.com/a/42137273/4058484
export JEKYLL_SRC=${WORKING_DIR}
export JEKYLL_GITHUB_TOKEN=${TOKEN}
if [[ "${OWNER}" == "chetabahana" ]]; then
export JEKYLL_SRC=${WORKING_DIR}/docs
fi
export JEKYLL_CFG=${JEKYLL_SRC}/_config.yml
sed -i -e "s/eq19/${OWNER}/g" ${JEKYLL_CFG}
if [[ -z "${TOKEN}" ]]; then
echo -e "Please set the TOKEN environment variable."
exit 1
fi
# Pre-handle Jekyll baseurl
if [[ -n "${JEKYLL_BASEURL-}" ]]; then
JEKYLL_BASEURL="--baseurl ${JEKYLL_BASEURL}"
fi
# Initialize environment
export RUBYOPT=W0
export RUBY_VERSION=2.7.0
export RAILS_VERSION=5.0.1
export BUNDLER_VER=${BUNDLER_VER}
export BUNDLE_GEMFILE=/maps/Gemfile
export BUNDLE_SILENCE_ROOT_WARNING=1
export NOKOGIRI_USE_SYSTEM_LIBRARIES=1
export PAGES_REPO_NWO=$GITHUB_REPOSITORY
export VENDOR_BUNDLE=${WORKING_DIR}/vendor/bundle
export SSL_CERT_FILE=$(realpath .github/hook-scripts/cacert.pem)
# identity
echo -e "\n$hr\nWHOAMI\n$hr"
whoami
pwd
id
# os version
echo -e "\n$hr\nOS VERSION\n$hr"
cat /etc/os-release
uname -r
# file system
echo -e "\n$hr\nFILE SYSTEM\n$hr"
df -h
# rootdir
echo -e "$hr\nROOT DIR\n$hr"
cd / && pwd && ls -al
# pckages
echo -e "$hr\nVENV DIR\n$hr"
cd /maps && pwd && ls -al
# current
echo -e "$hr\nWORK DIR\n$hr"
cd ${WORKING_DIR} && pwd && ls -al
echo -e "$hr\nPRIOR INSTALLATION\n$hr"
chown -R root:root ${HOME} && dpkg -l
# cloning default repository
# https://stackoverflow.com/a/74439875/4058484
git config --global user.name "${ACTOR}"
git config --global user.email "${ACTOR}@users.noreply.github.com"
git config --global credential.helper store &>/dev/null
echo "https://{ACTOR}:${TOKEN}@github.com" > /root/.git-credentials
git clone --quiet --recurse-submodules -j8 ${REPOSITORY} /maps/feed/default
export NPM_CACHE_DIR=${VENDOR_BUNDLE}/npm
apt-get install -qq npm &>/dev/null && apt-get install -qq yarn &>/dev/null
npm install --prefix /maps --cache ${NPM_CACHE_DIR} &>/dev/null
export PATH=${HOME}/.local/bin:$PATH
export PIP_CACHE_DIR=${VENDOR_BUNDLE}/pip
# https://pypi.org/project/pipx/
python -m pip install --upgrade pip setuptools six wheel &>/dev/null
python -m pip install pytest-cov -r /maps/requirements.txt &>/dev/null
python -m pip install jax[cuda11_cudnn82] \
-f https://storage.googleapis.com/jax-releases/jax_releases.html &>/dev/null
export GEM_PATH=${VENDOR_BUNDLE}/gem
export GEM_HOME=${GEM_PATH}/ruby/${RUBY_VERSION}
export PATH=$PATH:${GEM_HOME}/bin
# https://stackoverflow.com/a/30369485/4058484
apt-get install -qq ruby ruby-dev ruby-bundler build-essential &>/dev/null
gem install rails --version "$RAILS_VERSION" --quiet --silent &>/dev/null
# installed packages
echo -e "\n$hr\nUPON INSTALLATION\n$hr"
dpkg -l
# Setting default ruby version
echo -e "$hr\nTENSORFLOW VERSION\n$hr"
pip show tensorflow-gpu && pip -V
# https://stackoverflow.com/a/60945404/4058484
ruby -v && bundler version && python -V
node -v && npm -v
# Restore modification time (mtime) of git files
echo -e "$hr\nEPOCH TEST\n$hr"
/maps/Journal/Scripts/restore.sh
/maps/Journal/Scripts/prime_list.sh
/maps/Journal/Scripts/init_environment.sh
# Clean up bundler cache
CLEANUP_BUNDLER_CACHE_DONE=false
bundle config path ${GEM_PATH}
bundle config cache_all true
cleanup_bundler_cache() {
/maps/Journal/Scripts/cleanup_bundler.sh
rm -rf ${GEM_HOME} && mkdir -p ${GEM_HOME}
gem install bundler -v "${BUNDLER_VER}" &>/dev/null
echo -e "\nCLEANUP BUNDLE\n$hr" && bundle install
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 cache firstly.
OS_NAME_FILE=${GEM_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 -e $os_name > $OS_NAME_FILE
fi
# Check and execute pre_build_commands commands
cd ${JEKYLL_SRC}
if [[ ${PRE_BUILD_COMMANDS} ]]; then
eval "${PRE_BUILD_COMMANDS}"
fi
build_jekyll() {
echo -e "\n$hr\nJEKYLL BUILD\n$hr" && pwd
# https://gist.github.com/DrOctogon/bfb6e392aa5654c63d12
JEKYLL_GITHUB_TOKEN=${TOKEN} bundle exec jekyll build --trace --profile \
${JEKYLL_BASEURL} -c ${JEKYLL_CFG} -d ${WORKING_DIR}/build
# vendor/bundle
echo -e "\n$hr\nVENDOR BUNDLE\n$hr" && chown -R root:root ${HOME}
cd ${HOME} && mv -f .gem gem && mv -f gem/* ${VENDOR_BUNDLE}/gem/
echo ${VENDOR_BUNDLE} && ls -al ${VENDOR_BUNDLE} && echo -e "\n"
echo ${NPM_CACHE_DIR} && ls -al ${NPM_CACHE_DIR} && echo -e "\n"
echo ${PIP_CACHE_DIR} && ls -al ${PIP_CACHE_DIR} && echo -e "\n"
echo ${GEM_PATH} && ls -al ${GEM_PATH} && echo -e "\n"
echo ${GEM_HOME} && ls -al ${GEM_HOME} && echo -e "\n"
chmod -R a+rwx,o-w ${PIP_CACHE_DIR}
echo -e "Source cleaning..\n"
rm -rf gem && ls -al
}
build_jekyll || {
$CLEANUP_BUNDLER_CACHE_DONE && exit -1
echo -e "\nRebuild all gems and try to build again\n"
cleanup_bundler_cache
build_jekyll
}
cd ${WORKING_DIR}
echo -e "$hr\nDEPLOYMENT\n$hr"
# https://unix.stackexchange.com/a/83895/158462
git submodule foreach -q /maps/Journal/Scripts/github_pages.sh
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
exit $?