Skip to content

Using Data from Triggering Workflow

olsido edited this page Aug 5, 2024 · 1 revision

Use Case

Using data from the triggering workflow allows you to pass information between workflows, such as artifacts or environment variables. This is useful for workflows that need to share data or results.

Implementation

Unfortunately, we cannot pass the result between workflows using workflow_run as we can with workflow_call. The only way to pass a value from one workflow to another is for the first workflow to upload a file with that value, and for the second workflow to download that file and read the value.

Additionally, workflows by default do not share artifacts. For the second workflow to read the file uploaded by the first workflow, it needs to provide a token. Simply provide the secret called GITHUB_TOKEN, which is automatically generated by GitHub. You don’t need to do anything to create it.

First Workflow (.github/workflows/using-data-from-triggering-workflow–first.yml)

name: Using Data from Triggering Workflow - First  # Name of the workflow

on:
  workflow_dispatch:  # This workflow is triggered manually

jobs:
  generate-message:  # Job name
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Set message  # Step to create a message
        run: echo "Hello from the first workflow" > message.txt  # Create a file named message.txt with the content "Hello from the first workflow"
      - name: Upload message as artifact  # Step to upload the message as an artifact
        uses: actions/upload-artifact@v4  # Use the upload-artifact action
        with:
          name: message  # Name of the artifact
          path: message.txt  # Path to the file to be uploaded

View on Gist       View in this repo

Second Workflow (.github/workflows/using-data-from-triggering-workflow–second.yml)

name: Using Data from Triggering Workflow - Second  # Name of the workflow

on:
  workflow_run:  # Trigger this workflow when another workflow completes
    workflows: ["Using Data from Triggering Workflow - First"]  # Name of the triggering workflow
    types:
      - completed  # Trigger when the workflow is completed

jobs:
  use-message:  # Job name
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Download artifact  # Step to download the artifact
        uses: actions/download-artifact@v4  # Use the download-artifact action
        with:
          name: message  # Name of the artifact to download
          github-token: ${{ secrets.GITHUB_TOKEN }}  # GitHub token for authentication
          run-id: ${{ github.event.workflow_run.id }}  # ID of the triggering workflow run
          path: ./  # Path to download the artifact to
      - name: Read and echo the message  # Step to read and print the message
        run: cat message.txt  # Display the content of message.txt

View on Gist       View in this repo

This setup demonstrates how to pass data between workflows using artifacts. The first workflow generates a message and uploads it as an artifact. The second workflow downloads the artifact and uses the data, allowing workflows to share information and results.

Clone this wiki locally