How to stop a build that is currently running/being executed?
Asked Answered
S

1

8

I am trying to stop Bamboo builds (that are currently running) if they pass a certain use case.

I have a list of the builds that I need to stop. Now, I want to send a REST request to stop the build(s) very similar to the "Stop Build" button that is on the top right of a build (see image). enter image description here

In the REST API documentation I have only seen this which only stops the build if it is queued.

https://docs.atlassian.com/bamboo/REST/3.3-SNAPSHOT/

/queue/{projectKey}-{buildKey}-{buildNumber}

Stop build execution, however only if build has not been started yet - so if is waiting in the build queue. If build does not exist in the queue anymore, method has no effect.

I need a way to stop RUNNING builds.

Shaped answered 6/9, 2016 at 23:25 Comment(1)
Unfortunately, there is a bug in Bamboo Rest API. The behavior of "Actions --> Stop job" is different from the Rest API call. Bamboo cannot stop a build properly, it continues to run until all child processes are finished.Pickled
S
2

I was able to figure out the names of the Bamboo builds I needed to stop via my own automation server. Then I was able to achieve this by hitting the stopPlan button code directly. Here is the Ruby method I wrote:

# Get request to stop a build located at the given url
def stop_bamboo_build_request(build_key)
  logger.debug "Build Key: #{build_key}"
  uri = URI("#{Rails.configuration.bamboo_base_url}/build/admin/stopPlan.action?planKey=#{build_key}")

  # Create client
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER

  # Create Request
  req =  Net::HTTP::Get.new(uri)
  # Add Auth
  req.basic_auth(Rails.configuration.bamboo_username, Rails.configuration.bamboo_password)

  # Add headers
  req.add_field "X-Atlassian-Token", "no-check"

  # Fetch Request
  res = http.request(req)
  logger.debug "Response HTTP Status Code: #{res.code}"
  logger.debug "Response HTTP Response Body: #{res.body}"
rescue StandardError => e
  logger.debug "HTTP Request failed (#{e.message})"
end
Shaped answered 28/9, 2016 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.