CircleCI: How to Download All Artifacts After a Successful Build

Marco Sanguineti
Towards Dev
Published in
6 min readMay 19, 2023

--

A Step-by-Step Guide to Efficiently Retrieve Build Artifacts from CircleCI for Continuous Integration and Delivery

Photo by Sigmund on Unsplash

Introduction

CircleCI is a popular and powerful continuous integration and delivery platform that has been designed to help software development teams automate their build, test, and deployment processes. As a cloud-based platform, CircleCI provides developers with a flexible and scalable solution for managing their workflows and pipelines, enabling them to build and deliver high-quality software applications quickly and efficiently.

With CircleCI, developers can easily integrate code changes into their main branch, run automated tests to ensure that everything is working correctly, and deploy their code to production with just a few clicks. The platform supports a wide range of programming languages and frameworks, making it a versatile tool for teams of all sizes and backgrounds.

One of the key features of CircleCI is its ability to generate build artifacts, which are the output files created during the build process. These artifacts can include binary files, test reports, code coverage reports, and more. In this article, we will explore how to download all the artifacts from a successful build in CircleCI, helping you to take full advantage of this powerful platform and streamline your development workflow.

In this article, we will explore how to download all the artifacts from a successful build in CircleCI, using a Python script that leverages the CircleCI API. The CircleCI API allows you to programmatically interact with your CircleCI projects, enabling you to automate tasks and retrieve information about your builds, jobs, and artifacts.

Scripts

Our Python script will use the CircleCI API to retrieve the list of artifacts for a specific build and download them to your local machine. This can be useful if you want to analyze the output files generated during the build process or share them with other team members.

To use the script, you will need to provide your CircleCI API token, the name of your CircleCI project, your username, and the vcs type (i.e. GitHub). The script will then authenticate with the CircleCI API using your token, retrieve the list of artifacts for the specified build, and download them to a local directory.

By providing this Python script, we aim to help developers streamline their development workflow and make retrieving and analysing the artifacts generated during a CircleCI build easier. With the power of the CircleCI API and the simplicity of Python, you can automate repetitive tasks and focus on what really matters — building and delivering high-quality software applications.

The code above is a Python script that downloads all artifacts from the latest build of a CircleCI project using the CircleCI API. Let’s take a closer look at how this code works and what it does.

The script starts by importing the os and json modules, which are used for accessing the file system and parsing JSON data, respectively.

Next, the script defines several variables that are used throughout the script. These include the name of the JSON file to be generated (__json_name), the username and project name for the CircleCI project (__username and __project_name), the CircleCI token for API authentication (__circleci_token), and the version control system (VCS) type (__vcs_type).

The script then defines a conditional statement that checks whether the script is being run as the main program. This is a common practice in Python to ensure that code in a module is only executed when the module itself is run, and not when it is imported by another module.

Inside the main block, the script defines the base_command and target_folder variables. base_command is a string that contains the curl command with the CircleCI API token added as a header. target_folder is a string that specifies the directory's name where the artifacts will be downloaded.

The script then checks whether the target folder exists and deletes it if it does. This is done to ensure that the folder is empty before downloading artifacts.

The script then creates the target folder using the os.mkdir method and changes the current working directory to the target folder using the os.chdir method.

The script then defines the cmd variable, which is a curl command that retrieves the list of artifacts for the latest build of the specified CircleCI project using the CircleCI API. The command includes the CircleCI API token, the VCS type, the username, and the project name.

The script then uses the os.system method to execute the cmd command and save the output to a JSON file with the name specified in the __json_name variable.

Next, the script opens the JSON file using the with open statement and loads the contents of the file into the files variable using the json.load method.

The script then uses a for loop to iterate over each artifact in the files list and download it to the target folder. For each artifact, the script extracts the file extension and saves the artifact to a file with the same name and extension in the target folder using the curl command and the os.system method.

Overall, this script provides a simple and effective way to download all artifacts from the latest build of a CircleCI project using Python. By leveraging the CircleCI API and the os and json modules in Python, developers can automate the process of retrieving artifacts and make it easier to access and analyze the output files generated during the build process.

This script can also be easily customized to fit the specific needs of a development team, such as filtering artifacts by type or name or downloading artifacts from a specific build number. By providing a Python script for downloading artifacts from a CircleCI project, developers can streamline their workflow and improve their development process.

Greetings

In conclusion, downloading all artifacts after a build in CircleCI is an important part of the development process. By using a Python script that leverages the CircleCI API, developers can automate the process of retrieving and downloading artifacts, making it easier to analyze the output files generated during the build process.

In this article, we’ve shown you how to use a Python script to download all artifacts from a CircleCI build and store them locally. We hope this article has been helpful in streamlining your development workflow and improving your development process.

If you have any questions or comments about this article, please feel free to leave them below. Thank you for reading!

Join Medium Membership

If you enjoyed this article and want to keep learning more about this topic, I invite you to join Medium membership at this link.

By becoming a member, you’ll have access to a wider variety of high-quality content, and exclusive access to member-only stories, and you’ll be supporting independent writers and creators like myself. Plus, as a member, you’ll be able to highlight your favourite passages, save stories for later, and get personalized reading recommendations. Sign up today and let’s continue exploring this topic and others together.

Thank you for your support! Until next,

Marco

--

--

Graduated in Mechanical Engineering, I work in the world of AI, Deep Learning and Software Development. Passionate about Technology, Videogames and AI.