forked from dmnemec/copy_file_to_another_repo_action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·83 lines (70 loc) · 2.24 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
#!/bin/sh
set -e
set -x
if [ -z "$INPUT_SOURCE_FILE" ]; then
echo "Source file must be defined"
return 1
fi
if [ -z "$INPUT_GIT_SERVER" ]; then
INPUT_GIT_SERVER="github.com"
fi
if [ -z "$INPUT_DESTINATION_BRANCH" ]; then
INPUT_DESTINATION_BRANCH=main
fi
OUTPUT_BRANCH="$INPUT_DESTINATION_BRANCH"
CLONE_DIR=$(mktemp -d)
echo "Cloning destination git repository"
git config --global user.email "$INPUT_USER_EMAIL"
git config --global user.name "$INPUT_USER_NAME"
git clone --single-branch --branch $INPUT_DESTINATION_BRANCH "https://x-access-token:$API_TOKEN_GITHUB@$INPUT_GIT_SERVER/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"
if [ ! -z "$INPUT_RENAME" ]; then
echo "Setting new filename: ${INPUT_RENAME}"
DEST_COPY="$CLONE_DIR/$INPUT_DESTINATION_FOLDER/$INPUT_RENAME"
else
DEST_COPY="$CLONE_DIR/$INPUT_DESTINATION_FOLDER"
fi
echo "Copying contents to git repo"
mkdir -p $CLONE_DIR/$INPUT_DESTINATION_FOLDER
if [ -z "$INPUT_USE_RSYNC" ]; then
cp -R "$INPUT_SOURCE_FILE" "$DEST_COPY"
else
echo "rsync mode detected"
rsync -avrh "$INPUT_SOURCE_FILE" "$DEST_COPY"
fi
cd "$CLONE_DIR"
if [ ! -z "$INPUT_DESTINATION_BRANCH_CREATE" ]; then
echo "Creating new branch: ${INPUT_DESTINATION_BRANCH_CREATE}"
git checkout -b "$INPUT_DESTINATION_BRANCH_CREATE"
OUTPUT_BRANCH="$INPUT_DESTINATION_BRANCH_CREATE"
fi
if [ -z "$INPUT_COMMIT_MESSAGE" ]; then
INPUT_COMMIT_MESSAGE="Update from https://$INPUT_GIT_SERVER/${GITHUB_REPOSITORY}/commit/${GITHUB_SHA}"
fi
push_changes() {
echo "Pulling"
# Not wrapping in quotes to allow for multiple flags
# shellcheck disable=SC2086
git pull origin "$OUTPUT_BRANCH" $INPUT_PULL_FLAGS || return 1
echo "Pushing git commit"
git push -u origin HEAD:"$OUTPUT_BRANCH" || return 1
}
echo "Adding git commit"
git add .
if git status | grep -q "Changes to be committed"; then
git commit --message "$INPUT_COMMIT_MESSAGE"
echo Retries: "$INPUT_RETRY_COUNT"
sleep_duration_s=5
retries=0
until [ $retries -ge "$INPUT_RETRY_COUNT" ]; do
push_changes && break
retries=$((retries + 1))
if [ $retries -ge "$INPUT_RETRY_COUNT" ]; then
echo "Exceeded max retries"
exit 1
fi
echo "Retrying in $sleep_duration_s seconds..."
sleep "$sleep_duration_s"
done
else
echo "No changes detected"
fi