Bootstrap FreeKB - GitHub Actions - Sharing Jobs using workflow_call and a private repository (reusable workflow)
GitHub Actions - Sharing Jobs using workflow_call and a private repository (reusable workflow)

Updated:   |  GitHub Actions articles

Let's say you have similar, if not identical tasks in different GitHub Actions Workflows. For example, one of the most common task is actions/checkout. Perhaps you want to ensure all of your workflows are using v4 of actions/checkout. 

name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  push:
    branches:
      - main
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: Checking out the repository code
        uses: actions/checkout@v4

 

Let's create a private repository that will contain your common tasks. I created a private repository named "Actions". I know, I'm super creative!

 

Let's update the private repository so that it can be used by our other repositories.

  1. Select your private repository
  2. Select Settings
  3. In the left panel, expand Actions and select General
  4. Under Access, select Accessible from repositories owned by the user 'username'
  5. Select Save

 

Let's create a workflow, perhaps with the following.

name: Reusable Workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_call:
  workflow_dispatch:
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: Checking out the repository code
        uses: actions/checkout@v4

 

Now, let's update a workflow in one of our other repositories to call this workflow. In this example, @main means that .github/workflows/main.yml is in the "main" branch of the JohnDoe/Actions repository.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  demo:
    uses: JohnDoe/Actions/.github/workflows/main.yml@main

 

Instead of using @main, you can use the SHA.

 

In this example, d6e0428bb6c2102c47f495f4cc81f62486a168f1 is the SHA.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  demo:
    uses: JohnDoe/Actions/.github/workflows/main.yml@d6e0428bb6c2102c47f495f4cc81f62486a168f1

 

And if all goes well, the GitHub Action Workflow should return something like this.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e34140 in the box below so that we can be sure you are a human.