Buildkite-agent artifact download --pipeline --branch --last-passed

For hand deployments it could be useful feature.

buildkite-agent artifact download --pipeline myPipName --branch myBranchName --last-passed arfifactPath .

to download the given artifact from the last passed build for the given pipeline and branch name.

It will be indeed really useful in case we want to download a test report.

Hey!

Thanks for the feedback! Not sure if it’s something that we can work on but it sound very interesting. Would you mind creating this feature request in the agent repo?

Thanks!

I will think about it.

Meanwhile, I created a script using the GraphQL API to download an artifact on the latest success build of the main branch:

#!/bin/bash

response=$(curl -s https://graphql.buildkite.com/v1 \
  -H "Authorization: Bearer $BUILDKITE_API_ACCESS_TOKEN" \
  -d '{"query":"{pipeline(slug:\"myorg/mypipeline\"){builds(branch:\"main\",state:PASSED,first:1){edges{node{jobs(first:1,step:{key: \"knapsack\"}){edges{node{ ... on JobTypeCommand{artifacts{edges{node{downloadURL}}}}}}}}}}}}"}')

artifact_url=$(grep -E -o "https://[^ \"]+" <<< "$response")

curl -s $artifact_url > knapsack_rspec_report.json

I will create a private plugin to wrap it.

I would like to bring this idea back to the table. We are currently looking for this to download some snapshots on main and the amount of extra code needed to achieve this feels suboptimal.

Hi @Valkum I totally understand you and I wanted to check if you explored using the bk cli as an alternative as I believe this should be a bit easier
I tested with this

BUILD_NUMBER=$(bk build list \
  --pipeline YOUR_ORG/YOUR_PIPELINE \
  --branch YOUR_BRANCH \
  --state passed \
  --limit 1 \
  --output json | python3 -c "import sys,json; print(json.load(sys.stdin)[0]['number'])")

bk artifacts download --build "$BUILD_NUMBER" --pipeline YOUR_ORG/YOUR_PIPELINE

It finds the last passed build for the given pipeline and branch, then downloads the artifacts into a folder called artifacts-build-{number} in your current directory. I tested this and it picks up the latest passed build each time it runs.

You’ll need the bk CLI installed(GitHub - buildkite/cli: A command line interface for Buildkite. · GitHub) and your API token will need read_builds and read_artifacts scopes.

This also does not look like an improvement over talking to the API endpoints using curl to get the artifact URL from builds. It still requires a subshell to extract a build number.

The docs of bk artifacts download already say

```

BuildNumber string `help:"Build number containing the artifact. If omitted, the most recent build on the current branch will be used." short:"b" name:"build"`

So, if that command could get a branch argument, this would be enough to power this feature request.

The last wish would be a way to avoid the requirement to hand in an API token. The job token should be enough. Though this comes with security implications, I believe.

Hi @Valkum thanks a lot for clarifying there. Please can you send this request via support@buildkite.com for us to review further. Thanks