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

113 lines
4.0 KiB
Bash
Raw 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
2021-08-20 18:54:32 -06:00
echo "[+] Action start"
2021-08-29 06:24:21 -06:00
SOURCE_BEFORE_DIRECTORY="$1"
SOURCE_DIRECTORY="$2"
DESTINATION_GITHUB_USERNAME="$3"
DESTINATION_REPOSITORY_NAME="$4"
2021-10-25 16:41:08 -06:00
GITHUB_SERVER="$5"
USER_EMAIL="$6"
USER_NAME="$7"
DESTINATION_REPOSITORY_USERNAME="$8"
TARGET_BRANCH="$9"
COMMIT_MESSAGE="${10}"
TARGET_DIRECTORY="${11}"
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
2020-08-10 07:31:08 -06:00
then
2021-10-25 16:03:05 -06:00
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
fi
2020-03-29 09:07:19 -06:00
if [ -z "$USER_NAME" ]
then
2021-10-25 16:03:05 -06:00
USER_NAME="$DESTINATION_GITHUB_USERNAME"
fi
CLONE_DIR=$(mktemp -d)
2020-03-29 09:13:00 -06:00
2021-08-20 17:23:03 -06:00
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
# Setup git
2020-06-08 15:49:19 -06:00
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"
2021-12-14 17:09:49 -07:00
{
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
} || {
echo "::error::Could not clone the destination repository. Command:"
2021-12-14 16:58:36 -07:00
echo "::error::git clone --single-branch --branch $TARGET_BRANCH https://$USER_NAME:the_api_token@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git $CLONE_DIR"
2021-12-14 17:13:36 -07:00
echo "::error::(Note that the USER_NAME and API_TOKEN is redacted by GitHub)"
echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB"
exit 1
2021-12-14 17:09:49 -07:00
}
ls -la "$CLONE_DIR"
TEMP_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" "$TEMP_DIR/.git"
# $TARGET_DIRECTORY is '' by default
ABSOLUTE_TARGET_DIRECTORY="$CLONE_DIR/$TARGET_DIRECTORY/"
echo "[+] Deleting $ABSOLUTE_TARGET_DIRECTORY"
rm -rf "$ABSOLUTE_TARGET_DIRECTORY"
echo "[+] Creating (now empty) $ABSOLUTE_TARGET_DIRECTORY"
mkdir -p "$ABSOLUTE_TARGET_DIRECTORY"
2021-08-29 06:24:21 -06:00
echo "[+] Listing Current Directory Location"
ls -al
echo "[+] Listing root Location"
ls -al /
2021-08-29 07:26:38 -06:00
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
2021-08-31 04:04:57 -06:00
echo "[+] List contents of $SOURCE_DIRECTORY"
2021-08-29 16:15:51 -06:00
ls "$SOURCE_DIRECTORY"
2021-08-29 07:26:38 -06:00
2021-08-31 04:04:57 -06:00
echo "[+] Checking if local $SOURCE_DIRECTORY exist"
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
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"
echo "actions/checkout@v2. See: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L16"
exit 1
fi
2021-08-20 18:54:32 -06:00
echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME"
cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY"
cd "$CLONE_DIR"
2021-08-20 17:23:03 -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
2021-06-24 00:59:20 -06:00
ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
2020-11-04 09:23:02 -07:00
2021-08-20 17:23:03 -06:00
echo "[+] Adding git commit"
2020-03-29 09:07:19 -06:00
git add .
2021-08-20 17:23:03 -06:00
echo "[+] git status:"
2020-08-10 08:02:28 -06:00
git status
2021-08-20 17:23:03 -06:00
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"
2020-08-10 08:02:28 -06:00
2021-08-20 17:23:03 -06:00
echo "[+] Pushing git commit"
2020-11-10 02:01:07 -07:00
# --set-upstream: sets de branch when pushing to a branch that does not exist
2021-06-24 00:59:20 -06:00
git push "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"