forked from newrelic/wiki-sync-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·133 lines (112 loc) · 4.29 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
#!/bin/bash
function debug() {
echo "::debug file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
}
function warning() {
echo "::warning file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
}
function error() {
echo "::error file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
}
function add_mask() {
echo "::add-mask::$1"
}
###############################################################################
## Check required inputs that don't have defaults
###############################################################################
if [ -z "$SOURCE" ]; then
error "SOURCE environment variable is not set"
exit 1
fi
if [ -z "$DESTINATION" ]; then
error "DESTINATION environment variable is not set"
exit 1
fi
if [ -z "$GITHUB_PERSONAL_ACCESS_TOKEN" ]; then
error "GITHUB_PERSONAL_ACCESS_TOKEN environment variable is not set"
exit 1
fi
if [ "$SOURCE" = "wiki" ]; then
SYNC_DIRECTORY=$DESTINATION
elif [ "$DESTINATION" = "wiki" ]; then
SYNC_DIRECTORY=$SOURCE
else
error "Either SOURCE or DESTINATION must be set to 'wiki'"
exit 1
fi
add_mask "${GITHUB_PERSONAL_ACCESS_TOKEN}"
###############################################################################
## Check optional inputs that don't have defaults
###############################################################################
if [ -z "${GIT_AUTHOR_EMAIL:-}" ]; then
debug "GIT_AUTHOR_EMAIL not set, using default"
GIT_AUTHOR_EMAIL="$GIT_AUTHOR_NAME@users.noreply.github.com"
fi
if [ -z "${WIKI_COMMIT_MESSAGE:-}" ]; then
debug "WIKI_COMMIT_MESSAGE not set, using default"
WIKI_COMMIT_MESSAGE="Sync $SOURCE to $DESTINATION"
fi
###############################################################################
## Set wiki repo URL
###############################################################################
if [ -z "$GITHUB_REPOSITORY" ]; then
error "GITHUB_REPOSITORY environment variable is not set"
exit 1
fi
GIT_REPOSITORY_URL="https://${GITHUB_PERSONAL_ACCESS_TOKEN}@github.com/$GITHUB_REPOSITORY.wiki.git"
###############################################################################
## Check out wiki
###############################################################################
debug "Checking out wiki repository"
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
(
cd "$tmp_dir" || exit 1
git init
git config user.name "$GIT_AUTHOR_NAME"
git config user.email "$GIT_AUTHOR_EMAIL"
git pull "$GIT_REPOSITORY_URL"
)
###############################################################################
## Sync files if there's a diff between source and destination
###############################################################################
debug "Checking diff between $SOURCE and $DESTINATION"
# diff returns -1 if there are differences, which exits
# the workflow. || is used to bypass this exiting prematurely
DIFF=$(diff -qr --exclude=.git $SYNC_DIRECTORY $tmp_dir || true)
if [ "$DIFF" != "" ]; then
debug "Syncing contents of $SOURCE to $DESTINATION"
# Check which direction the sync is occurring
if [ "$DESTINATION" = "wiki" ]; then # $SOURCE -> wiki
rsync -avzr --delete --exclude='.git/' "$SOURCE/" "$tmp_dir"
debug "Committing and pushing changes"
(
cd "$tmp_dir" || exit 1
### Workaround: add github workspace as safe directory
git config --global --add safe.directory "$tmp_dir"
git add .
git commit -m "$WIKI_COMMIT_MESSAGE"
git push --set-upstream "$GIT_REPOSITORY_URL" master
)
else # wiki -> $DESTINATION
rsync -avzr --delete --exclude='.git/' "$tmp_dir/" "$DESTINATION"
debug "Committing and pushing changes"
(
git config --global user.name "$GIT_AUTHOR_NAME"
git config --global user.email "$GIT_AUTHOR_EMAIL"
### Workaround: add github workspace as safe directory
git config --global --add safe.directory "$GITHUB_WORKSPACE"
# develop could have been modified by the time we get here, so pull before pushing
# Maybe don't need if we checkout develop...
# git pull --ff-only origin develop
git checkout -b $BRANCH$GITHUB_RUN_ID
git add .
git commit -m "$WIKI_COMMIT_MESSAGE"
git push origin $BRANCH$GITHUB_RUN_ID
)
fi
else
warning "No file diff between $SOURCE and $DESTINATION. Exiting."
fi
rm -rf "$tmp_dir"
debug "Finished - $SOURCE synced to $DESTINATION"
exit 0