Git Product home page Git Product logo

fork-sync-with-upstream-action's Introduction

Github Action: Fork Sync With Upstream

An action for forks! Automatically sync a branch on your fork with the latest commits from the original repo. Keep things up to date! (It actually works for syncing any repo branch with an upstream repo, but I was thinking about forks when I made it.)

Buy Me A Coffee

v2.4 updates:

  • Add required target_repo_token input - must always be set to ${{ secrets.GITHUB_TOKEN }}
  • Add domain input option
  • Fix some typos

Note: This is the final v2 update. All new updates go to v3 and later.

Intended Workflow

This action is primarily intended to keep untouched branches up to date with a remote repo. So let's say you want to keep main updated with the upstream repo. You make a new branch dev (or whatever) and commit your new changes there - not on main. If you commit on main, you create a split history between your downstream and upstream changes, and any merge from upstream will fail. (This is just how git works.) But if you keep them separate and keep main clean, you can always pull the upstream's latest from main.

From there, you can make controlled merges into your active branches, make clean PRs directly from main, and pretty much do whatever you want with your smoothly updated branch!

sample git workflow

(Pretend this says 'main' so I don't have to edit the image. Thanks.))

If you really want to make commits and merge from upstream on the same branch, I added some notes to achieve this below. This is not recommended, however.

How to Use

As with any Github Action, you must include it in a workflow for your repo to run. Place the workflow at .github/workflows/my-sync-workflow.yaml in the default repo branch (required for scheduled jobs at this time). For more help, see Github Actions documentation.

Input Variables

Basic Use

Name Required? Default Example
upstream_repository aormsby/Fork-Sync-With-Upstream-action
upstream_branch 'master', 'main', 'dev'
target_branch 'master', 'main', 'prod'
target_repo_token must always be set to ${{ secrets.GITHUB_TOKEN }}
github_token ${{ secrets.UPSTREAM_REPO_TOKEN }}

For github_token - use ${{ secrets.GITHUB_TOKEN }} where GITHUB_TOKEN is the name of the secret in your repo (see docs for help)

Advanced Use (all optional args)

Name Required? Default Example
git_checkout_args '--recurse-submodules'
git_fetch_args '--tags'
git_log_format_args '--pretty=oneline' '--graph --pretty=oneline'
git_pull_args 'pull' '--ff-only'
git_push_args '--force'
git_user 'Action - Fork Sync'
git_email '[email protected]'
git_pull_rebase_config 'false'
Git Config Settings

Some basic git config settings must be in place to pull and push data during the action process. The following input values are set by default:

  • git_user
  • git_email
  • git_pull_rebase_config -> set the same as git default - 'pull.rebase = false'

These values are reset at the end of the action step. They can be modified as input if you want a different config. Set any of these config values to null to use existing git config data.

When You Want To Merge Into An Active Working Branch (Not Recommended)

If there are no conflicts between upstream and downstream branches => set git_pull_args: '--allow-unrelated-histories'.

This will allow divergent histories to be merged, but be careful with the results. It's very likely there will be conflicts in any merge unless you know for sure there won't be, so your mileage may vary here.

If you need to overwrite data for some reason => git_pull_args: '-s recursive -Xtheirs' (or -Xours)

Again, not recommended. But if you don't care about losing some data, just choose one side or the other. I actually haven't tested this, but I think it should go through. Do you feel lucky, punk?

Output Variables

has_new_commits - True when new commits were included in this sync

Sync Process - Quick Overview

Right now, the main.js script only exists to execute upstream-sync.sh. It's possible that future updates will add functionality. The shell script does the following:

  1. Check if you included upstream_branch in your inputs (required!)
  2. Make sure the right local branch is checked out (target_branch)
  3. Add the upstream repo you listed
  4. Check if there are any new commits to sync (and prints any new commits as oneline statements)
  5. Sync from the upstream repo (generally by pulling)
  6. Push to the target branch of the target repo

Ta-da!

Sample Workflow

This workflow is currently in use in some of my forked repos. View Live Sample

on:
  schedule:
    - cron:  '0 7 * * 1,4'
    # scheduled at 07:00 every Monday and Thursday

  workflow_dispatch:  # click the button on Github repo!


jobs:
  sync_with_upstream:
    runs-on: ubuntu-latest
    name: Sync main with upstream latest

    steps:
    # Step 1: run a standard checkout action, provided by github
    - name: Checkout main
      uses: actions/checkout@v2
      with:
        ref: main
        # submodules: 'recursive'     ### may be needed in your situation

    # Step 2: run this sync action - specify the upstream repo, upstream branch to sync with, and target sync branch
    - name: Pull (Fast-Forward) upstream changes
      id: sync
      uses: aormsby/[email protected]
      with:
        upstream_repository: aormsby/hugo-deploy-to-pages
        upstream_branch: main
        target_branch: main
        git_pull_args: --ff-only                    # optional arg use, defaults to simple 'pull'
        github_token: ${{ secrets.GITHUB_TOKEN }}   # optional, for accessing repos that require authentication

    # Step 3: Display a message if 'sync' step had new commits (simple test)
    - name: Check for new commits
      if: steps.sync.outputs.has_new_commits
      run: echo "There were new commits."

    # Step 4: Print a helpful timestamp for your records (not required, just nice)
    - name: Timestamp
      run: date

fork-sync-with-upstream-action's People

Contributors

aormsby avatar bdougie avatar cognifloyd avatar delazj avatar manuelvanrijn avatar mathieucarbou avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.