How to share the Github Actions workflow in an organization privately
By: Nitin Singh
Tags: Github Actions

How to share the Github Actions workflow in an organization privately Nitin Singh Sep 3·3 min read

Working in micro-services architecture provides its own advantage of delivering services independently. In most cases, for backend APIs, services use the similar project structure and build strategy. Almost every CI tool provides a facility to have centralized way of building these common services. Like in Jenkins we can write groovy script with build steps which can build multiple services of same kind. On contrary, Github Actions provide workflows templating and importing mechanism of sharing any composite build steps.

There are different official ways by which you can share your workflow template within your organization.

1. Most popular is through publicly available “.github” repo in your github organization.

Sharing workflows with your organization — GitHub Docs

2. You can also create your own actions using Docker, or by Javascript or by Composite way. Whole documentation is available below.

About actions — GitHub Docs

However, these official and popular ways have their own short comings. Here are few:

1. Your Workflow Template or Custom Actions should be publicly available. However, large organizations have major security policies and therefore may not allow creation of publicly available repo.

2. In such organizations, we mostly have multiple product or service groups. Sometimes, it creates a big mess when you want to share something like workflow templates.

Lets now discuss how to share Github Action workflow templates within your organization privately. Instead of pulling or importing any workflow template, I would like to discuss the idea of publishing the workflow template.

- Create a repo which will host the workflow templates.

- Create a workflow in that repo which will do the publishing part.

So, let’s get started. I am going to use two types of github repo. One(workflow-templates) which will be the source of workflow templates and other(java-service-1, java-service-2) will be the target.

Note: I am using vs-code as editor, but you can use any editor of your choice.

Steps:

1. Create a workflow template repo and add a template in it (I am going to skip the GitHub repo creation part and will directly jump to the template creation.)

a. Open directory in vs-code

code workflow-templates

b. add folder name “templates”

c. add following file in your “templates” directory

mvn-test.yml

name: Maven Test



on:

push:

branches:

- 'master'

jobs:

mvn-test:



runs-on: ubuntu-latest

steps:

- name: Checkout repo

uses: actions/checkout@v2



- name: Set up JDK 1.11

uses: actions/setup-java@v1

with:

java-version: 1.11



- name: Build with Maven

shell: bash

run: |

mvn test

2. add folder “.github/workflows” in your project root.

a. add following workflow file in the “.github/workflows”

publish-workflow.yml

name: Workflow Publisher on:

push:

branches: [ 'develop', 'master' ]

paths:

- 'templates/**' jobs:

publish-workflow-template:

runs-on: ubuntu-latest

strategy:

matrix:

target_repo: [ 'java-service-1' , 'java-service-2' ]

steps:

- name: Checkout Workflows

uses: actions/checkout@v2

with:

path: main - name: Checkout target repo

uses: actions/checkout@v2

with:

repository: ${{ github.repository_owner }}/${{ matrix.target_repo }}

path: ${{ matrix.target_repo }}

token: ${{ secrets.RW_REPO}}

ref: master - name: Update workflows

shell: bash

run: |

mkdir -p .github/workflows/

cp ../main/templates/* .github/workflows/

working-directory: ./${{ matrix.target_repo }} - name: Publish workflows

shell: bash

run: |

git config --local user.email "action@github.com"

git config --local user.name "GitHub Action"

git add .

git commit -m "Github Agent | Workflows has updated"

git push

working-directory: ./${{ matrix.target_repo }}

Note: please change the target repos according to your requirement.

3. Before committing your changes, lets create a Personal Access Token(PAT). Use following link to create your own PAT and name it as RW_REPO.

Personal Access Tokens (github.com)

Note: please enable SSO for your organization if you have any.

4. Use the following link to add your PAT in your repo(workflow-templates)

https://github.com/<your-organization>/workflow-templates/settings/secrets/actions/new

5. Commit your changes. Voila!! Your new workflow(Maven Test) should be published to the target repos. If you make any subsequent changes to the workflow-templates it should be synced automatically to all target repos.

References:

Publisher repo:

Target repo: