How to invoke a script in the Teamcity CMD step?

I have a TeamCity job that runs a CMD step with a built-in bash script.

If the user cancels the task or he fails, I want to send a notification via tcp to another system.

I put a trap handler in a script, but the trap does not start, assuming that it is not transmitted by any of the usual signals.

#!/usr/bin/env bash 

function handle_error() {
    echo "TRAPPED $1"
    exit 1;
}

trap 'handle_error ERR' ERR
trap 'handle_error INT' INT
trap 'handle_error TERM' TERM
trap 'handle_error QUIT' QUIT
trap 'handle_error EXIT' EXIT

for i in $(seq 30); do echo $i; sleep 1; done

I get the following output when I run the task and then cancel it.

[11:34:36][Step 1/1] Starting: /opt/teamcity_agent_a11/temp/agentTmp/custom_script4059331588446614618
[11:34:36][Step 1/1] in directory: /opt/teamcity_agent_a11/work/84217cc201b5cd23
[11:34:36][Step 1/1] 1
[11:34:37][Step 1/1] 2
[11:34:38][Step 1/1] 3
[11:34:39][Step 1/1] 4
[11:34:40][Step 1/1] 5
[11:34:43][Step 1/1] Process exited with code 137
[11:34:43][Step 1/1] Step Command Line interrupted
[11:34:44]Build was interrupted. Artifacts will not be published for this build
[11:34:44]Build canceled

The message TRAPPED is not displayed. Is there any way to make this work?

Alternatively, is there a way to configure TeamCity to run another script if this job fails or is canceled?

- UPDATE -

Now I have found that with 10.0.2 TeamCity offers to control this behavior using parameters.

teamcity.force.kill.process.on.cancel.build=false, Teamcity SIGTERM, SIGKILL 60 (overridable via teamcity.force.kill.process.on.cancel.build.timeout.sec),

: https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848

+4
2

Exit Codes With Special Meanings 137 , SIGKILL,

,

docs (. 12.1.2. Usage of signals with kill)

+1

, 10.0.2 Teamcity .

teamcity.force.kill.process.on.cancel.build=false, Teamcity SIGTERM, SIGKILL 60 (overridable via teamcity.force.kill.process.on.cancel.build.timeout.sec),

: https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848

+2

Source: https://habr.com/ru/post/1692304/


All Articles