forked from newrelic/wiki-sync-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·129 lines (109 loc) · 4.12 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
#!/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="chore(docs): Sync $SOURCE to $DESTINATION [skip-cd]"
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://${GIT_AUTHOR_NAME}:${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 remote add origin $GIT_REPOSITORY_URL
git fetch
git checkout master
)
###############################################################################
## 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 --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/' --exclude='.git*' "$SOURCE/" "$tmp_dir"
debug "Committing and pushing changes"
(
cd "$tmp_dir" || exit 1
git add .
git commit -m "$WIKI_COMMIT_MESSAGE"
git push --set-upstream "$GIT_REPOSITORY_URL" master
)
else # wiki -> $DESTINATION
rsync -avzr --delete --exclude='.git/' --exclude='.git*' "$tmp_dir/" "$DESTINATION"
debug "Committing and pushing changes"
(
git config user.name "$GIT_AUTHOR_NAME"
git config user.email "$GIT_AUTHOR_EMAIL"
# 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 add .
git commit -m "$WIKI_COMMIT_MESSAGE"
git push origin $BRANCH
)
fi
else
warning "No file diff between $SOURCE and $DESTINATION. Exiting."
fi
rm -rf "$tmp_dir"
debug "Finished - $SOURCE synced to $DESTINATION"
exit 0