-
Notifications
You must be signed in to change notification settings - Fork 178
/
deploy.sh
executable file
·98 lines (83 loc) · 2.87 KB
/
deploy.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
#!/usr/bin/env bash
## deploy.sh: run during a CI build to deploy manuscript outputs to the output and gh-pages branches on GitHub.
# Set options for extra caution & debugging
set -o errexit \
-o nounset \
-o pipefail
# set environment variables for GitHub Actions
REPO_SLUG=${GITHUB_REPOSITORY}
COMMIT=${GITHUB_SHA}
BRANCH=${DEFAULT_BRANCH:-main}
# Add commit hash to the README
OWNER_NAME="$(dirname "$REPO_SLUG")"
REPO_NAME="$(basename "$REPO_SLUG")"
export REPO_SLUG COMMIT OWNER_NAME REPO_NAME
envsubst < webpage/README.md > webpage/README-complete.md
mv webpage/README-complete.md webpage/README.md
# Configure git
git config --global push.default simple
git config --global user.email "$(git log --max-count=1 --format='%ae')"
git config --global user.name "$(git log --max-count=1 --format='%an')"
git checkout "$BRANCH"
# Configure deployment credentials
MANUBOT_DEPLOY_VIA_SSH=true
git remote set-url origin "[email protected]:$REPO_SLUG.git"
if [ -v MANUBOT_SSH_PRIVATE_KEY ] && [ "$MANUBOT_SSH_PRIVATE_KEY" != "" ]; then
echo >&2 "[INFO] Detected MANUBOT_SSH_PRIVATE_KEY. Will deploy via SSH."
elif [ -v MANUBOT_ACCESS_TOKEN ] && [ "$MANUBOT_ACCESS_TOKEN" != "" ]; then
echo >&2 "[INFO] Detected MANUBOT_ACCESS_TOKEN. Will deploy via HTTPS."
MANUBOT_DEPLOY_VIA_SSH=false
git remote set-url origin "https://[email protected]/$REPO_SLUG.git"
else
echo >&2 "[INFO] Missing MANUBOT_SSH_PRIVATE_KEY and MANUBOT_ACCESS_TOKEN. Will deploy via SSH."
fi
if [ $MANUBOT_DEPLOY_VIA_SSH = "true" ]; then
# Decrypt and add SSH key
eval "$(ssh-agent -s)"
(
set +o xtrace # disable xtrace in subshell for private key operations
if [ -v MANUBOT_SSH_PRIVATE_KEY ]; then
base64 --decode <<< "$MANUBOT_SSH_PRIVATE_KEY" | ssh-add -
else
echo >&2 "Deployment will fail since neither of the following environment variables are set: MANUBOT_ACCESS_TOKEN or MANUBOT_SSH_PRIVATE_KEY."
fi
)
fi
# Fetch and create gh-pages and output branches
git remote set-branches --add origin gh-pages output
git fetch origin gh-pages:gh-pages output:output || \
echo >&2 "[INFO] could not fetch gh-pages or output from origin."
# Configure versioned webpage and timestamp
manubot webpage \
--timestamp \
--no-ots-cache \
--checkout=gh-pages \
--version="$COMMIT"
# Commit message
MESSAGE="\
$(git log --max-count=1 --format='%s')
[ci skip]
This build is based on
https://github.com/$REPO_SLUG/commit/$COMMIT.
This commit was created by the following CI build and job:
$CI_BUILD_WEB_URL
$CI_JOB_WEB_URL
"
# Deploy the manubot outputs to output
ghp-import \
--push \
--branch=output \
--message="$MESSAGE" \
output
# Deploy the webpage directory to gh-pages
ghp-import \
--no-jekyll \
--follow-links \
--push \
--branch=gh-pages \
--message="$MESSAGE" \
webpage
if [ $MANUBOT_DEPLOY_VIA_SSH = "true" ]; then
# Workaround https://github.com/travis-ci/travis-ci/issues/8082
ssh-agent -k
fi