What version of GoCD are you using? If your version is old, v3 may not be available. You can always use the `latest` version by using `-H 'Accept: application/vnd.go.cd+json`. Adding `-v` to `curl` will also print response header which will indicate the version `latest` resolves to, which might be `v2` in your case.
I'm running 21.1.0. v3 was first available since 20.9.0 according to api.gocd.org. # works for me curl -v \ -H 'Accept: application/vnd.go.cd+json' \ -H "Authorization: Bearer $token" \ "http://${service_host_port}/go/api/stages/$pipeline/$stage/history" As far as the history goes, it should be in the correct order. Perhaps it's because you aren't filtering out current running stages (i.e., stages with "Unknown" as the result)? Try this: # Get the whole JSON result into a variable of the latest stage that finished # NOTE: stages that are scheduled/running will have result == "Unknown", so we # filter those out with `jq` last_stage_result="$(curl -v \ -H 'Accept: application/vnd.go.cd+json' \ -H "Authorization: Bearer $token" "https://$host_and_port/go/api/stages/$pipeline/$stage/history" | \ jq '[.stages[] | select (.result != "Unknown")][0]')" # extract the result state from json blob result_state="$(echo "$last_stage_result" | jq -r .result)" if [ "$result_state" != "Passed" ]; then # extract the counter of that same result counter="$(echo $last_stage_result | jq -r .counter)" # do the rest of your code here fi On Mon, Feb 22, 2021 at 4:03 PM Gabriel Callaghan < [email protected]> wrote: > Hi, > > I am running into some different results than what I wanted. My goal is to > use jq to filter results of the "get stage history" ( > https://api.gocd.org/current/#get-stage-history) so that I can get the > most recent run and to check it for if it has been passed or not. > > At the moment, I am using that API with v2+ on the third line as it > provides results. If I change it to v3+, it gets "<html><body><h2>404 Not > found</h2></body></html>". This is why I have been using it as v2+. > However, when using v2+ I am getting back weird results. It does not seem > to be in order from most recent results to the oldest, it is being filtered > somehow? It means that as a result, my script is trying to run a pipeline > that is an older version, when it should be checking and running the most > recent version of a pipeline. > > Why is it doing that, and what can I do to make sure that the stage > history json provides results from the most recent to the latest? > > If it helps, my current script is this: > > #! /bin/bash > > fetchPipelineHistory=$( curl "{COMPANY > NAME}/go/api/stages/Games-AcesHigh-PHY-Test/Approve/history" \ > -H "Authorization: Bearer {MY TOKEN)" \ > -H 'Accept: application/vnd.go.cd.v2+json') > > getResultOfCurrentStage=$( echo $fetchPipelineHistory | jq -r > '.stages[].result') > > counter=$( echo $fetchPipelineHistory | jq -r '.stages[0].counter') > > if [ "$getResultOfCurrentStage" != "Passed" ] > then > > echo "{COMPANY > NAME}/api/stages/Games-AcesHigh-PHY-Test/$counter/Approve/run" > > curl "{COMPANY > NAME}/go/api/stages/Games-AcesHigh-PHY-Test/$counter/Approve/run" \ > -H "Authorization: Bearer {MY TOKEN}" \ > -H 'X-GoCD-Confirm: true' \ > -H 'Accept: application/vnd.go.cd.v2+json' \ > -X POST > > fi > > Thank you for reading, > Gabes. > > > -- > You received this message because you are subscribed to the Google Groups > "go-cd" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/go-cd/bcb78dac-5cdb-4216-ad87-a9e56291ef4an%40googlegroups.com > <https://groups.google.com/d/msgid/go-cd/bcb78dac-5cdb-4216-ad87-a9e56291ef4an%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "go-cd" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/CAPKX9jYBGsCm_DURCpUWKo%3D83kub-O%3DfdBvh76v3H3PF8DOddw%40mail.gmail.com.
