github-action-push-to-anoth.../entrypoint.sh

50 lines
1.4 KiB
Bash
Raw Permalink Normal View History

2020-03-29 08:44:47 -06:00
#!/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
2020-03-29 08:44:47 -06:00
echo "Starts"
SOURCE_DIRECTORY="$1"
DESTINATION_GITHUB_USERNAME="$2"
DESTINATION_REPOSITORY_NAME="$3"
2020-06-08 15:49:19 -06:00
USER_EMAIL="$4"
DESTINATION_REPOSITORY_USERNAME="$5"
TARGET_BRANCH="$6"
2020-11-04 09:23:02 -07:00
COMMIT_MESSAGE="$7"
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
2020-08-10 07:31:08 -06:00
then
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
fi
2020-03-29 09:07:19 -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"
# Setup git
2020-06-08 15:49:19 -06:00
git config --global user.email "$USER_EMAIL"
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"
ls -la "$CLONE_DIR"
2020-08-10 08:02:28 -06:00
echo "Cleaning destination repository of old files"
# Copy files into the git and deletes all git
2020-03-29 09:49:16 -06:00
find "$CLONE_DIR" | grep -v "^$CLONE_DIR/\.git" | grep -v "^$CLONE_DIR$" | xargs rm -rf # delete all files (to handle deletions)
ls -la "$CLONE_DIR"
2020-08-10 08:02:28 -06:00
echo "Copying contents to to git repo"
cp -r "$SOURCE_DIRECTORY"/* "$CLONE_DIR"
2020-03-29 09:07:19 -06:00
cd "$CLONE_DIR"
2020-08-10 08:02:28 -06:00
ls -la
2020-03-29 09:07:19 -06:00
2020-08-10 08:02:28 -06:00
echo "Adding git commit"
2020-11-04 09:23:02 -07:00
ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
2020-11-04 09:23:02 -07:00
2020-03-29 09:07:19 -06:00
git add .
2020-08-10 08:02:28 -06:00
git status
2020-11-04 09:23:02 -07:00
git commit --message "$COMMIT_MESSAGE"
2020-08-10 08:02:28 -06:00
echo "Pushing git commit"
git push origin "$TARGET_BRANCH"