7

I have two workflows. One deploys on push to master to a test environment. The other deploys on release to prod environment. They are 90% identical, code copy&paste.

Is there a concept such as extracting part of the duplicate logic and putting it into its own file/partial/fragment?

phifa
  • 856
  • 7
  • 11
  • 1
    Does this answer your question? [Does Github Actions have templates](https://stackoverflow.com/questions/59230841/does-github-actions-have-templates) – smac89 May 09 '20 at 21:52
  • Unfortunately not. The best one can do now is to create an action, which offers little to no benefit tbh – smac89 May 09 '20 at 21:59

2 Answers2

4

Reusing workflows in GitHub Actions is available in public beta now:

To summarize, the workflow that has to be reused needs workflow_call trigger. The caller workflows can then refer it directly within a job by using uses keyword as shown in the following example which is quoted directly from the GitHub page.

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@172239021f7ba04fe7327647b213799853a9eb89
  call-workflow-2:
    uses: octo-org/another-repo/.github/workflows/workflow-2.yml@v1

It does have some limitations though.

kamimanzoor
  • 101
  • 1
  • 4
1

At this time, reusing one workflow YAML in another, is not possible.

This is (was) discussed in several places:

As for your particular problem, not all hope is lost.

I have been using Kojo - a command line utility that generates files from templates - for exactly this purpose. I have one template file and I generate two workflows - one for stage, one for production. Perhaps it will be of use to you until a more formal solution from GitHub is available.

(Disclaimer: I am Kojo's author).

DannyB
  • 12,810
  • 5
  • 55
  • 65