How to sync up files with GitHub Actions.
When you need to keep some common code across all branches, like workflows, READMEs, and scripts, you can do it manually by pulling code from one branch and committing to another branch. However, this can easily get out of sync over time, and it is manual work that requires time to constantly sync all code across all relevant branches.
In this blog, you learn how to automagically sync up files between branches using GitHub Actions.
Scenario
Suppose, you have a workflow in the main branch, that you want to sync on other branches (prod, develop, int) and you have one workflow which you want to sync up on those branches.
This is the workflow you want to sync up, but could be any other file:
#.github/workflows/tests.yml
name: Hello from main
on:
pull_request:
branches:
- main
- develop
- int
- prod
jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: test
run: |
echo "Hello from ${{ github.base_ref }}"
Solution
There is a very cool GitHub Action named BetaHuhn/repo-file-sync-action that can do the sync of files for us using automation.
You just need two things to configure it:
- workflow file with on .github/workflow/sync-up.yaml
- the configuration specifying the branches to sync up. .github/sync.yml
The workflow will run every time new changes are merged to `main` branch.
#.github/workflows/sync-up.yaml
name: Sync Common Code
on:
pull_request:
branches:
- main
types: [closed]
jobs:
sync_backup_workflow:
runs-on: [atc-ubuntu-22.04-v2]
if: github.event.pull_request.merged == true
steps:
- uses: actions/checkout@v2
- name: Sync Up
uses: BetaHuhn/repo-file-sync-action@latest
with:
GH_PAT: ${{ secrets.GH_TOKEN }}
CONFIG_PATH: .github/sync.yml
COMMIT_EACH_FILE: false
PR_LABELS: "automated-sync"
# Uncomment to skip Pull Request and push directly
# SKIP_PR: true
On secrets.GH_TOKEN refers to a personal access token that is needed to access your repositories you have to specify a Personal Access token as the value for GH_PAT (GITHUB_TOKEN will not work).
You need repo full permission for the automation work.
On the .github/sync.yml saved on the main branch, you can find all the individual files that are synced from the source on the main to the destination in the group.
#.github/workflows/sync.yml
group:
repos: |
laysauchoa/repo-from-template@prod
laysauchoa/repo-from-template@develop
laysauchoa/repo-from-template@int
files:
- source: .github/workflows/tests.yml
dest: .github/workflows/tests.yml
So once, a pull request is merged in the main branch, other pull requests will be automatically opened with the new changes to sync.
Hope this is useful for you to keep everything in sync. You can read more on how to keep everything synced on the actions documentation, including how to sync entire repositories.
–
Hello 👋! I'm Laysa, a developer and cloud engineer ☁️, a public speaker 🎤, and a knowledge sharer 📚 as I go along
If you like this article, why not give it a share?
And you can follow me or reach out here | Laysa Uchoa on X | Laysa Uchoa on LinkedIn
Get in Touch.
Let’s discuss how we can help with your cloud journey. Our experts are standing by to talk about your migration, modernisation, development and skills challenges.