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
2023-05-10 03:08:03 -06:00
. ./functions.sh
2021-08-20 18:54:32 -06:00
echo "[+] Action start"
2023-05-10 03:08:03 -06:00
getArgs " $@ "
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
2021-10-25 16:03:05 -06:00
DESTINATION_REPOSITORY_USERNAME = " $DESTINATION_GITHUB_USERNAME "
2020-08-07 14:49:54 -06:00
fi
2020-03-29 09:07:19 -06:00
2021-04-28 23:06:40 -06:00
if [ -z " $USER_NAME " ]
then
2021-10-25 16:03:05 -06:00
USER_NAME = " $DESTINATION_GITHUB_USERNAME "
2021-04-28 23:06:40 -06:00
fi
2022-06-08 16:09:10 -06:00
# Verify that there (potentially) some access to the destination repository
# and set up git (with GIT_CMD variable) and GIT_CMD_REPOSITORY
if [ -n " ${ SSH_DEPLOY_KEY : = } " ]
then
2022-07-14 15:04:39 -06:00
echo "[+] Using SSH_DEPLOY_KEY"
2023-05-10 03:08:03 -06:00
sshDeployKey
2022-06-08 16:09:10 -06:00
elif [ -n " ${ API_TOKEN_GITHUB : = } " ]
then
2022-07-14 15:04:39 -06:00
echo "[+] Using API_TOKEN_GITHUB"
2023-05-10 03:08:03 -06:00
githubToken
2022-06-10 01:45:34 -06:00
else
2022-07-14 15:04:39 -06:00
echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY)"
2022-06-08 16:09:10 -06:00
exit 1
fi
2020-03-29 09:36:01 -06:00
CLONE_DIR = $( mktemp -d)
2020-03-29 09:13:00 -06:00
2022-04-13 02:17:12 -06:00
echo "[+] Git version"
git --version
2023-02-25 14:58:06 -07:00
echo "[+] Enable git lfs"
git lfs install
2021-08-20 17:23:03 -06:00
echo " [+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME "
2023-05-10 03:08:03 -06:00
setupGit
cloneRepo
2021-12-14 16:42:49 -07:00
2020-03-29 09:30:55 -06:00
ls -la " $CLONE_DIR "
2021-06-11 23:46:38 -06:00
TEMP_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/"
2021-06-11 23:46:38 -06:00
mv " $CLONE_DIR /.git " " $TEMP_DIR /.git "
2021-12-08 15:55:47 -07:00
# $TARGET_DIRECTORY is '' by default
ABSOLUTE_TARGET_DIRECTORY = " $CLONE_DIR / $TARGET_DIRECTORY / "
2021-06-11 23:46:38 -06:00
2021-12-08 15:55:47 -07:00
echo " [+] Deleting $ABSOLUTE_TARGET_DIRECTORY "
rm -rf " $ABSOLUTE_TARGET_DIRECTORY "
2021-06-11 23:46:38 -06:00
2021-12-08 15:55:47 -07:00
echo " [+] Creating (now empty) $ABSOLUTE_TARGET_DIRECTORY "
mkdir -p " $ABSOLUTE_TARGET_DIRECTORY "
2021-08-29 06:24:21 -06:00
2021-12-08 15:55:47 -07:00
echo "[+] Listing Current Directory Location"
ls -al
echo "[+] Listing root Location"
ls -al /
2021-08-29 07:26:38 -06:00
2021-10-25 17:04: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 "
2023-05-10 03:08:03 -06:00
checkSource
2021-04-25 03:55:07 -06:00
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 "
2021-06-11 23:46:38 -06:00
cp -ra " $SOURCE_DIRECTORY " /. " $CLONE_DIR / $TARGET_DIRECTORY "
cd " $CLONE_DIR "
2020-12-02 09:07:47 -07:00
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 "
2020-11-04 09:45:08 -07:00
COMMIT_MESSAGE = " ${ COMMIT_MESSAGE /ORIGIN_COMMIT/ $ORIGIN_COMMIT } "
2021-04-25 10:59:53 -06:00
COMMIT_MESSAGE = " ${ COMMIT_MESSAGE / \$ GITHUB_REF / $GITHUB_REF } "
2020-11-04 09:23:02 -07:00
2022-04-13 02:17:12 -06:00
echo " [+] Set directory is safe ( $CLONE_DIR ) "
# Related to https://github.com/cpina/github-action-push-to-another-repository/issues/64 and https://github.com/cpina/github-action-push-to-another-repository/issues/64
# TODO: review before releasing it as a version
git config --global --add safe.directory " $CLONE_DIR "
2023-05-10 03:08:03 -06:00
switchBranch
2023-04-25 06:31:00 -06:00
2021-08-20 17:23:03 -06:00
echo "[+] Adding git commit"
2020-03-29 09:07:19 -06:00
git add .
2021-04-25 03:55:07 -06:00
2021-08-20 17:23:03 -06:00
echo "[+] git status:"
2020-08-10 08:02:28 -06:00
git status
2020-11-05 01:28:13 -07:00
2021-08-20 17:23:03 -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-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
2022-06-08 16:09:10 -06:00
git push " $GIT_CMD_REPOSITORY " --set-upstream " $TARGET_BRANCH "