Reuse jobs in a workflow with parameters

I’m migrating a pipeline from Circle CI to Github Actions and am finding it a bit weird that I can only run jobs once instead of creating a job, then calling it from the workflow section, making it possible to call a job multiple times without duplicating the commands/scripts in that job.

My pipeline pushes out code to three environments, then runs a lighthouse scan for each of them. In circle ci I have 1 job to push the code to my envs and 1 job to run lighthouse. Then from my workflow section, I just call the jobs 3 times, passing the env as a parameter. Am I missing something or is there no way to do this in github actions? Do I just have to write out my commands 3 times in each job?

What’s the best option depends on how different and how big your jobs are. In principle you have three options to reuse workflow code:

  • Write one job with a matrix strategy, putting the parameters into the matrix. This keeps all code in one workflow file.
  • Create a composite action and call it with the appropriate parameters for each environment.
  • Create a reusable workflow and call it with the appropriate parameters for each environment.
1 Like