Skip to content

Instantly share code, notes, and snippets.

@danielealbano
Last active February 4, 2018 20:33
Show Gist options
  • Save danielealbano/648da54dbacda857c83934bba0584c6a to your computer and use it in GitHub Desktop.
Save danielealbano/648da54dbacda857c83934bba0584c6a to your computer and use it in GitHub Desktop.
BashCI - A simple CI written in bash for Gitlab (can be easily adapted to any other CI)
#!/bin/bash
JOBS_FOLDER="$(dirname $0)/jobs"
[email protected]
function response_headers() {
local CODE=$1
local REASON=$2
local CONTENT_TYPE=$3
if [ -z "$CONTENT_TYPE" ];
then
CONTENT_TYPE=text/html
fi
echo -en "HTTP/1.1 $CODE $REASON\r\n";
echo -en "Content-Type: $CONTENT_TYPE\r\n";
echo -en "Transfer-Encoding: Identity\r\n";
echo -en "Connection: Close\r\n";
echo -en "\r\n";
}
function response_200() {
response_headers "200" "OK" "$1"
}
function response_403() {
response_headers "403" "Forbidden" "$1"
}
function response_404() {
response_headers "404" "Not Found" "$1"
}
function response_500() {
response_headers "500" "Internal Error" "$1"
}
function check_gitlab_token() {
local EXPECTED_TOKEN=$1
if [ "$HTTP_HEADER_XGITLABTOKEN" != "$EXPECTED_TOKEN" ];
then
response_403
echo '<h1>Token mismatch</h1>'
exit 0
fi
}
function send_email() {
local SUBJECT=$1
local MESSAGE=$2
local ATTACHMENT=$3
}
# read the request
read HTTP_REQUEST
HTTP_REQUEST_URL="${HTTP_REQUEST#GET }"
HTTP_REQUEST_URL="${HTTP_REQUEST_URL% HTTP/*}"
HTTP_REQUEST_PATH="${HTTP_REQUEST_URL%%\?*}"
HTTP_REQUEST_QUERY="${HTTP_REQUEST_URL#*\?}"
unset HTTP_REQUEST
# read and parse the headers
while true; do
read HTTP_HEADER
[ -z "$HTTP_HEADER" ] && break
HTTP_HEADER_KEY=${HTTP_HEADER%%:*}
HTTP_HEADER_VALUE=${HTTP_HEADER#*:}
HTTP_HEADER_KEY=$(echo $HTTP_HEADER_KEY | tr [:lower:] [:upper:] | sed 's/[^A-Z]//g')
HTTP_HEADER_VALUE=$(echo $HTTP_HEADER_VALUE | sed -e 's/^[[:space:]]*//')
eval "HTTP_HEADER_$HTTP_HEADER_KEY='$HTTP_HEADER_VALUE'"
unset HTTP_HEADER
unset HTTP_HEADER_KEY
unset HTTP_HEADER_VALUE
done
# read and parse the query string
for HTTP_QUERY in $(echo $HTTP_REQUEST_QUERY | sed -e 's/\&/ /g') ; do
HTTP_QUERY_KEY=${HTTP_QUERY%%=*}
HTTP_QUERY_VALUE=${HTTP_QUERY#*=}
HTTP_QUERY_KEY=$(echo $HTTP_QUERY_KEY | tr [:lower:] [:upper:] | sed 's/[^A-Z]//g')
HTTP_QUERY_VALUE=$(echo $HTTP_QUERY_VALUE | tr -d ';''\001'-'\011''\013''\014''\016'-'\037''\200'-'\377')
eval "HTTP_QUERY_$HTTP_QUERY_KEY='$HTTP_QUERY_VALUE'"
unset HTTP_QUERY
unset HTTP_QUERY_KEY
unset HTTP_QUERY_VALUE
done
HTTP_QUERY_JOB=$(echo "$HTTP_QUERY_JOB" | sed 's/[^0*9A-Za-z_\-]//g')
if [ -z "$HTTP_QUERY_JOB" ];
then
response_404
echo '<h1>Job not specified</h1>'
exit 0
fi
JOB_PATH="$JOBS_FOLDER/$HTTP_QUERY_JOB.sh"
if [ "$(dirname $(realpath $JOB_PATH 2>/dev/null))" != "$(realpath $JOBS_FOLDER)" ];
then
response_404
echo '<h1>Invalid job name</h1>'
exit 0
fi
if [ ! -f "$JOB_PATH" ];
then
response_404
echo '<h1>Not-existant job</h1>'
exit 0
fi
if [ ! -x "$JOB_PATH" ];
then
response_404
echo '<h1>Job not executable</h1>'
exit 0
fi
JOB_OUTPUT=$($JOB_PATH 2>&1)
JOB_EXIT_CODE=$?
if [ $JOB_EXIT_CODE -eq 0 ];
then
response_200
echo "<h1>DONE</h1>"
else
response_500
echo "<h1>Execution Failed!</h1>"
echo "<hr>"
echo "<pre>"
echo $JOB_OUTPUT
echo "</pre>"
JOB_OUTPUT_PATH=$(mktemp)
echo "$JOB_OUTPUT" > $JOB_OUTPUT_PATH
send_email "[BashCI] Job $HTTP_QUERY_JOB Failed" "The job $HTTP_QUERY_JOB failed the execution with exit code $JOB_EXIT_CODE" "$JOB_OUTPUT_PATH"
rm $JOB_OUTPUT_PATH
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment