Job Artifacts and Outputs - CircleCI Tutorial

Introduction

Job artifacts and outputs are essential features of CircleCI that allow you to capture and share data generated during your CI/CD workflows. Artifacts are files generated by jobs, such as build artifacts, logs, or test reports, while outputs are values or data that jobs produce. This tutorial will guide you through the steps of working with job artifacts and outputs in CircleCI.

Example

Let's consider an example where we build a web application and want to capture the compiled assets as an artifact:

version: 2.1
jobs:
  build:
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - run: npm install
      - run: npm build
      - store_artifacts:
        path: dist/

Job Artifacts and Outputs

To work with job artifacts and outputs in CircleCI, follow these steps:

1. Generate job artifacts

Within your job steps, generate the artifacts you want to capture. Artifacts can be generated by running build scripts, compiling code, or any other process that produces files. Use the store_artifacts key to specify the paths or patterns for the artifacts. For example:

steps:
    - run: npm build
    - store_artifacts:
      path: dist/

2. Define job outputs

If your job needs to produce specific data or values that can be reused in subsequent steps or workflows, define job outputs. Use the outputs key to define the desired outputs. For example:

steps:
    - run: echo "Hello, CircleCI!" >> output.txt
    - persist_to_workspace:
      root: ./
      paths: output.txt
    - store_test_results:
      path: test-results/

3. Access artifacts and outputs

To access artifacts and outputs, you can use the CircleCI API or refer to them in subsequent steps or workflows. Artifacts can be downloaded using the CircleCI web interface or programmatically via the API. Job outputs can be referenced using the ${{ steps..outputs. }} syntax. For example:

steps:
    - run: echo "The output is ${{ steps.build.outputs.output_name }}"

Common Mistakes

  • Not specifying the correct paths or patterns for artifacts
  • Forgetting to define job outputs or referencing them incorrectly
  • Not persisting artifacts or outputs to a workspace

Frequently Asked Questions (FAQs)

  1. Can I access artifacts and outputs from other jobs or workflows?

    Artifacts can be accessed from other jobs or workflows using the CircleCI API or by using workspace sharing mechanisms. Outputs, on the other hand, can only be accessed within the same job.

  2. How long are artifacts retained in CircleCI?

    Artifacts are retained for 30 days by default. However, you can configure the retention policy in your CircleCI organization settings.

  3. Can I share artifacts publicly?

    Yes, you can configure artifacts to be publicly accessible by enabling the public URL feature in your CircleCI project settings.

Summary

In this tutorial, you learned how to work with job artifacts and outputs in CircleCI. You now know how to generate artifacts, define job outputs, and access them in subsequent steps or workflows. Artifacts and outputs are powerful features that allow you to capture and share valuable data and results from your CI/CD processes. Consult the CircleCI documentation for further details and advanced usage options.