GitHub Actions: You Can Build Reusable Workflows!

Rizèl Scarlett - Nov 17 '21 - - Dev Community

I hope you're enjoying the Actions Hackathon 2021. I'm impressed by the participants' submissions leveraging already existing actions. Guess what? GitHub made it even easier for participants to use existing workflows and actions with a new feature called reusable workflows.

What are the benefits of reusable workflows?

Prior to the launch of this feature, if you wanted to reuse an existing workflow, you had to copy and paste the workflow into your new workflow. This can make workflows seem overly verbose. As software engineers, we want to follow best practices. One of my favorite best practices for writing clean code is the DRY principle, which is an acronym for Don't Repeat Yourself. Adopting reusable workflows also makes it easier to manage workflows. Now, with reusable workflows, you can write clean, maintainable Actions.

How do you create reusable workflows?

  • Terms to know
    • Caller workflow: a workflow that uses another workflow
    • Called workflow: the reusable workflow

You can make a workflow reusable by adding a workflow_call trigger to the called workflow. A workflow_call trigger looks like this:

on: 
  workflow_call:
Enter fullscreen mode Exit fullscreen mode

Inside of the workflow_call trigger, you can pass in the necessary inputs and secrets similar to the example below:

on:
  workflow_call:
    inputs:
      ring:
        description: 'Identifier for the target deployment ring'
        default: 'ring-0'
        required: false
        type: string
      environment:
        required: false
        type: string
    secrets:
      token:
        required: false
Enter fullscreen mode Exit fullscreen mode

Your caller workflow can call multiple workflows; however, a called workflow cannot call other reusable workflows.

How can one workflow gain access to a reusable workflow?

If a workflow meets one of the criteria below, it can gain access to another workflow:

  • The called workflow is stored in a public repository.
  • Both workflows are in the same repository.
  • The called workflow is stored in an internal repository, and the settings for that repository allow it to be accessed. Click here to learn how to configure those settings.

Resources

Below is a list of resources that may help you build and use your own reusable workflows:

Join the GitHub Actions Hackathon

From now until December 8th, GitHub is running a Hackathon. By participating, you have the chance to win gift cards and swag. The challenge is to create and submit a workflow leveraging already existing actions. GitHub Marketplace currently has over 10,000 actions.

Read here to learn more about participating in the hackathon.

And if you need any help regarding actions, I'll be available to answer questions in this thread.

Tell me about a use case where reusable workflows would benefit you!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .