This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsync-upstream.sh
87 lines (63 loc) · 2.3 KB
/
sync-upstream.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
#!/bin/sh -l
set -eou pipefail
echo "[+] Action start"
if [ -n "${PAT:=}" ]
then
GIT_CMD_REPOSITORY="https://planetscale:[email protected]/$DESTINATION_REPOSITORY.git"
else
echo "::error::PAT is empty"
exit 1
fi
CLONE_DIR=$(mktemp -d)
trap 'rm -rf -- "$CLONE_DIR"' EXIT
echo "[+] Git version"
git --version
echo "[+] Cloning destination git repository"
git config --global user.email "[email protected]"
git config --global user.name "PlanetScale Actions Bot"
{
git clone --single-branch --depth 1 --branch "$DESTINATION_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR"
} || {
echo "::error::Could not clone the destination repository. Command:"
echo "::error::git clone --single-branch --branch $DESTINATION_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR"
exit 1
}
ls -la "$CLONE_DIR"
TEMP_DIR=$(mktemp -d)
trap 'rm -rf -- "$TEMP_DIR"' EXIT
mv "$CLONE_DIR/.git" "$TEMP_DIR/.git"
ABSOLUTE_DESTINATION_DIRECTORY="$CLONE_DIR/$DESTINATION_DIRECTORY/"
echo "[+] Clearing $ABSOLUTE_DESTINATION_DIRECTORY"
rm -rf "$ABSOLUTE_DESTINATION_DIRECTORY"
mkdir -p "$ABSOLUTE_DESTINATION_DIRECTORY"
echo "[+] Listing $PWD"
ls -al
echo "[+] Listing /"
ls -al /
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
echo "[+] Listing $SOURCE_DIRECTORY"
ls "$SOURCE_DIRECTORY"
echo "[+] Checking if local $SOURCE_DIRECTORY exist"
if [ ! -d "$SOURCE_DIRECTORY" ]
then
echo "ERROR: $SOURCE_DIRECTORY does not exist"
exit 1
fi
echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $DESTINATION_DIRECTORY in git repo docs"
cp -a "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$DESTINATION_DIRECTORY"
cd "$CLONE_DIR"
echo "[+] Files that will be pushed"
ls -la
echo "[+] Set $CLONE_DIR as safe directory"
# https://github.com/cpina/github-action-push-to-another-repository/issues/64
git config --global --add safe.directory "$CLONE_DIR"
echo "[+] Adding git files"
git add .
echo "[+] git status:"
git status
echo "[+] git diff-index:"
# git diff-index : to avoid doing the git commit failing if there are no changes to be commit
git diff-index --quiet HEAD || git commit --message 'docs: downstream' --message "https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
echo "[+] Pushing git commit"
# --set-upstream: sets the branch when pushing to a branch that does not exist
git push "$GIT_CMD_REPOSITORY" --set-upstream "$DESTINATION_BRANCH"