Problem statement and introduction:

In many mid to large scale organisations software solutions are created using micro-services architecture. There you might encounter a bunch of repeated implementations when it comes to CI/CD processes. Managing consistency across multiple repositories can quickly become cumbersome. Whether it’s standardizing CI workflows, updating documentation, or synchronizing Dockerfiles, keeping everything in sync is crucial but often manual. In short, a bunch of repeated Ctrl c + Ctrl v.

That's where the newly published Sync Files to Multiple Repos via API action I developed comes into play. Licensed under MIT, this action is now available in GitHub Marketplace for free use. Built using Python and GitHub’s REST API, this action makes syncing files and directories across many repositories clean, efficient, and fully automated.

Sync Files to Multiple Repos via API
Source Code in GitHub


What this GitHub Action does?

This action allows you to synchronize specific directories or all directories in a repo across multiple GitHub repositories, without needing to clone or open PRs manually. It uses GitHub's REST API to:

  • Fetch default branches of target repositories
  • Create or update files in said repositories
  • Create new branches for changes (optional)
  • Open pull requests automatically (optional)

Key components of the action:

Here are five key components of the sync-files-multi-repo action:

1. Using GitHub API

I used GitHub API for file updates, fetching repository data and branch related operations in this action. This helped me learn about REST APIs in python and also gave me an alternative to the traditional approach of relying on multiple git commands.

2. Secure and Scoped Authentication

The action uses a Personal Access Token (PAT) injected in the workflow via GitHub secrets. it's important to highlight, when syncing .github/workflows, it requires the workflow scope. This showcases integration of secure and scoped authentication in GitHub Actions.

3. Full Directory Sync Support

Supports copying full directories from the source repository to destination repositories. It maintains subdirectory structure and makes configuration simple with just two inputs:

  • copy-from-directory
  • copy-to-directory

If not provided, it falls back to syncing the root directory in respective repos.

4. Flexible Sync Modes

You can choose to commit changes directly or open pull requests:

  • create-pull-request: true opens a PR on a new branch
  • false commits directly to the default branch

Great for balancing speedy automation against PR review compliances.

5. Simple Setup for Broad Adoption

Target repositories are listed in a plain sync-repos.txt file. The action provides sensible defaults, enabling even non-DevOps contributors to use it effectively. In future release the .txt file may be replaced with a more structured file like JSON or YAML.


Real-World Use Cases

Use Case Description
Sync shared documentation Keep README.md, LICENSE, etc., consistent across repos
Standardize CI/CD Deploy the same GitHub workflows to all microservices
Update base Dockerfiles Roll out updated base image configs across repos

Behind the Scenes: How It Works

The action is implemented in pure Python using standard libraries like os, base64, and requests. Some implementation highlights include:

  • Dynamic default branch detection:
def get_default_branch(target_repo):
      response = requests.get(f"https://api.github.com/repos/{target_repo}", headers=HEADERS)
      return response.json().get("default_branch")
  • File uploads with Base64 encoding:
response = requests.put(url, json=payload, headers=HEADERS)
  • Automated PR creation:
response = requests.post(f"https://api.github.com/repos/{repo}/pulls", json=payload, headers=HEADERS)

Architecture Summary

Component Details
Language Python 3
API GitHub REST v3
Auth PAT via Authorization header
Branching Timestamped feature branches

Conclusion

The sync-files-multi-repo GitHub Action is a robust, easy to use automation for cross-repository updates. As we discussed, it offers real value to engineering teams in multiple use cases.

If you’ve been managing updates across multiple repositories manually, give this action a spin and let me know your thoughts in the comments. Follow me for more such automations.

X Profile
LinkedIn

Thank You.