DEV Community

Cover image for GitHub Actions: You Can Build Reusable Workflows!
Rizèl Scarlett for GitHub

Posted on

GitHub Actions: You Can Build Reusable Workflows!

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!

Top comments (4)

Collapse
 
epassaro profile image
epassaro

Long time waiting for something similar to the Azure Pipelines Templates. Thanks.

Collapse
 
epassaro profile image
epassaro

Unfortunately using this on step scope is really important. For example, we can't re-use a workflow to deploy our working environment.

Collapse
 
iamksam profile image
ɯɐSʞɯɐᴉ

Happy to see this! Been using it for a few weeks now. Aside from a major upgrade to issues/projects there's only a few features left that could bring GitHub up to Azure DevOps' level.
Currently it looks like you can't use or pass a protected environment secret in the caller to the called workflow through that secret param yet.
Also, for this to be useful at Enterprise scale the org secrets need to be mappable to specific yaml files in other repos like how Azure DevOps has in a policy for Service Connections.
Secrets with high level access to shared endpoints are too easy for a malicious dev to echo to the log (via char injection or storing in a file) to be trusted.

Collapse
 
pachverb profile image
new/bird

do you know how to release by github actions