git-docker/rootfs/usr/local/bin/clone

124 lines
3.2 KiB
Plaintext
Raw Permalink Normal View History

2023-10-01 16:54:37 +00:00
#!/bin/bash
2018-08-08 21:43:53 +00:00
if [[ -n "${DRONE_WORKSPACE}" ]]; then
2023-10-01 18:44:02 +00:00
# ensure the unprivileged drone user can write
# to the workspace. This is required because
# the workspace is a docker volume and is owned
# by root.
# sudo mkdir -p ${DRONE_WORKSPACE}
# sudo chown drone:drone ${DRONE_WORKSPACE}
# ensure the workspace is the current working
# directory. This should already be the case,
# but we cd just to be safe.
cd "${DRONE_WORKSPACE}" || exit 1
2018-08-08 21:43:53 +00:00
fi
# force the home directory path.
2021-05-05 01:07:07 +00:00
2021-05-14 13:09:58 +00:00
# if [ "$HOME" != "/home/drone" ]; then
# echo "[DEBUG] setting default home directory"
# export HOME=/home/drone
# fi
2021-05-05 01:07:07 +00:00
2018-08-08 21:43:53 +00:00
# if the netrc enviornment variables exist, write
# the netrc file.
2023-10-01 16:54:37 +00:00
if [[ -n "${DRONE_NETRC_MACHINE}" ]]; then
2023-10-01 18:44:02 +00:00
cat <<EOF > "${HOME}/.netrc"
2018-08-08 21:43:53 +00:00
machine ${DRONE_NETRC_MACHINE}
login ${DRONE_NETRC_USERNAME}
password ${DRONE_NETRC_PASSWORD}
EOF
fi
# if the ssh_key environment variable exists, write
# the ssh key and add the netrc machine to the
# known hosts file.
2023-10-01 16:54:37 +00:00
if [[ -n "${DRONE_SSH_KEY}" ]]; then
2023-10-01 18:44:02 +00:00
mkdir "${HOME}/.ssh"
echo -n "${DRONE_SSH_KEY}" > "${HOME}/.ssh/id_rsa"
chmod 600 "${HOME}/.ssh/id_rsa"
2023-10-01 16:54:37 +00:00
2023-10-01 18:44:02 +00:00
touch "${HOME}/.ssh/known_hosts"
chmod 600 "${HOME}/.ssh/known_hosts"
ssh-keyscan -H "${DRONE_NETRC_MACHINE}" > /etc/ssh/ssh_known_hosts 2> /dev/null
2018-08-08 21:43:53 +00:00
fi
2023-10-01 17:03:40 +00:00
# # AWS codecommit support using AWS access key & secret key
# # Refer: https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-https-unixes.html
2023-10-01 17:03:40 +00:00
# if [[ -n "${DRONE_AWS_ACCESS_KEY}" ]]; then
# aws configure set aws_access_key_id "${DRONE_AWS_ACCESS_KEY}"
# aws configure set aws_secret_access_key "${DRONE_AWS_SECRET_KEY}"
# aws configure set default.region "${DRONE_AWS_REGION}"
2023-10-01 17:03:40 +00:00
# git config --global credential.helper '!aws codecommit credential-helper $@'
# git config --global credential.UseHttpPath true
# fi
2018-08-08 21:43:53 +00:00
# configure git global behavior and parameters via the
# following environment variables:
2019-01-21 20:30:42 +00:00
if [[ -z "${DRONE_COMMIT_AUTHOR_NAME}" ]]; then
2023-10-01 18:44:02 +00:00
export DRONE_COMMIT_AUTHOR_NAME=drone
fi
2019-01-21 20:30:42 +00:00
if [[ -z "${DRONE_COMMIT_AUTHOR_EMAIL}" ]]; then
2023-10-01 18:44:02 +00:00
export DRONE_COMMIT_AUTHOR_EMAIL=drone@localhost
fi
export GIT_AUTHOR_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_AUTHOR_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}
export GIT_COMMITTER_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_COMMITTER_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}
2018-08-08 21:43:53 +00:00
# invoke the sub-script based on the drone event type.
# TODO we should ultimately look at the ref, since
# we need something compatible with deployment events.
2023-10-01 16:54:37 +00:00
CLONE_TYPE=${DRONE_BUILD_EVENT}
case ${DRONE_COMMIT_REF} in
refs/tags/*)
CLONE_TYPE=tag
;;
refs/pull/*)
CLONE_TYPE=pull_request
;;
refs/pull-request/*)
CLONE_TYPE=pull_request
;;
refs/merge-requests/*)
CLONE_TYPE=pull_request
;;
esac
git_clone_retry(){
2023-10-01 18:44:02 +00:00
retries="${PLUGIN_RETRIES:-0}"
if [ -n "${retries##*[0-9]*}" ] || [ "${retries}" -lt 0 ]; then
echo "PLUGIN_RETRIES defined but is not a number: ${retries}" >&2
exit 1
fi
echo "Cloning with ${retries} retries"
n=0
until [ "$n" -gt "${retries}" ]; do
$1 && return
n=$((n+1))
done
exit 1
}
2023-10-01 16:54:37 +00:00
case ${CLONE_TYPE} in
2018-08-08 21:43:53 +00:00
pull_request)
2023-10-01 18:44:02 +00:00
git_clone_retry clone-pull-request
;;
2018-08-08 21:43:53 +00:00
tag)
2023-10-01 18:44:02 +00:00
git_clone_retry clone-tag
;;
2018-08-08 21:43:53 +00:00
*)
2023-10-01 18:44:02 +00:00
git_clone_retry clone-commit
;;
esac