Files
GitHub.webhook-scripts/src/bin/new_clone.sh
Clemens Schwaighofer 5251fbf140 Create basic SSH config and key generataion, other minor updates
Basis ssh host config and ssh keygen script, does not hard create, but prints out commads
Fix sudo set check in init.sh file
Repository name to ssh config host name fix in the new clone script. Change was done to match ssh config create file
2025-09-11 17:52:53 +09:00

90 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# AUTHOR: Clemens Schwaighofer
# DATE: 2025/6/27
# DESC: create a new basic clone
# COMMAND: new_clone.sh <Repo.git> <branch> [<host>] [<Repo Target Name>] [<remote name>]
REPOSITORY="$1";
BRANCH="$2";
REMOTE_HOST="$3";
REPOSITORY_FOLDER="$4"
REMOTE_NAME="$5";
if [ -z "${REMOTE_NAME}" ]; then
REMOTE_NAME="origin"
fi;
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
# shellcheck source=init.sh
. "${BASE_FOLDER}init.sh";
if [ -z "${REPOSITORY}" ]; then
echo "[!] Must set a repository path";
error=1;
fi;
# if remote host is empty try to set from repository
if [ -z "${REMOTE_HOST}" ]; then
if [[ "${REPOSITORY}" == *":"* ]]; then
REMOTE_HOST=$(echo "${REPOSITORY}" | cut -d ":" -f 1);
else
echo "[!] Must set a repository path with remote host for the repository";
error=1;
fi;
fi;
# if we have an ":" in the repository, split by it and replace it with the remote host
if [[ "${REPOSITORY}" == *":"* ]]; then
REPOSITORY=$(echo "${REPOSITORY}" | cut -d ":" -f 2);
fi;
# strip .git from the repository path, this is folder and ssh key Host name
if [ -z "${REPOSITORY_FOLDER}" ]; then
GIT_REPOSITORY_NAME=$(basename "${REPOSITORY}" .git);
else
GIT_REPOSITORY_NAME="${REPOSITORY_FOLDER}";
fi;
if [ $error -eq 1 ]; then
exit;
fi;
error=0
echo "* Validate SSH PEM Key exist and SSH config";
if ! grep "Host ${GIT_REPOSITORY_NAME}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then
echo "[!] ssh config entry for Host ${GIT_REPOSITORY_NAME} is missing";
error=1;
else
# make sure the identiy file is there
# grep "IdentityFile" in this
SSH_TEST=("${SUDO_COMMAND[@]}" "ssh" "${REMOTE_HOST}");
result=$("${SSH_TEST[@]}" 2>&1);
# this can be key or deploy key
validate_string="You've successfully authenticated"
if [[ "$result" != *"$validate_string"* ]]; then
echo "Could not connect to ${REMOTE_HOST}: ${result}";
error=1;
fi;
fi;
if [ $error -eq 1 ]; then
exit;
fi;
unique_id=$(uuidgen | tr -d '-' | head -c 8);
# log folder target
LOG_FILE="${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}${GIT_REPOSITORY_NAME}.log";
# from the repository get the last path without the .git so we have the target folder
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] New clone from ${REMOTE_HOST}:${REPOSITORY}::${BRANCH} into ${GIT_REPOSITORY_NAME}" | tee -a "$LOG_FILE";
# clone everything
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "clone" "-b" "${BRANCH}" "--single-branch" "--depth" "1" "--origin" "${REMOTE_NAME}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${GIT_REPOSITORY_NAME}")
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
# set the repository folder
GIT_REPOSITORY_FOLDER="${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${GIT_REPOSITORY_NAME}";
# show origin info
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" remote show "${REMOTE_NAME}" );
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
# get last log entry
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" log -n 1 --pretty=short --no-color);
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [FINISH] clone completed" | tee -a "$LOG_FILE";
# __END__