Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to cancel previous builds #101

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .teamcity/src/subprojects/build/core/ProjectCore.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package subprojects.build.core

import jetbrains.buildServer.configs.kotlin.*
import jetbrains.buildServer.configs.kotlin.buildSteps.*
import jetbrains.buildServer.configs.kotlin.triggers.*
import subprojects.*
import subprojects.build.*
Expand Down Expand Up @@ -36,6 +37,37 @@ object ProjectCore : Project({

buildType {
allowExternalStatus = true

steps {
script {
name = "Cancel Previous Builds"
scriptContent = """
#!/bin/bash

function teamcityApiRequest() {
local route=${'$'}1; shift
curl --silent --user "%teamcity.auth.userId%:%teamcity.auth.password%" \
"%teamcity.serverUrl%/app/rest${'$'}route" \
--header "Content-Type: application/json" \
"${'$'}@"
}

currentBuildId="%teamcity.build.id%"

# Get running builds on the same branch
runningBuilds=${'$'}(teamcityApiRequest "/builds?locator=buildType:%system.teamcity.buildType.id%,branch:%teamcity.build.branch%,state:running" | jq -r '.build[].id')

# Cancel each running build except the current one
body='{"comment": "Superseded by build '${'$'}currentBuildId'", "readdIntoQueue": false}'
for buildId in ${'$'}runningBuilds; do
if [ "${'$'}buildId" != "${'$'}currentBuildId" ]; then
teamcityApiRequest "/builds/${'$'}buildId" --request POST --data "${'$'}body"
fi
done
""".trimIndent()
}
}

createCompositeBuild(
buildId = "KtorCore_All",
buildName = "Build All Core",
Expand Down