V. F.
V. F.

Reputation: 91

How to use snippets in Github action workflow file to avoid duplicates?

Problem: We use github actions workflow for CI and we have many github repositories. I need to be able change everything repeatable for every repository at once.

Is it possible to use in github action workflow yml file some snippet that located mb in different repository.

Upvotes: 9

Views: 2333

Answers (3)

Cole Bittel
Cole Bittel

Reputation: 2884

There is currently (Feb. 3, 2021) no supported method for reusing workflows or snippets from a centralized repository. There are hacks, as Michael Parker has cleverly demonstrated, but these come with significant downsides (eg. observability, opacity, etc.).

I've written this blog post that describes the problem you have in more detail, along with an open-source solution.

––

Similar topics:

Bringing this issue to GH's attention:

Upvotes: 3

Michael Parker
Michael Parker

Reputation: 8410

One way of doing this is having a central CICD / GitHub actions repository with shared workflows which are triggered on repository_dispatch events.

on:
  repository_dispatch:
    types: 
      - your_event
jobs:
  job1:
    name: Do something
    runs-on: ubuntu-latest
    env:
      SOURCE_BRANCH: ${{ github.event.client_payload.source_branch }}
      SOURCE_REPO: ${{ github.event.client_payload.source_repo }}

# do all your stuff

Then in each github repo you write a small workflow file which outlines the triggers for the local repo, pushing to master / opening a PR etc. That action simply dispatches a repository_dispatch event to your central CICD repo with the repo and branchname it came from.

name: Trigger external CICD
on:
  push:
    branches:
      - master
jobs:
  trigger_cicd:
    name: Trigger external CICD
    runs-on: ubuntu-latest
    steps:
      - name: Send repository_dispatch event
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.CICD_GITHUB_TOKEN }}
          repository: yourorg/centralcicdrepo
          event-type: ${{ env.EVENT_TYPE }}
          client-payload: '{"source_branch": "${{ github.ref }}", "source_repo": "${{ github.repository }}" }'

One gotcha is that you need an access token to talk between repos, in the above example it's added as a secret called CICD_GITHUB_TOKEN. The easiest is to just use your own account but this will label all your central CICD runs as 'triggered by you'. You can also create a bot account or you can have each developer add their access tokens as secrets then map the right author to the right access token.

Upvotes: 3

dabo248
dabo248

Reputation: 3437

You can include other public and local actions in your workflow, which lets you reuse common steps. Using versioned actions with {owner}/{repo}@{ref}:

steps:    
  - uses: actions/setup-node@74bc508 # Reference a specific commit
  - uses: actions/setup-node@v1      # Reference the major version of a release   
  - uses: actions/[email protected]    # Reference a minor version of a release  
  - uses: actions/setup-node@master  # Reference a branch

..or local actions with ./path/to/dir:

jobs:
  my_first_job:
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: Use local my-action
        uses: ./.github/actions/my-action

https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses

Upvotes: 6

Related Questions