Re-using your Github Action Workflow

Did you ever get stuck where you need to copy-paste your Github action workflow across files and couldn't find a way to reuse them,
Here I am with an article that will give you a new idea on the beta feature from Github to reuse your workflows across files
What are workflows?
Each workflow contains a set of jobs can be simplified as testing, deploy or check lint, further a jobs are divided into steps which are more simplified versions of process inside
Create reusable workflow
To create a reusable workflow we need to attach a different dispatcher then regular onPush or onPR, you need to include onWorkflowCall which will trigger the workflow inside a workflow
Limitation and options
Before creating a workflow, try understand the things you should keep in mind such as
A reusable workflow cannot be reused inside another reusable workflow
You can not inherit the variable from where you are calling, you need to explicit pass the variables
A Workflow can contains other triggers than onWorkflowCall
You can only use reusable workflow if they are public, inside same repo or same organization
Get Started
To begin with, you can create a file inside .github/workflows/sample-test.yml or similar
on:
workflow_call:
inputs:
image_name:
required: true
type: string
tag:
type: string
secrets:
registry_username:
required: true
registry_password:
required: truejobs:
build:
// your steps...Here you can create a workflow by using the key workflow_call and pass the variable using input and encrypted input as secrets
Now whatever you write inside the jobs will get run if you call this workflow
How to call?
Moving ahead, there might be a question that how to call this workflow inside another
To use a workflow you should follow the same process as regular workflow file but main area would be, inside jobs
You can create a separate
jobs:..other jobs docker:
uses: n3wt0n/ReusableWorkflow/.github/workflows/buildAndPublishDockerImage.yml@main
with:
image_name: my-awesome-app
tag: $GITHUB_RUN_NUMBER
secrets:
registry_username: ${{secrets.REGISTRY_USERNAME}}
registry_password: ${{secrets.REGISTRY_PASSWORD}}That’s all you need to do. After doing this you can run and reuse any number of workflows in your project
If you like my content do consider subscribing to my substack account here
Thanks!
Re-using your Github Action Workflow was originally published in Nerd For Tech on Medium, where people are continuing the conversation by highlighting and responding to this story.


