-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
75 lines (60 loc) · 2.35 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
#!/bin/sh -l
set -e # if a command fails it stops the execution
set -u # script fails if trying to access to an undefined variable
echo "Starts"
SOURCE_DIRECTORY="$1"
DESTINATION_GITHUB_USERNAME="$2"
DESTINATION_REPOSITORY_NAME="$3"
USER_EMAIL="$4"
USER_NAME="$5"
DESTINATION_REPOSITORY_USERNAME="$6"
TARGET_BRANCH="$7"
COMMIT_MESSAGE="$8"
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
fi
if [ -z "$USER_NAME" ]
then
USER_NAME="$DESTINATION_GITHUB_USERNAME"
fi
CLONE_DIR=$(mktemp -d)
echo "Cloning destination git repository"
# Setup git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:[email protected]/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
ls -la "$CLONE_DIR"
TARGET_DIR=$(mktemp -d)
# This mv has been the easier way to be able to remove files that were there
# but not anymore. Otherwise we had to remove the files from "$CLONE_DIR",
# including "." and with the exception of ".git/"
mv "$CLONE_DIR/.git" "$TARGET_DIR"
if [ ! -d "$SOURCE_DIRECTORY" ]
then
echo "ERROR: $SOURCE_DIRECTORY does not exist"
echo "This directory needs to exist when push-to-another-repository is executed"
echo
echo "If you want to copy a directory that exist in the source repository"
echo "to the target repository: you need to clone the source repository"
echo "in a previous step in the same build section."
exit 1
fi
echo "Copy contents to target git repository"
cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR"
cd "$TARGET_DIR"
echo "Files that will be pushed:"
ls -la
ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
echo "git add:"
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 "$COMMIT_MESSAGE"
echo "git push origin:"
# --set-upstream: sets de branch when pushing to a branch that does not exist
git push "https://$USER_NAME:[email protected]/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"