DEV Community

cloud-sky-ops
cloud-sky-ops

Posted on

3 1 1

Automating File Sync Across Repositories with a GitHub Action

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")
Enter fullscreen mode Exit fullscreen mode
  • File uploads with Base64 encoding:
  response = requests.put(url, json=payload, headers=HEADERS)
Enter fullscreen mode Exit fullscreen mode
  • Automated PR creation:
  response = requests.post(f"https://api.github.com/repos/{repo}/pulls", json=payload, headers=HEADERS)
Enter fullscreen mode Exit fullscreen mode

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.


Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay