forked from cpina/github-action-push-to-another-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathentrypoint.sh
executable file
·79 lines (64 loc) · 2.39 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
#!/bin/sh -l
# Combination of:
# - https://github.com/cpina/github-action-push-to-another-repository
# - https://github.com/dmnemec/copy_file_to_another_repo_action
set -e
set -x
echo "Start"
if [ -z "$INPUT_AUTHOR" ]
then
INPUT_AUTHOR="$GITHUB_ACTOR"
fi
if [ -z "$INPUT_AUTHOR_EMAIL" ]
then
INPUT_AUTHOR_EMAIL="$INPUT_AUTHOR@users.noreply.github.com"
fi
if [ -z "$INPUT_TARGET_BRANCH" ]
then
INPUT_TARGET_BRANCH="main"
fi
TARGET_BRANCH_EXISTS=true
CLONE_DIR=$(mktemp -d)
echo "Clean up old references maybe"
git remote prune origin
echo "Cloning destination git repository"
# Setup git
git config --global user.email "$INPUT_AUTHOR_EMAIL"
git config --global user.name "$INPUT_AUTHOR"
# Clone branch matching the target branch name or default branch (master, main, etc)
{ # try
git clone --single-branch --branch "$INPUT_TARGET_BRANCH" "https://$INPUT_TOKEN@github.com/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"
} || { # on no such remote branch, pull default branch instead
echo "The input target branch does not already exist on the target repository. It will be created."
git clone --single-branch "https://$INPUT_TOKEN@github.com/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"
TARGET_BRANCH_EXISTS=false
}
ls -la "$CLONE_DIR"
echo "Copying files to git repo. Invisible files must be handled differently than visible files."
# Include dot files for source filepath
mkdir -p $CLONE_DIR/$INPUT_DESTINATION_FOLDER
if test -f "$INPUT_SOURCE_FILE_PATH"/*; then
cp -r "$INPUT_SOURCE_FILE_PATH"/* "$CLONE_DIR/$INPUT_DESTINATION_FOLDER"
else
echo "WARNING: No visible files exist"
fi
invisible_exists=false
if test -f "$INPUT_SOURCE_FILE_PATH"/.??*; then
cp -r "$INPUT_SOURCE_FILE_PATH"/.??* "$CLONE_DIR/$INPUT_DESTINATION_FOLDER"
else
echo "WARNING: No invisible/hidden (dot) files exist"
fi
cd "$CLONE_DIR"
ls -la
# Create branch locally if it doesn't already exist locally
if [ "$TARGET_BRANCH_EXISTS" = false ] ; then
git checkout -b "$INPUT_TARGET_BRANCH"
fi
echo "Adding git commit"
git add .
git status
# git diff-index to avoid an error when there are no changes to commit
git diff-index --quiet HEAD || git commit --message "Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
echo "Pushing git commit. Create branch if none exists."
# --set-upstream also creates the branch if it doesn't already exist in the destination repository
git push origin --set-upstream "$INPUT_TARGET_BRANCH"