Canarys | IT Services

Blogs

Reusable Workflows

Share

Reusable Workflows:

A reusable workflow is a pre-defined GitHub Actions workflow that can be called from another workflow. Reusable workflows make it easy to treat a workflow like an Action. It can be referenced and executed from other workflows in the caller’s context. This allows teams to share common workflow patterns, to centralize best practices, and to centralize the development of processes.

Reusable workflows listen for a special event, workflow_call. The workflow must be in the .github/workflows folder in order to be triggered. The workflow can also receive inputs.

Benefits of using reusable workflows:

  • Suppose if you have one workflow and want to build the same set of workflows in any other workflows in that case you can use one Reusable workflow, so you don’t need to copy and paste your workflows from one repository to another.
  • By using the tag uses and provide the path of reusable workflow, you can have access to call the reusable workflow from another GitHub action workflows.
  • Reusing workflows mainly avoids duplication, and you can build up a library of reusable workflows that can be maintained in your organization also.

Creating Reusable workflow from GitHub Actions:

  • Usually, reusable workflows are same as other GitHub Actions workflow, but you must use one special event workflow_call to trigger and then you can use as reusable workflow.

` on: `

     workflow_call: `

  • You can then use this workflow in another workflows by adding in the keyword uses:

` Uses:

USER_OR_ORG_NAME/REPO_NAME/.github/workflows/REUSABLE_WORKFLOW_FILE.yml@TAG_OR_BRANCH `

  • To get access for Reusable workflows, inside your organization. Just go to your settings select Actions and Allow all actions and reusable workflows in your organization.

reusable

Example of Reusable workflow using in another workflow:

The below workflow is the reusable workflow with name – Workflow-A.yml

name: Reusable workflow

on:

  workflow_call:

    # Map the workflow outputs to job outputs

    outputs:

      firstword:

        description: “The first output string”

        value: ${{ jobs.example_job.outputs.output1 }}

      secondword:

        description: “The second output string”

        value: ${{ jobs.example_job.outputs.output2 }}

jobs:

 example_job:

    name: Generate output

    runs-on: ubuntu-latest

    # Map the job outputs to step outputs

    outputs:

      output1: ${{ steps.step1.outputs.firstword }}

      output2: ${{ steps.step2.outputs.secondword }}

    steps:

      – id: step1

        run: echo “::set-output name=firstword::hello”

      – id: step2

        run: echo “::set-output name=secondword::world”

The below is the workflow calling reusable workflow with name – workflow-B.yml

name: Call a reusable workflow

on:

  workflow_dispatch:

jobs:

  job1:

    uses: CanarysPlayground/Reusable-workflow/.github/workflows/workflow-A.yml@master

  job2:

    runs-on: ubuntu-latest

    needs: job1

    steps:

      – run: echo ${{ needs.job1.outputs.firstword }} ${{ needs.job1.outputs.secondword }}

Limitations with reusable workflows:

  • Suppose If you have a reusable workflow in a private repository then other workflows in the same private repository only can be able to use it.
  • You can only have a reusable workflow call another reusable workflow, but you can’t have it reference more than one.
  • Reusable workflow is stored in a public repository, then your organization allows you to use reusable workflows only in the public repository.
  • Reusable workflows can’t call other reusable workflows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.