2020-03-29 08:44:47 -06:00
|
|
|
#!/bin/sh -l
|
|
|
|
|
2020-11-04 08:02:44 -07:00
|
|
|
set -e # if a command fails it stops the execution
|
|
|
|
set -u # script fails if trying to access to an undefined variable
|
|
|
|
|
2020-03-29 08:44:47 -06:00
|
|
|
echo "Starts"
|
2020-11-04 08:27:20 -07:00
|
|
|
SOURCE_DIRECTORY="$1"
|
|
|
|
DESTINATION_GITHUB_USERNAME="$2"
|
|
|
|
DESTINATION_REPOSITORY_NAME="$3"
|
2020-06-08 15:49:19 -06:00
|
|
|
USER_EMAIL="$4"
|
2020-11-04 08:27:20 -07:00
|
|
|
DESTINATION_REPOSITORY_USERNAME="$5"
|
2020-09-23 01:49:12 -06:00
|
|
|
TARGET_BRANCH="$6"
|
2020-11-04 09:23:02 -07:00
|
|
|
COMMIT_MESSAGE="$7"
|
2020-08-07 14:49:54 -06:00
|
|
|
|
2020-11-04 08:27:20 -07:00
|
|
|
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
|
2020-08-10 07:31:08 -06:00
|
|
|
then
|
2020-11-04 08:27:20 -07:00
|
|
|
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
|
2020-08-07 14:49:54 -06:00
|
|
|
fi
|
2020-03-29 09:07:19 -06:00
|
|
|
|
2020-03-29 09:36:01 -06:00
|
|
|
CLONE_DIR=$(mktemp -d)
|
2020-03-29 09:13:00 -06:00
|
|
|
|
2020-08-10 08:02:28 -06:00
|
|
|
echo "Cloning destination git repository"
|
2020-03-29 09:20:35 -06:00
|
|
|
# Setup git
|
2020-06-08 15:49:19 -06:00
|
|
|
git config --global user.email "$USER_EMAIL"
|
2020-11-04 08:27:20 -07:00
|
|
|
git config --global user.name "$DESTINATION_GITHUB_USERNAME"
|
|
|
|
git clone --single-branch --branch "$TARGET_BRANCH" "https://$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
|
2020-03-29 09:30:55 -06:00
|
|
|
ls -la "$CLONE_DIR"
|
|
|
|
|
2020-12-02 13:04:04 -07:00
|
|
|
TARGET_DIR=$(mktemp -d)
|
2021-04-25 03:55:07 -06:00
|
|
|
# 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/"
|
2020-12-02 13:04:04 -07:00
|
|
|
mv "$CLONE_DIR/.git" "$TARGET_DIR"
|
2020-03-29 09:30:55 -06:00
|
|
|
|
2021-04-25 03:55:07 -06:00
|
|
|
if [ ! -d "$SOURCE_DIRECTORY" ]
|
|
|
|
then
|
2021-04-25 04:26:51 -06:00
|
|
|
echo "ERROR: $SOURCE_DIRECTORY does not exist"
|
2021-04-25 03:57:58 -06:00
|
|
|
echo "This directory needs to exist when push-to-another-repository is executed"
|
|
|
|
echo
|
2021-04-25 04:09:13 -06:00
|
|
|
echo "In the example it is created by ./build.sh: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19"
|
2021-04-25 03:57:58 -06:00
|
|
|
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. For example using"
|
2021-04-25 04:09:13 -06:00
|
|
|
echo "actions/checkout@v2. See: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L16"
|
2021-04-25 03:55:07 -06:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Copy contents to target git repository"
|
2020-12-02 13:04:04 -07:00
|
|
|
cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR"
|
|
|
|
cd "$TARGET_DIR"
|
2020-12-02 09:07:47 -07:00
|
|
|
|
2021-04-25 03:55:07 -06:00
|
|
|
echo "Files that will be pushed:"
|
2020-08-10 08:02:28 -06:00
|
|
|
ls -la
|
2020-03-29 09:07:19 -06:00
|
|
|
|
2020-11-04 09:23:02 -07:00
|
|
|
ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
|
2020-11-04 09:45:08 -07:00
|
|
|
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
|
2020-11-04 09:23:02 -07:00
|
|
|
|
2021-04-25 03:55:07 -06:00
|
|
|
echo "git add:"
|
2020-03-29 09:07:19 -06:00
|
|
|
git add .
|
2021-04-25 03:55:07 -06:00
|
|
|
|
|
|
|
echo "git status:"
|
2020-08-10 08:02:28 -06:00
|
|
|
git status
|
2020-11-05 01:28:13 -07:00
|
|
|
|
2021-04-25 03:55:07 -06:00
|
|
|
echo "git diff-index:"
|
2020-11-05 01:28:13 -07:00
|
|
|
# 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"
|
2020-08-10 08:02:28 -06:00
|
|
|
|
2021-04-25 03:55:07 -06:00
|
|
|
echo "git push origin:"
|
2020-11-10 02:01:07 -07:00
|
|
|
# --set-upstream: sets de branch when pushing to a branch that does not exist
|
2020-11-10 01:59:28 -07:00
|
|
|
git push origin --set-upstream "$TARGET_BRANCH"
|