Example bash script to get job artifacts from pipeline

Hi There!

Recently the Support team created a script for easily getting artifacts from a pipeline. This was created since the artifacts are associated with the jobs in the pipeline, it requires finding the jobs and returning the results.

This script will likely need to be modified to be used for your purposes, but should be a good starting point!

A few notes/caveats:

  1. It is not setup for pagination, so if you have a lot of jobs associated with your workflows you may need to update to account for that
  2. It does create a file (art_test.txt) in the current working directory, but deletes it at the end
  3. It requires jq to be installed
  4. Within the Variables section, you will need to enter your API token, organization and project
#!/bin/bash -e
clear
# This works under the assumption that the number of jobs is less than pagination for results
# run with: source get_artifacts.sh -- it will prompt you for a pipeline number, i.e. 91
​
### Variables ###
​
# Set to personal API token
CIRCLE_API_TOKEN=
# set to github or bitbucket
VCS=github
# set to organization
ORG=
# set to project
PROJECT=
​
### Commands ###
​
# Check for Input
read -p 'Input Pipleline Number: ' PIPELINE_NUMBER
​
if [ -z "$PIPELINE_NUMBER" ]
  then
    echo "No argument supplied, please enter pipleline number."
    exit 1
fi    
​
PIPELINE_ENDPOINT="https://circleci.com/api/v2/project/$VCS/$ORG/$PROJECT/pipeline/$PIPELINE_NUMBER?circle-token=$CIRCLE_API_TOKEN"
RESPONSE="$(curl -s $PIPELINE_ENDPOINT)"
PIPELINE_ID="$(echo $RESPONSE | jq -r '.id')"
​
# Get Workflow ID
​
WORKFLOW_ID=$(curl -s https://circleci.com/api/v2/pipeline/$PIPELINE_ID/workflow\?circle-token\=$CIRCLE_API_TOKEN | jq -r '.items[] | "\(.id) = \(.name),"')
​
# Show Workflow ID's and give option to select which to display Artifacts for which workflow.
# Workflow_name, workflow ID
echo
echo "******** Below are Workflow IDs *******"
echo $WORKFLOW_ID
echo
read -p 'Enter Workflow ID to proceed: ' WORKFLOW_ID_NO
​
# We need to filter out approval jobs
JOB_NUMBERS=$(curl -s https://circleci.com/api/v2/workflow/$WORKFLOW_ID_NO/job\?circle-token\=$CIRCLE_API_TOKEN | jq -r '.items[] | select (.type? == "build") | .job_number')
​
echo $JOB_NUMBERS | tr " " "\n" > art_test.txt
echo
echo "Searching for Artifacts .........."
echo
​
for x in `cat art_test.txt` ; do ART_ENDPOINT="https://circleci.com/api/v2/project/$VCS/$ORG/$PROJECT/$x/artifacts?circle-token=$CIRCLE_API_TOKEN" ARTS=$(curl -s $ART_ENDPOINT | jq -r '.items[] | .url'); echo; echo "*** Artifacts For Job Number $x ***" ; echo $ARTS ; echo; done
# cleanup
​
rm -rf art_test.txt
​
#### END ####
2 Likes

So recently I built this into the swissknife orb.

Check out the get job artifacts command. It can be used in conjunction with the get rerun job command.

Would love feedback on other features that can be added to make this more helpful

2 Likes

Thanks for including it @roopakv

1 Like