How to Pass Data Between Jobs in a GitHub Workflow?

How to Pass Data Between Jobs in a GitHub Workflow

GitHub Actions empowers developers to automate their workflows directly from the GitHub repository. The feature allows us to create custom software development life cycle (SDLC) workflows that we can tailor to our needs. However, one aspect of GitHub Actions that could be puzzling, especially for beginners, is how to pass data between jobs. In this blog, we’ll focus on that and provide a step-by-step guide.

Conceptual Overview

Before diving into the code, it’s crucial to understand some of the GitHub Actions’ concepts that we’ll use in this tutorial.

GitHub Actions

GitHub Actions help you automate your software development workflows in the same place you store and collaborate on your code. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.

Workflow Files

Workflow files use YAML syntax, and they must have either a .yml or .yaml file extension. You must store workflow files in the .github/workflows directory of your repository.

Artifacts

In GitHub Actions, an artifact is a file or a set of files generated during a workflow run. We can use artifacts to share data between jobs and store data once a workflow is completed.

Step-by-Step Guide

Now, let’s get into the practical part of passing data between jobs in a GitHub Workflow using artifacts.

Step 1: Setting Up the Workflow

First, we need to set up a new workflow. In your GitHub repository, create a new file at .github/workflows/data-passing.yml. The workflow triggers whenever a push is made to the master branch.

Step 2: Creating the First Job

Let’s define the first job that creates a data file to pass to the next job. We’ll call this job create-file.

Step 3: Uploading Artifact

After creating the file, we need to upload it as an artifact using the actions/upload-artifact@v2 action. This action will allow us to access the file in subsequent jobs.

Step 4: Creating the Second Job

Next, we need to define a second job that will download and use the file created in the first job. We’ll call this job use-file.

Step 5: Using the Artifact

Finally, in the second job, let’s use the downloaded artifact. For this example, we’ll simply display the contents of the file.

Step 6: Final Outcome

The final data-passing.yml workflow file will look like the following:

After you push this file to your master branch, GitHub Actions will start running this workflow. You can see the results of each run in the “Actions” tab of your repository.

Conclusion

With GitHub Actions and artifacts, you can create complex CI/CD workflows and pass data between different jobs efficiently. Understanding this mechanism enables more flexible and sophisticated workflows, improving your automation processes.