Skip to content

About

Keeps a fork that carries its own changes up to date: merges the upstream branch into a pull request that CI tests and merge-when-green merges

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

sync-upstream

GitHub CI

Keeps a fork that carries its own changes up to date. It merges the upstream branch into a new branch of the fork's trunk and opens a pull request, so CI tests the result and merge-when-green merges it once the check passes. A merge that conflicts is aborted and reported in an issue instead.

When to use this

A fork whose trunk has commits upstream does not, so GitHub's Sync fork cannot fast-forward it. This merges upstream in rather than rebasing the trunk, which keeps the trunk's history and every tag or pin that points into it valid.

Where the fork has no changes of its own, GitHub's Sync fork or gh repo sync does the job and this is not needed.

Use

The action expects the trunk to be checked out with full history, using a credential that can push and that starts workflows. A deploy key does both; a push made with GITHUB_TOKEN starts no workflow, so CI would never run on the sync branch.

name: Sync upstream

on:
  schedule:
    - cron: "17 */6 * * *"
  workflow_dispatch:

permissions:
  contents: read
  pull-requests: write
  issues: write

concurrency:
  group: sync-upstream
  cancel-in-progress: false

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: main
          fetch-depth: 0
          ssh-key: ${{ secrets.DEPLOY_KEY }}
      - uses: ExaDev/sync-upstream@main
        with:
          trunk: main
          upstream-url: https://github.com/OWNER/REPOSITORY.git
          upstream-branch: main

Scheduled workflows only run from the default branch, so the trunk the workflow lives on has to be the default branch.

Pair it with merge-when-green using merge-method: fast-forward and the same deploy key. The sync branch is pushed by a deploy key, so its CI run is a push event, not a pull_request one; let the merge workflow accept it, for example by matching the branch prefix:

on:
  workflow_run:
    workflows: [CI]
    types: [completed]
jobs:
  merge:
    if: >-
      github.event.workflow_run.conclusion == 'success'
      && (github.event.workflow_run.event == 'pull_request'
          || startsWith(github.event.workflow_run.head_branch, 'sync/upstream-'))
    runs-on: ubuntu-latest
    steps:
      - uses: ExaDev/merge-when-green@v1
        with:
          ssh-key: ${{ secrets.DEPLOY_KEY }}
          merge-method: fast-forward
          required-check: Required checks

What a run does

  1. Fast-forwards each branch in mirror-branches from upstream, and with mirror-tags: 1 copies upstream's tags. A mirror that has diverged fails the run; nothing is overwritten.
  2. Stops when the trunk already contains the upstream branch.
  3. Otherwise merges the upstream branch into <branch-prefix><upstream commit> cut from the trunk. Paths listed in dropped-paths-file are removed from the merge, which also resolves upstream editing a file the fork deleted.
  4. On a conflict, aborts the merge, pushes nothing and opens or updates one issue with the conflicting files. On success, pushes the branch and opens a pull request with the automerge label.

A branch for the same upstream commit is only ever created once, so a run that finds it already pushed does not open a second pull request while one is open.

Inputs

Input Default Meaning
trunk required The branch that carries the fork's own changes.
upstream-url required Upstream's URL, added as the upstream remote when it is missing.
upstream-branch required The upstream branch to merge.
upstream-remote upstream The name of that remote.
mirror-branches none Branches to fast-forward from upstream.
mirror-tags 0 1 pushes upstream's tags to the fork.
dropped-paths-file none One path per line to remove from every merge.
branch-prefix sync/upstream- Prefix of the sync branch.
label automerge The label put on the pull request.
conflict-issue-title Upstream merge conflicts The issue opened or updated on a conflict.
git-user-name, git-user-email sync-upstream The identity on the merge commit.
github-token github.token Opens the pull request and the issue.

Outputs are branch (the sync branch, when one was pushed or already existed) and status (0, or 2 for a conflict).

Testing

tests/sync-upstream.test.sh builds throwaway upstream and fork repositories and covers an up-to-date trunk, a clean merge, a dropped path that upstream changes, a conflict, a diverged mirror, the branch prefix, a minimal configuration, tag mirroring and a missing setting. It runs in CI.

About

Keeps a fork that carries its own changes up to date: merges the upstream branch into a pull request that CI tests and merge-when-green merges

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages