'How to have the .github workflows sharable or reusable

We have a repo that contains a bunch of workflows that we want to share amongst all of our dev repositories.

github-common-workflows repository:
  workflows/build.yml
  workflows/test.yml

Is there a way to import the repo above as a submodule or subtree within our dev repos, e.g.:

dev-repo repository:
  .github/<point to the github-common-workflows repo>

BECAUSE - when using submodules, github actions will not recognize any workflows at all Hence no github workflow will be triggered (e.g. upon push)

Any ideas how to achieve a single place that contains all the workflows and shared amongst all repos?



Solution 1:[1]

A possible solution for private repos is to upload the shareable repo as an artifact, e.g. a compressed archive and host it somewhere accessible. When the caller workflow kicks in, simply download the archive and extract it into the current context.

E.g.:

Assuming $archive-url =https://example.org/shareable-workflow.tar: workflow.yml

    jobs:
      - name: Checkout context repo
         run: actions/checkout@v2

      - name: Download the shareable workflow
         run: curl $archive-url | tar xzf -  

      - uses: ./workflow.yml

Solution 2:[2]

Having .github/workflows as a submodule won't work since github can't see the workflows defined in the target repo.

Solution:

Workflows can be defined in a common separate workflows repo and can be reused by multiple repositories, using the caller/called reusable workflow syntax (workflow_call) official docs can be found here:

https://docs.github.com/en/actions/using-workflows/reusing-workflows

Bear in mind, as for Feb.2022 Reusable workflows are not available for github enterprise (unfortunately)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idriz Å unja
Solution 2