Skip to content

Instantly share code, notes, and snippets.

@kobymeir
Forked from VladRassokhin/service-messages.sh
Last active October 15, 2017 22:24
Show Gist options
  • Save kobymeir/a4f82fd0eadf7f2681bd6365cc1de08a to your computer and use it in GitHub Desktop.
Save kobymeir/a4f82fd0eadf7f2681bd6365cc1de08a to your computer and use it in GitHub Desktop.
Simple bash functions library to say something to TeamCity. See https://confluence.jetbrains.com/display/TCDL/Build+Script+Interaction+with+TeamCity will only printout messages if running under TeamCity, otherwise nothing will be printed out.
#!/usr/bin/env bash
# Documentation for TeamCity messages:
# https://confluence.jetbrains.com/display/TCDL/Build+Script+Interaction+with+TeamCity
function __tc_encode {
# Only unicode characters are not supported
echo -n "$1" | sed "s/\([|']\)/\|\1/g; s/\[/\|\[/g; s/\]/\|\]/g; s/\r/\|r/g;" | sed ':a;N;$!ba;s/\n/|n/g'
}
function __tc_echo {
# Only print messages if running under TeamCity
if [ -n "${TEAMCITY_VERSION}" ]; then
echo "$1"
fi
}
function __tc_message {
__tc_echo "##teamcity[message text='$(__tc_encode "$2")' status='${1:-NORMAL}']"
}
function __tc_simple {
__tc_echo "##teamcity[$1 '$(__tc_encode "$2")']"
}
function tc_message {
__tc_message "NORMAL" "$1"
}
function tc_message_failure {
__tc_message "FAILURE" "$1"
}
function tc_message_error {
__tc_message "ERROR" "$1"
}
function tc_message_warning {
__tc_message "WARNING" "$1"
}
function tc_message_normal {
__tc_message "NORMAL" "$1"
}
function tc_block_open {
__tc_echo "##teamcity[blockOpened name='$(__tc_encode "$1")']"
}
function tc_block_close {
__tc_echo "##teamcity[blockClosed name='$(__tc_encode "$1")']"
}
function tc_progress {
__tc_simple "progressMessage" "$1"
}
function tc_progress_start {
__tc_simple "progressStart" "$1"
}
function tc_progress_finish {
__tc_simple "progressFinish" "$1"
}
function tc_build_problem {
# $1 - text (required)
# $2 - identity (optional)
if [ "$2" = "" ]; then
__tc_echo "##teamcity[buildProblem description='$(__tc_encode "$1")']"
else
__tc_echo "##teamcity[buildProblem description='$(__tc_encode "$1")' identity='$(__tc_encode "$2")']"
fi
}
function tc_build_status {
# $1 - text (required)
# $2 - status (optional)
if [ "$2" = "" ]; then
__tc_echo "##teamcity[buildStatus text='$(__tc_encode "$1")']"
else
__tc_echo "##teamcity[buildStatus text='$(__tc_encode "$1")' status='$(__tc_encode "$2")']"
fi
}
function tc_build_number {
__tc_simple "buildNumber" "$1"
}
function tc_set_parameter {
__tc_echo "##teamcity[setParameter name='$(__tc_encode "$1")' value='$(__tc_encode "$2")']"
}
# Testing code
function __tc_unit_test()
{
tc_block_open "Test block"
tc_message "Test message"
tc_message "$(echo -e "Test\nmultiline\n\rmessage")"
tc_message "Test\nnon-multiline\n\rmessage"
tc_message "Test
multiline
message"
tc_block_close "Test block"
tc_block_open 'Test "quoted"'
tc_message "Test escaping ' | [ ]"
tc_message "Test non-escaping \ / { }"
tc_block_close 'Test "quoted"'
}
# If the script is being executed with "test" argument run the unit test.
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && [[ "test" == "${1}" ]] && __tc_unit_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment