Trigger GitHub Actions Workflow from Another Workflow with Inputs, Outputs, and Secrets
Introduction
GitHub Actions is a powerful feature that lets developers automate, customize, and execute their software development workflows right in their repositories. One of the common practices is to trigger one workflow from another, which allows for better segmentation and control over complex workflow processes. This blog post will guide you on triggering a GitHub Actions workflow from another, passing inputs, outputs, and secrets between them.
GitHub Actions Overview
GitHub Actions provides an automation platform where workflows, defined in YAML files, respond to various repository events. Workflows can be simple, performing tasks like code linting, or more complex, handling build, test, and deployment processes.
Triggering a Workflow from Another
The key to triggering one workflow from another lies in the workflow_run
event. Let’s break down how to set up one workflow to trigger another, passing inputs, outputs, and secrets.
Step 1: Create Workflow YAML Files
Start by creating two new YAML files within the .github/workflows
directory in your repository. Let’s name them first-workflow.yml
and second-workflow.yml
.
Step 2: Define the First Workflow
In first-workflow.yml
, define a simple job that sets an output. This output will later be used by the second workflow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
name: First Workflow on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Set output id: step1 run: | echo "This is the first workflow" echo "::set-output name=timestamp::$(date +%s)" outputs: timestamp: ${{ steps.step1.outputs.timestamp }} |
Step 3: Define the Second Workflow
In second-workflow.yml
, define a job that is triggered by the workflow_run
event. It then prints the output from the first workflow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
name: Second Workflow on: workflow_run: workflows: ["First Workflow"] types: - completed jobs: build: runs-on: ubuntu-latest steps: - name: Get output from first workflow id: workflow_run uses: actions/github-script@v5 with: script: | const runId = context.payload.workflow_run.id const artifactClient = github.getArtifactOctokit() const artifacts = await artifactClient.rest.actions.listWorkflowRunArtifacts({ owner: context.repo.owner, repo: context.repo.repo, run_id: runId, }) return artifacts.data.artifacts[0].archive_download_url result-orientation: verbose token: ${{ secrets.GITHUB_TOKEN }} - name: Download artifact run: | wget "${{ steps.workflow_run.outputs.result }}" |
Here, the second workflow is triggered when the first workflow is completed. It then uses the github-script
action to fetch the output from the first workflow. Note that the GITHUB_TOKEN
secret is used for authentication.
Conclusion
GitHub Actions offers great flexibility and control in CI/CD pipeline setup. Triggering one workflow from another provides better organization and segmentation of complex workflows, and the ability to pass inputs, outputs, and secrets between workflows allows for secure and effective pipeline management.
While this guide outlines a basic example, real-world workflows may involve more complex tasks like building applications, running tests, and deploying code to various environments. GitHub Actions offers endless possibilities to create efficient and flexible pipelines, customized to your project’s needs.