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

3

9

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.

Colquitt answered 5/3, 2020 at 11:17 Comment(0)
O
6

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

Oxidate answered 10/3, 2020 at 17:55 Comment(2)
The question asks about workflow snippets/files located in a different repository. This answer reuses actions that live in different repositories, but not workflow snippets.Pylle
Can you do something like this: 1) define all common actions in a central repository 2) define that ./.github/actions as a gitsubmodule that is fetched before next actions are invoked 3) in next steps use the actions from submodule?Twill
P
3

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.

Pedate answered 2/5, 2020 at 19:18 Comment(2)
This attempts to answer the question more directly, whereas the most upvoted answer reuses "steps", not "workflows.Pylle
This works. Only issue is that the workflow run shows up in the central repo. So if you have multiple repos having a common workflow then there will be a workflow run in the multiple repos as also multiple runs in the common repo.Sixfold
P
3

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:

Pylle answered 3/2, 2021 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.