1+ #! /usr/bin/env bash
2+
3+ set -euo pipefail
4+
5+ remote_repository=" ${DASHBOARD_REMOTE_REPOSITORY:? } "
6+ publish_branch=" ${DASHBOARD_PUBLISH_BRANCH:- master} "
7+ source_dir=" ${DASHBOARD_SOURCE_DIR:? } "
8+ token=" ${DASHBOARD_TOKEN:? } "
9+ git_user_name=" ${DASHBOARD_GIT_USER_NAME:- easyscience[bot]} "
10+ git_user_email=" ${DASHBOARD_GIT_USER_EMAIL:? } "
11+ commit_message=" ${DASHBOARD_COMMIT_MESSAGE:? } "
12+ max_attempts=" ${DASHBOARD_PUSH_ATTEMPTS:- 3} "
13+ delay_seconds=" ${DASHBOARD_PUSH_DELAY_SECONDS:- 15} "
14+
15+ workspace_dir=" $( mktemp -d) "
16+ repo_dir=" ${workspace_dir} /dashboard"
17+ remote_url=" https://x-access-token:${token} @github.com/${remote_repository} .git"
18+
19+ cleanup () {
20+ rm -rf " ${workspace_dir} "
21+ }
22+
23+ prepare_worktree () {
24+ if [[ ! -d " ${repo_dir} /.git" ]]; then
25+ git clone --branch " ${publish_branch} " --depth 1 " ${remote_url} " " ${repo_dir} "
26+ else
27+ git -C " ${repo_dir} " fetch origin " ${publish_branch} "
28+ git -C " ${repo_dir} " checkout " ${publish_branch} "
29+ git -C " ${repo_dir} " reset --hard " origin/${publish_branch} "
30+ git -C " ${repo_dir} " clean -fd
31+ fi
32+
33+ git -C " ${repo_dir} " config user.name " ${git_user_name} "
34+ git -C " ${repo_dir} " config user.email " ${git_user_email} "
35+ }
36+
37+ sync_publish_dir () {
38+ cp -R " ${source_dir} /." " ${repo_dir} /"
39+ git -C " ${repo_dir} " add .
40+
41+ if git -C " ${repo_dir} " diff --cached --quiet; then
42+ return 1
43+ fi
44+
45+ git -C " ${repo_dir} " commit -m " ${commit_message} "
46+ }
47+
48+ trap cleanup EXIT
49+
50+ prepare_worktree
51+
52+ if ! sync_publish_dir; then
53+ echo " No dashboard changes to publish."
54+ exit 0
55+ fi
56+
57+ for (( attempt = 1 ; attempt <= max_attempts; attempt += 1 )) ; do
58+ if git -C " ${repo_dir} " push origin " HEAD:${publish_branch} " ; then
59+ echo " Dashboard published on attempt ${attempt} ."
60+ exit 0
61+ fi
62+
63+ if (( attempt == max_attempts)) ; then
64+ echo " Dashboard publish failed after ${max_attempts} attempts." >&2
65+ exit 1
66+ fi
67+
68+ echo " Dashboard push attempt ${attempt} failed. Retrying in ${delay_seconds} s." >&2
69+ sleep " ${delay_seconds} "
70+
71+ prepare_worktree
72+
73+ if ! sync_publish_dir; then
74+ echo " Dashboard changes already exist in the target repository."
75+ exit 0
76+ fi
77+ done
0 commit comments