forked from drud/action-cross-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·74 lines (63 loc) · 1.6 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
#!/usr/bin/env bash
set -euo pipefail
# Ensure all variables are present
SOURCE="$1"
REPO="$2"
TARGET="$3"
BRANCH="$4"
GIT_USER="$5"
GIT_EMAIL="$6"
GIT_COMMIT_MSG="$7"
EXCLUDES=()
if [[ ! -z ${8+x} ]]; then
X=(${8//:/ })
for x in "${X[@]}"; do
EXCLUDES+=('--exclude')
EXCLUDES+=("/$x")
done
fi
# Create Temporary Directory
TEMP=$(mktemp -d)
# Setup git
git config --global user.email $GIT_EMAIL
git config --global user.name $GIT_USER
git clone $REPO $TEMP
cd $TEMP
# Check if branch exists
LS_REMOTE="$(git ls-remote --heads origin refs/heads/$BRANCH)"
if [[ -n "$LS_REMOTE" ]]; then
echo "Checking out $BRANCH from origin."
git checkout $BRANCH
else
echo "$BRANCH does not exist on origin, creating new branch."
git checkout -b $BRANCH
fi
# Sync $TARGET folder to $REPO state repository with excludes
f="/"
if [[ -f "${GITHUB_WORKSPACE}/${SOURCE}" ]]; then
f=""
fi
echo "running 'rsync -avh --delete "${EXCLUDES[@]}" $GITHUB_WORKSPACE/${SOURCE}${f} $TEMP/$TARGET'"
rsync -avh --delete "${EXCLUDES[@]}" $GITHUB_WORKSPACE/${SOURCE}${f} $TEMP/$TARGET
# Success finish early if there are no changes
# i.e. up to date and branch exists
if [ -z "$(git status --porcelain)" ] && [ -n "$LS_REMOTE" ]; then
echo "no changes to sync"
exit 0
fi
# Add changes and push commit
git add .
if [[ -n "$GIT_COMMIT_MSG" ]]; then
git commit -m "$GIT_COMMIT_MSG"
else
SHORT_SHA=$(echo $GITHUB_SHA | head -c 6)
git commit -F- <<EOF
Automatic CI SYNC Commit $SHORT_SHA
Syncing with $GITHUB_REPOSITORY commit $GITHUB_SHA
EOF
fi
if [[ -n "$LS_REMOTE" ]]; then
git push
else
git push origin $BRANCH
fi