Repo File Sync Action

repository·master·Indexed 18 days ago

https://github.com/betahuhn/repo-file-sync-action

A GitHub Action to keep files, such as workflows, configurations, or entire directories, in sync across multiple repositories or branches. It automates synchronization by detecting changes in a source repository and opening Pull Requests in target repositories. Features include support for Nunjucks templates, directory syncing with exclusion rules, group-based syncing to multiple repos, and integration with GitHub App installation tokens or Personal Access Tokens.

Tokens
5.5K
Snippets
22
Records
25
Agent score
13%

What's inside repo-file-sync-action

  1. Use GitHub App Installation Tokens

    master

    Instead of a GH_PAT, you can use a token from a GitHub App installation via the GH_INSTALLATION_TOKEN input. This allows for more granular access control.

    Required Permissions for the App:

    • Contents: Read & Write
    • Metadata: Read-only
    • Pull requests: Read & Write (if using PRs, which is the default)
    • Workflows: Read & Write (if syncing workflow files)

    Note: If using an installation token, you must also provide the GIT_EMAIL and GIT_USERNAME inputs.

  2. Use Nunjucks templates for dynamic file content

    master

    You can render files using Jinja-style template syntax powered by Nunjucks.

    To enable templating:

    1. Set the template field to true (if no variables are needed) or to a dictionary of context variables.
    2. Use {{ variable.name }} syntax within your source files.
    3. Use {% extends './path' %} for template inheritance.

    Variables provided in the template dictionary are available to the Nunjucks engine during the sync process.

    user/repo:
      - source: src/README.md
        template:
          user:
            name: 'Maxi'
            handle: '@BetaHuhn'

    src/README.md

    Created by {{ user.name }} ({{ user.handle }})

  3. Create a sync.yml configuration file

    master

    To configure which files are synced to which repositories, create a sync.yml file in the .github directory of your main repository.

    Target repositories are specified as top-level keys using the format username/repository-name or username/repository-name@branch. Under each target, you list the files or directories to be synced.

    user/repo:
      - path/to/file.txt
    user/repo2@develop:
      - path/to/file2.txt
  4. Sync individual files and directories

    master

    You can sync files by listing their paths directly, or sync entire directories using the source and dest keys.

    • Individual files: List the path directly under the repository key.
    • Custom destination: Use source for the original path and dest for the target path/filename.
    • Directories: Provide a directory path to source and a target directory to dest.
    user/repo:
      # List individual files
      - .github/workflows/build.yml
      - LICENSE
      
      # Custom destination path or filename
      - source: workflows/build.yml
        dest: .github/workflows/build.yml
      - source: LICENSE.md
        dest: LICENSE
    
      # Entire directories
      - source: workflows/
        dest: .github/workflows/
  5. Install and set up Repo File Sync Action

    master

    To use this action, create a workflow file in .github/workflows/ (e.g., sync.yml) that triggers on push or workflow_dispatch. You must provide authentication via either GH_PAT or GH_INSTALLATION_TOKEN.

    Important: The standard GITHUB_TOKEN will not work. For GH_PAT, you must use a Personal Access Token with the repo scope. It is recommended to store this as a Repository Secret.

    name: Sync Files
    on:
      push:
        branches:
          - main
          - master
      workflow_dispatch:
    jobs:
      sync:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout Repository
            uses: actions/checkout@main
          - name: Run GitHub File Sync
            uses: BetaHuhn/repo-file-sync-action@v1
            with:
              GH_PAT: ${{ secrets.GH_PAT }}
  6. Sync to different branches

    master

    You can target specific branches in the destination repositories by using the @branch suffix in the repository name. This is particularly useful when using groups to sync the same source files to different language or region branches of the same repository.

    group:
      repos: |
        foo/bar@de
        foo/bar@es
        foo/bar@fr
      files:
        - source: .github/workflows/
          dest: .github/workflows/
  7. Sync files to multiple repositories using groups

    master

    To avoid repetition when syncing the same set of files to multiple repositories, use the group syntax. A group defines a list of repos (as a multi-line string) and a list of files to be applied to all of them.

    group:
      - files: 
          - source: workflows/build.yml
            dest: .github/workflows/build.yml
          - source: LICENSE.md
            dest: LICENSE
        repos: |
          user/repo1
          user/repo2
    
      - files: 
          - source: configs/dependabot.yml
            dest: .github/dependabot.yml
        repos: |
          user/repo3
          user/repo4
  8. Choose the correct version tag

    master

    To ensure stability, choose your versioning strategy carefully:

    • @v1 (Recommended): Uses the latest non-breaking version. This includes bug fixes but prevents breaking changes from disrupting your workflow.
    • @latest: Always uses the absolute latest version available. Use this if you want the newest features immediately and are prepared for potential breaking changes.
    • Specific version: Use a fixed version number if you need absolute immutability, but remember to update it manually or via Dependabot.
    # Recommended for stability
    uses: BetaHuhn/repo-file-sync-action@v1
    
    # For latest features (use with caution)
    uses: BetaHuhn/repo-file-sync-action@latest
  9. Configure Repo File Sync Action via GitHub Actions

    master

    The repo-file-sync-action is a GitHub Action designed to synchronize files or directories from a source repository to a target repository. It automates the process of cloning the target, copying files, committing changes, and optionally creating or updating a Pull Request.

    Core Workflow

    1. Cloning: The action clones the target repository locally.
    2. Syncing: It iterates through the configured files, copying them from the source to the destination path in the target repo.
    3. Committing: Depending on configuration, it either commits each file individually or commits all changes in a single batch.
    4. Pushing: Changes are pushed to the target repository branch.
    5. Pull Request: If enabled, it creates or updates a Pull Request with optional labels, assignees, and reviewers.
  10. Configure synchronization with sync.yml

    master

    Create a .yml file at .github/sync.yml to define which files or directories should be synced to which target repositories.

    Supported formats:

    • Simple list: Map a user/repository to a list of file paths.
    • Explicit mapping: Use source and dest keys to map a specific source file to a different destination path/filename in the target repository.
    user/repository:
      - .github/workflows/test.yml
      - .github/workflows/lint.yml
    
    user/repository2:
      - source: workflows/stale.yml
        dest: .github/workflows/stale.yml
  11. Delete orphaned files in target directories

    master

    When syncing entire directories, you can enable the deleteOrphaned option. If set to true, any files present in the target repository's destination directory that are NOT present in the source directory will be deleted. This option only works for directory syncs and is scoped to the specific directory entry.

    user/repo:
      - source: workflows/
        dest: .github/workflows/
        deleteOrphaned: true
  12. Exclude files when syncing directories

    master

    When syncing an entire directory, you can prevent specific files from being copied by using the exclude key. The paths provided in exclude must be relative to the source path.

    user/repo:
      - source: workflows/
        dest: .github/workflows/
        exclude: |
          node.yml
          lint.yml