mirror of
https://github.com/cpina/github-action-push-to-another-repository.git
synced 2025-01-10 09:09:00 -07:00
5520368043
Created functions.sh file
112 lines
3.0 KiB
Bash
Executable File
112 lines
3.0 KiB
Bash
Executable File
#!/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
|
|
|
|
. ./functions.sh
|
|
|
|
echo "[+] Action start"
|
|
getArgs "$@"
|
|
|
|
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
|
|
then
|
|
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
|
|
fi
|
|
|
|
if [ -z "$USER_NAME" ]
|
|
then
|
|
USER_NAME="$DESTINATION_GITHUB_USERNAME"
|
|
fi
|
|
|
|
# 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
|
|
echo "[+] Using SSH_DEPLOY_KEY"
|
|
sshDeployKey
|
|
elif [ -n "${API_TOKEN_GITHUB:=}" ]
|
|
then
|
|
echo "[+] Using API_TOKEN_GITHUB"
|
|
githubToken
|
|
else
|
|
echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY)"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
CLONE_DIR=$(mktemp -d)
|
|
|
|
echo "[+] Git version"
|
|
git --version
|
|
|
|
echo "[+] Enable git lfs"
|
|
git lfs install
|
|
|
|
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
|
|
setupGit
|
|
|
|
cloneRepo
|
|
|
|
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"
|
|
|
|
echo "[+] Listing Current Directory Location"
|
|
ls -al
|
|
|
|
echo "[+] Listing root Location"
|
|
ls -al /
|
|
|
|
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
|
|
|
|
echo "[+] List contents of $SOURCE_DIRECTORY"
|
|
ls "$SOURCE_DIRECTORY"
|
|
|
|
echo "[+] Checking if local $SOURCE_DIRECTORY exist"
|
|
checkSource
|
|
|
|
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"
|
|
|
|
echo "[+] Files that will be pushed"
|
|
ls -la
|
|
|
|
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}"
|
|
|
|
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"
|
|
|
|
switchBranch
|
|
|
|
echo "[+] Adding git commit"
|
|
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 "[+] Pushing git commit"
|
|
# --set-upstream: sets de branch when pushing to a branch that does not exist
|
|
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"
|