GitHub Actions: You Can Build Reusable Workflows!
Tags: actionshackathon21, github, opensource, devops
Published: 2021-11-17 03:46:12

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. 

<img width="100%" style="width:100%" src="https://media.giphy.com/media/hZj44bR9FVI3K/giphy.gif">


## 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:

```yaml
on: 
  workflow_call:
```

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

```yaml
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
```
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](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#allowing-access-to-components-in-an-internal-repository) 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:

- [CoderDave uses reusable workflows to build and publish a docker image](https://youtu.be/lRypYtmbKMs)
- [GitHub's official documentation about reusable workflows (with examples)](https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#creating-a-reusable-workflow)
- [Colin Dembovsky's detailed review of reusable workflows](https://colinsalmcorner.com/musings-on-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](https://github.com/marketplace?type=actions). GitHub Marketplace currently has over [10,000 actions](https://github.blog/2021-10-21-github-marketplace-welcomes-its-10000th-action/#:~:text=Celebrating%20the%20power%20of%2010%2C000%20actions&text=Linting%2C%20testing%2C%20and%20validating%20code,Connecting%20to%20third%2Dparty%20services). 

Read [here](https://dev.to/devteam/join-us-for-the-2021-github-actions-hackathon-on-dev-4hn4) 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](https://dev.to/devteam/github-actions-hackathon-2021-help-thread-1lk0).

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