Skip to content

Instantly share code, notes, and snippets.

@blmoore
Forked from VladRassokhin/service-messages.sh
Last active October 3, 2018 14:59
Show Gist options
  • Save blmoore/90bc9120f5d11777c6b1d47f41a300a3 to your computer and use it in GitHub Desktop.
Save blmoore/90bc9120f5d11777c6b1d47f41a300a3 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
#!/usr/bin/env bash
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 -e ':a' -e 'N' -e '$!ba' -e 's/\n/|n/g'
}
function __tc_message {
echo "##teamcity[message text='$(__tc_encode "$2")' status='${1:-NORMAL}']"
}
function __tc_simple {
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 {
echo "##teamcity[blockOpened name='$(__tc_encode "$1")']"
}
function tc_block_close {
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"
}
# $1 - text (required)
# $2 - identity (optional)
function tc_build_problem {
if [ "$2" = "" ]; then
echo "##teamcity[buildProblem description='$(__tc_encode "$1")']"
else
echo "##teamcity[buildProblem description='$(__tc_encode "$1")' identity='$(__tc_encode "$2")']"
fi
}
# $1 - text (required)
# $2 - status (optional)
function tc_build_status {
if [ "$2" = "" ]; then
echo "##teamcity[buildStatus text='$(__tc_encode "$1")']"
else
echo "##teamcity[buildStatus text='$(__tc_encode "$1")' status='$(__tc_encode "$2")']"
fi
}
function tc_build_number {
__tc_simple 'buildNumber' "$1"
}
function tc_set_parameter {
echo "##teamcity[setParameter name='$(__tc_encode "$1")' value='$(__tc_encode "$2")']"
}
#!/usr/bin/env bash
source service-messages.sh
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"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment