Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 471
V. F. Avatar asked Mar 05 '20 11:03

V. F.


People also ask

What is a reusable workflow?

A reusable workflow is a pre-defined GitHub Actions workflow that can be called from another workflow. Reusable workflows make it easy to treat a workflow like an Action. It can be referenced and executed from other workflows in the caller's context.

Can a GitHub workflow trigger another workflow?

If you specify multiple events, only one of those events needs to occur to trigger your workflow. If multiple triggering events for your workflow occur at the same time, multiple workflow runs will be triggered.


3 Answers

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

like image 154
dabo248 Avatar answered Oct 12 '22 09:10

dabo248


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.

like image 32
Michael Parker Avatar answered Oct 12 '22 10:10

Michael Parker


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:

  • DRYing GH Actions workflows
  • External workflow configuration

Bringing this issue to GH's attention:

  • Raise this issue with GH
  • GH Roadmap item
like image 30
Cole Bittel Avatar answered Oct 12 '22 08:10

Cole Bittel