Github Action To Sync Github Action's Workflow Files Across Repositories
| Argument | Defaults | Description |
|---|---|---|
GITHUB_TOKEN |
- | Required Token to use to get repos and write secrets. ${{secrets.GITHUB_TOKEN}} will not work. instead Personal Access Token Required* |
GPG_KEY_ID |
- | Required GPG Key ID to sign commits. You may import your GPG private key and use the output to the key id |
REPOSITORIES |
- | Required New line deliminator regex expressions to select repositories. Repositories are limited to those in which the token user is an owner or collaborator. |
WORKFLOW_FILES |
- | Required New line deliminator regex expressions. workflow files to be copied to provided repositories |
DRY_RUN |
false | Run everything except for nothing will be pushed. |
WORKFLOW_FILES_DIR |
workflows | Local Path Where Common Workflow Files Are Located Eg : workflows |
AUTO_CREATE_NEW_BRANCH |
false | Auto create new branch in a repository if the branch dose not exists |
COMMIT_EACH_FILE |
false | if you need to keep track of each file's commit history separate then set it to true |
PULL_REQUEST |
false | Set to true if you want the changes to be pushed via pull request. |
SKIP_CI |
false | Set to true if you want skip all automation inside target repository. |
COMMIT_MESSAGE |
false | You can provide your custom commit message. |
Github Personal Token Is required with the below scope
Full Repo is only required when you need to update private repository if your are updating only public repository then just select
public_repoinside repo scope
Repository With Default Branch
REPOSITORIES: |
username/repo
username/repo2Repository With Custom Branch
REPOSITORIES: |
username/repo@dev
username/repo1@dev2You Can also have same repository multiple times if you provide different branch name
- If you use
=as a file separatorfile1.md=myfile.mdthenfile1from the current repository will be copied to remote repository with the name ofmyfile.md - If you use
!=as a file separatorfile1.md!=myfile.mdthenfile1from the current repository will be copied to remote repository with the name ofmyfile.mdonly ifmyfile.mdalready not exists in the remote repository
Files - Source & Destination File Without Custom Name
WORKFLOW_FILES: |
dependabot.yml
.github/settings.ymldependabot.yml will save in root folder in the repository
.github/settings.yml will save in
.githubin the repository
Files - Source File In Root & Destination File In Custom Location
WORKFLOW_FILES: |
hello-bot.yml=.github/
pr-bot.yml=.github/pull-request.ymlhello-bot.yml will save in
.githubin the repository with the same namepr-bot.yml will save in
.githubin the repository with the namepull-request.yml
Folders - Source & Destination Folders Without Custom Name
WORKFLOW_FILES: |
folder1
.github/folder2folder1 will save in root folder in the repository
.github/folder2 will save in
.githubin the repository
Folders - Source & Destination Folders With Custom Name
WORKFLOW_FILES: |
folder1=./save-to-folder
.github/folder2=custom-folder/save-to-folder2folder1 will save inside
REPOSITORY ROOTin the name ofsave-to-folder.github/folder2 will save inside
custom-folderin the name ofsave-to-folder2
Before copying the WORKFLOW_FILES from the source to destination. this action will provide some flexibility.
this searches for a file in various locations for example lets take settings.yml as the file that you want to sync for multiple repository
./{OWNER}/{REPO_NAME}/workflows/{filename}./{OWNER}/workflows/{filename}./{WORKFLOW_FILES_DIR}/{filename}./.github/workflows/{filename}./{OWNER}/{REPO_NAME}/{filename}./{OWNER}/{filename}./{filename}
if the
settings.ymlis found insideworkflowsfolder then the destination is automatically forced to.github/workflowsin the destination repoif the
settings.ymlis outside ofworkflowsfolder then the destination then its copied to the destination
Lets assume that you want to maintain all the common github files in a single repository and suddenly a repository needs a single file to be changed in that case instead of editing the action yml file. you can just create a folder like {REPO_OWNER}/{REPO_NAME}/{FILE} to copy the overriden file to the destination
Create a new file in .github/workflows/ named workflow-sync.yml and copy & paste the below file content
name: Workflow Sync
on:
push:
branches:
- master
env:
DRY_RUN: false
REPOSITORIES: |
WORKFLOW_FILES: |
jobs:
Github_Workflow_Sync:
runs-on: ubuntu-latest
steps:
- name: Fetching Local Repository
uses: actions/checkout@master
- name: Running Workflow Sync
uses: mo-auto/actions-sync-workflows@main
with:
DRY_RUN: ${{ env.DRY_RUN }}
REPOSITORIES: ${{ env.REPOSITORIES }}
WORKFLOW_FILES: ${{ env.WORKFLOW_FILES }}
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
GPG_KEY_ID: ${{ steps.import_gpg.outputs.keyid }}
Spacing around the equal sign is important. For example, this will not work:
WORKFLOW_FILES: |
folder/file-sync.yml = folder/test.txtIt passes to the shell file 3 distinct objects
- folder/file-sync.ymll
- =
- folder/test.txt
instead of 1 object
- folder/file-sync.yml = folder/test.txt
and there is nothing I can do in code to make up for that
You do not need (nor want) leading / for the file path on either side of the equal sign
The only time you need / trailing is for folder copies.
While a file copy will technically still work with a leading /, a folder copy will not
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.