refac: project

This commit is contained in:
2023-10-01 18:54:37 +02:00
parent 39d233b3d9
commit d6adb0e2e3
37 changed files with 1106 additions and 1146 deletions

124
rootfs/usr/local/bin/clone Executable file
View File

@ -0,0 +1,124 @@
#!/bin/bash
if [[ -n "${DRONE_WORKSPACE}" ]]; then
# 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
fi
# force the home directory path.
# if [ "$HOME" != "/home/drone" ]; then
# echo "[DEBUG] setting default home directory"
# export HOME=/home/drone
# fi
# if the netrc enviornment variables exist, write
# the netrc file.
if [[ -n "${DRONE_NETRC_MACHINE}" ]]; then
cat <<EOF > "${HOME}/.netrc"
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.
if [[ -n "${DRONE_SSH_KEY}" ]]; then
mkdir "${HOME}/.ssh"
echo -n "${DRONE_SSH_KEY}" > "${HOME}/.ssh/id_rsa"
chmod 600 "${HOME}/.ssh/id_rsa"
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
fi
# AWS codecommit support using AWS access key & secret key
# Refer: https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-https-unixes.html
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}"
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
fi
# configure git global behavior and parameters via the
# following environment variables:
if [[ -z "${DRONE_COMMIT_AUTHOR_NAME}" ]]; then
export DRONE_COMMIT_AUTHOR_NAME=drone
fi
if [[ -z "${DRONE_COMMIT_AUTHOR_EMAIL}" ]]; then
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}
# 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.
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(){
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
}
case ${CLONE_TYPE} in
pull_request)
git_clone_retry clone-pull-request
;;
tag)
git_clone_retry clone-tag
;;
*)
git_clone_retry clone-commit
;;
esac

View File

@ -0,0 +1,43 @@
#!/bin/bash
set -x
FLAGS=""
if [[ -n "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin "${DRONE_REMOTE_URL}"
fi
# the branch may be empty for certain event types,
# such as github deployment events. If the branch
# is empty we checkout the sha directly. Note that
# we intentially omit depth flags to avoid failed
# clones due to lack of history.
if [[ -z "${DRONE_COMMIT_BRANCH}" ]]; then
set -e
set -x
git fetch origin
git checkout --quiet --force "${DRONE_COMMIT_SHA}"
exit 0
fi
# the commit sha may be empty for builds that are
# manually triggered in Harness CI Enterprise. If
# the commit is empty we clone the branch.
if [[ -z "${DRONE_COMMIT_SHA}" ]]; then
set -e
set -x
git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:"
git checkout -b "${DRONE_COMMIT_BRANCH}" "origin/${DRONE_COMMIT_BRANCH}"
exit 0
fi
set -e
set -x
git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:"
git checkout "${DRONE_COMMIT_SHA}" -b "${DRONE_COMMIT_BRANCH}"

View File

@ -0,0 +1,20 @@
#!/bin/bash
FLAGS=""
if [[ -n "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin "${DRONE_REMOTE_URL}"
fi
set -e
set -x
git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:"
git checkout "${DRONE_COMMIT_BRANCH}"
git fetch origin "${DRONE_COMMIT_REF}:"
git merge "${DRONE_COMMIT_SHA}"

17
rootfs/usr/local/bin/clone-tag Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
FLAGS=""
if [[ -n "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin "${DRONE_REMOTE_URL}"
fi
set -e
set -x
git fetch "${FLAGS}" origin "+refs/tags/${DRONE_TAG}:"
git checkout --quiet --force FETCH_HEAD

View File

@ -0,0 +1 @@
../../../../LICENSE