Skip to content
Snippets Groups Projects
Commit 4799ece1 authored by jurgenhaas's avatar jurgenhaas
Browse files

Merge branch 'develop' into 'main'

Merging develop into main

See merge request !37
parents c3581561 f7c74b4d
Branches
No related tags found
1 merge request!37Merging develop into main
Pipeline #772288 passed
#!/bin/bash
set -e
case "$1"
in
db) mode=CollectDatabase;;
files) mode=CollectFiles;;
db) task=CollectDatabase;;
files) task=CollectFiles;;
*) echo "Provide either db or files as a first argument!"
exit 1;;
esac
......@@ -11,168 +13,27 @@ branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "x$2" != "x" ]]; then
branch=$2
fi
rm -rf /tmp/${task}/
export DEBUG=no
function gitlab() {
EXPECTED=$1
METHOD=$2
REQUEST=$3
shift 3
if [[ "${DEBUG}" == "yes" ]]; then
echo curl --write-out "\nHTTP-Code:%{http_code}" -X "$METHOD" -s -H "Private-Token: ${GITLAB_PRIVATE_TOKEN}" "${GITLAB_URL}"/api/v4/"$REQUEST" "$@"
fi
output="$(curl --write-out "\nHTTP-Code:%{http_code}" -X "$METHOD" -s -H "Private-Token: ${GITLAB_PRIVATE_TOKEN}" "${GITLAB_URL}"/api/v4/"$REQUEST" "$@")"
if [[ "${DEBUG}" == "yes" ]]; then
echo $output
fi
result="$(echo "$output" | grep "{")"
code="$(echo "$output" | grep -m 1 "HTTP-Code:" | cut -d: -f2)"
if [[ $code -eq $EXPECTED ]]; then
return
fi
if [[ "${DEBUG}" == "no" ]]; then
echo $output
echo "Trigger pipeline ..."
glab ci run -b ${branch} --variables-env TRIGGERMODE:manual,TRIGGERTASK:${task},TRIGGER_INPUT:${branch}
echo "Waiting for pipeline to finish ..."
glab ci status -b ${branch} --live --compact
echo "Download artifact ..."
glab ci artifact ${branch} ${task}Manual --path="/tmp/${task}/"
if [[ -d /tmp/${task}/ ]]; then
if [[ "$task" == "CollectDatabase" ]]; then
echo "Copy DB dump into PHP container ..."
docker compose cp /tmp/${task}/drupal.sql php:/tmp
echo "Import DB ..."
drush -y sql:drop
drush -y sql:query --file=/tmp/drupal.sql
fi
echo "$METHOD request to $REQUEST did not succeed! Responds with code $code instead of $EXPECTED"
exit 99
}
function isNumeric() {
re='^[0-9]+$'
if [[ $1 =~ $re ]]; then
return 0
if [[ "$task" == "CollectFiles" ]]; then
echo "Please move the files from /tmp/${task}/ into your file directories manually."
fi
return 1
}
function parseUrl() {
# extract the protocol
proto="$(echo "$1" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="${1/$proto/}"
# extract the user (if any)
user="$(echo "$url" | grep @ | cut -d@ -f1)"
# extract the host and port
hostport="$(echo "${url/$user@/}" | cut -d/ -f1)"
# by request host without port
# shellcheck disable=SC2001
host="$(echo "$hostport" | sed -e 's,:.*,,g')"
# by request - try to extract the port
port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
# extract the path (if any)
path="$(echo "$url" | sed -e 's,:,/,g' | grep / | cut -d/ -f2-)"
}
if [[ -n ${CI_SERVER_HOST} ]]; then
GITLAB_URL=https://$CI_SERVER_HOST
fi
if [[ "x${GITLAB_URL}" == "x" ]]; then
echo "Missing GITLAB_URL environment variable, should be set to e.g. https://gitlab.lakedrops.com"
exit 3
fi
if [[ "x${GITLAB_PRIVATE_TOKEN}" == "x" ]]; then
echo "Missing GITLAB_PRIVATE_TOKEN environment variable"
exit 4
fi
parseUrl "$GITLAB_URL"
host1=$host
parseUrl "$(git config remote.origin.url)"
host2=$host
if [[ "$host1" != "$host2" ]]; then
echo "This repository doesn't match the provided GitLab instance"
exit 5
fi
if [[ -n ${CI_PROJECT_ID} ]]; then
PRJID=$CI_PROJECT_ID
else
echo "Find project ID from remote URL ..."
# shellcheck disable=SC2001
path="$(echo "$path" | sed -e 's,\..*,,g')"
gitlab 200 GET "projects?archived=0&search_namespaces=1&search=$path"
i=0
while true
do
NAMESPACE=$(echo "$result" | jq -r .[$i]."path_with_namespace")
if [[ "$NAMESPACE" == "$path" ]]; then
PRJID=$(echo "$result" | jq -r .[$i]."id")
if [[ "a${PRJID}a" == "aa" ]]; then
echo "No project found"
exit 6
fi
if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
echo "Can not find project ID"
exit 6
fi
break
fi
if [[ "a${NAMESPACE}a" == "aa" ]]; then
echo "No project found"
exit 6
fi
((i=i+1))
done
fi
echo "Find pipeline trigger token ..."
gitlab 200 GET projects/"$PRJID"/triggers
TRIGGERTOKEN=none
i=0
while [ $i -ge 0 ]
do
TOKEN=$(echo "$result" | jq -r .[$i]."token")
if [[ "$TOKEN" == "null" ]]; then
i=-1
elif [[ ${#TOKEN} -eq 4 ]]; then
((i=i+1))
else
TRIGGERTOKEN=$TOKEN
i=-1
fi
done
if [[ "$TRIGGERTOKEN" == "none" ]] || [[ "a${TRIGGERTOKEN}a" == "aa" ]]; then
echo "No token found, creating one ..."
gitlab 201 POST projects/"$PRJID"/triggers --data "description=Created by L3D"
TRIGGERTOKEN=$(echo "$result" | jq -r ."token")
if [[ "$TRIGGERTOKEN" == "none" ]] || [[ "a${TRIGGERTOKEN}a" == "aa" ]]; then
echo "No trigger token found and can not create one"
exit 7
fi
fi
echo "Trigger pipeline ..."
gitlab 201 POST projects/"$PRJID"/trigger/pipeline --data "token=$TRIGGERTOKEN" --data "ref=${branch}" --data "variables[TRIGGERTASK]=$mode" --data "variables[TRIGGER_INPUT]=$branch"
PIPELINEID=$(echo "$result" | jq -r ."id")
PIPELINESTATUS=$(echo "$result" | jq -r ."status")
while [ $PIPELINESTATUS != "success" ]
do
sleep 3
gitlab 200 GET /projects/"$PRJID"/pipelines/"$PIPELINEID"
PIPELINESTATUS=$(echo "$result" | jq -r ."status")
echo " $PIPELINESTATUS ..."
case "${PIPELINESTATUS}"
in
created) ;;
pending) ;;
running) ;;
success) ;;
*) exit 8;;
esac
done
gitlab 200 GET /projects/"$PRJID"/pipelines/"$PIPELINEID"/jobs
JOBID=$(echo "$result" | jq -r .[0]."id")
gitlab 200 GET /projects/"$PRJID"/jobs/"$JOBID"/artifacts --output /tmp/artifacts.zip
echo "Extract archive ..."
unzip -o /tmp/artifacts.zip -d /tmp
if [[ "$mode" == "CollectDatabase" ]]; then
echo "Copy DB dump into PHP container ..."
docker compose cp /tmp/drupal.sql php:/tmp
echo "Import DB ..."
drush -y sql:drop
drush -y sql:query --file=/tmp/drupal.sql
fi
if [[ "$mode" == "CollectFiles" ]]; then
echo "Please move the files from /tmp into your file directories manually."
echo "Something went wrong"
exit 2
fi
#!/bin/bash
export DEBUG=no
function gitlab() {
EXPECTED=$1
METHOD=$2
REQUEST=$3
shift 3
if [[ "${DEBUG}" == "yes" ]]; then
echo curl --write-out "\nHTTP-Code:%{http_code}" -X "$METHOD" -s -H "Private-Token: ${GITLAB_PRIVATE_TOKEN}" "${GITLAB_URL}"/api/v4/"$REQUEST" "$@"
fi
output="$(curl --write-out "\nHTTP-Code:%{http_code}" -X "$METHOD" -s -H "Private-Token: ${GITLAB_PRIVATE_TOKEN}" "${GITLAB_URL}"/api/v4/"$REQUEST" "$@")"
if [[ "${DEBUG}" == "yes" ]]; then
echo $output
fi
result="$(echo "$output" | grep "{")"
code="$(echo "$output" | grep -m 1 "HTTP-Code:" | cut -d: -f2)"
if [[ $code -eq $EXPECTED ]]; then
return
fi
if [[ "${DEBUG}" == "no" ]]; then
echo $output
fi
echo "$METHOD request to $REQUEST did not succeed! Responds with code $code instead of $EXPECTED"
exit 99
}
function isNumeric() {
re='^[0-9]+$'
if [[ $1 =~ $re ]]; then
return 0
fi
return 1
}
function parseUrl() {
# extract the protocol
proto="$(echo "$1" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="${1/$proto/}"
# extract the user (if any)
user="$(echo "$url" | grep @ | cut -d@ -f1)"
# extract the host and port
hostport="$(echo "${url/$user@/}" | cut -d/ -f1)"
# by request host without port
# shellcheck disable=SC2001
host="$(echo "$hostport" | sed -e 's,:.*,,g')"
# by request - try to extract the port
port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
# extract the path (if any)
path="$(echo "$url" | sed -e 's,:,/,g' | grep / | cut -d/ -f2-)"
}
set -e
SOURCEBRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "x$TARGETBRANCH" == "x" ]]; then
......@@ -67,59 +17,7 @@ if [[ "$TARGETBRANCH" == "$SOURCEBRANCH" ]]; then
echo "Source and target branches must be different."
exit 2
fi
if [[ -n ${CI_SERVER_HOST} ]]; then
GITLAB_URL=https://$CI_SERVER_HOST
fi
if [[ "x${GITLAB_URL}" == "x" ]]; then
echo "Missing GITLAB_URL environment variable, should be set to e.g. https://gitlab.lakedrops.com"
exit 3
fi
if [[ "x${GITLAB_PRIVATE_TOKEN}" == "x" ]]; then
echo "Missing GITLAB_PRIVATE_TOKEN environment variable"
exit 4
fi
parseUrl "$GITLAB_URL"
host1=$host
parseUrl "$(git config remote.origin.url)"
host2=$host
if [[ "$host1" != "$host2" ]]; then
echo "This repository doesn't match the provided GitLab instance"
exit 5
fi
if [[ -n ${CI_PROJECT_ID} ]]; then
PRJID=$CI_PROJECT_ID
else
echo "Find project ID from remote URL ..."
# shellcheck disable=SC2001
path="$(echo "$path" | sed -e 's,\..*,,g')"
gitlab 200 GET "projects?search_namespaces=1&search=$path"
i=0
while true
do
NAMESPACE=$(echo "$result" | jq -r .[$i]."path_with_namespace")
if [[ "$NAMESPACE" == "$path" ]]; then
PRJID=$(echo "$result" | jq -r .[$i]."id")
if [[ "a${PRJID}a" == "aa" ]]; then
echo "No project found"
exit 6
fi
if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
echo "Can not find project ID"
exit 6
fi
break
fi
if [[ "a${NAMESPACE}a" == "aa" ]]; then
echo "No project found"
exit 6
fi
((i=i+1))
done
fi
echo "Create merge request ..."
# Create MR
TITLE="Merging $SOURCEBRANCH into $TARGETBRANCH"
if [[ "${SKIPCONFIGIMPORT}" == "yes" ]]; then
TITLE="${TITLE} [SKIP_CONFIG_IMPORT]"
......@@ -130,16 +28,6 @@ fi
if [[ "$1" == "major" ]]; then
TITLE="${TITLE} [MAJOR_VERSION]"
fi
gitlab 201 POST projects/"$PRJID"/merge_requests --data "source_branch=$SOURCEBRANCH" --data "target_branch=$TARGETBRANCH" --data "title=$TITLE"
MRID=$(echo "$result" | jq -r ."iid")
if [[ $(isNumeric "$MRID") -eq 1 ]]; then
echo "Can not create merge request"
exit 7
fi
echo "Created MR at $(echo "$result" | jq -r ."web_url")"
echo "Merge ..."
# Merge MR
sleep 5
gitlab 200 PUT projects/"$PRJID"/merge_requests/"$MRID"/merge --data "should_remove_source_branch=0"
echo "Merged successfully!"
glab mr create --fill --yes --source-branch ${SOURCEBRANCH} --target-branch ${TARGETBRANCH} --title "$TITLE"
glab mr merge --yes ${SOURCEBRANCH}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment