Retrieving latest artifacts for a specific branch via API?

I’ve looked at the various API versions and I’m having trouble getting artifacts via a CLI script I want to run and was wondering if someone can point me in the right direction?

What I really want is just the latest artifacts for the most recent completed job for a given branch. The v1.1 API that should provide this doesn’t seem to work and I can’t find a combo of v1.1/v2 APIs to chain together to navigate down to what I want.

The org/project/branch are static and I just want the latest completed artifact for that specific branch. I

v1.1

  1. Has an API that lets you retrieve latest artifacts for a branch.
  2. Doesn’t seem to work for me. There’s another post I can’t link to titled “latest artifacts API returns empty list” that has the same problem (I haven’t contacted support yet)
  3. If I include the build number and use the other API it works fine, so I know my token is good. There doesn’t seem to be a good API via 1.1 to get a list of builds, so I don’t know that there’s a way to work around this (except maybe using v2 API to get a list of builds, if that’s even supported)

v2.x

  1. This doesn’t seem to have the support to navigate thru the workflows down into the job level so that I can get the artifacts. Th

GraphQL

  1. This has been in beta for quite some time (since 2019 per some other posts) so I didn’t dig too deep into it because it’s marked (somewhere) as not stable (the API/controller name is beta/unstable or something like that)

Interesting observations:

I have been running a curl command against the v1.1 API quite a bit. It always returns [].

curl -L "https://circleci.com/api/v1.1/project/gh/<username>/<repo>/latest/artifacts?branch=develop&filter=completed" -H "Accept: application/json" -H "Content-Type:
 application/json" -H "Circle-Token: $CIRCLECI_TOKEN"

I ran the command one time (randomly, just to test it again) and it worked. I ran it a few seconds later, and it started returning [] again. I also tried running with filter=completed, without any branch at all (against both) and received no results.

I tried it with the filter=failed and that actually returns results (but I need artifacts from successful/completed builds so that isn’t useful)

I confirmed this wasn’t an issue with my account; it appears the CircleCI API v1 artifacts endpoint clears the artifacts out as soon as the job is re-triggered. The filter=completed flag won’t work for an active branch (until it’s finished running and isn’t stomped on again)

Since we have multiple developers running builds on at any given time, the artifacts endpoint is useless for my use case.

As mentioned above; graphql api is still unstable/in-beta and v2 isn’t really clear on how to traverse down to get a job ID for the latest completed run for a branch but hopefully the API will support this at some point (you can’t currently list all jobs + filter w/ v2)

Google Chrome 2024-12-24 09.56.47

If you’re still wondering “how do i get CircleCI artifacts for a given branch or commit”, I think I found a stable enough workaround: You can use the v1 project API:

https://circleci.com/api/v1.1/project/gh/:username:/:repo:?filter=success

Once you retrieve that, you can loop thru each of those top level objects and find the matching:

  1. status (success or completed, not sure of difference?)
  2. workflowsjob_name
  3. branch or vcs_revision

Once you’ve pulled those top level objects out of that list, you can pull the workflowsjob_id values out, then use the v2 API linked above (or if your downloads aren’t really complex you can download the artifact directly from output.circle-artifacts.com)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.