GitHub Actions copy one git repository to another git repository

Configuration to have GitHub automatically copy files from one repository to another

GitHub Actions copy one git repository to another git repository
Page content

Introduction

GitHub provies “Actions” which are automatic actions that are ran on gitHub for you. This can be used to compile your code, compile your docker image, or do any arbitrary set of commands. This typically is done in software development to automatically push software between development and test. This is typically done to insure all the necessary code, configurations, etc necessary are checked into the source repository. Additionally this insures all the neccessary steps are done uniformly no matter which developer checks in the code.

Use Case

When code is commited to one git repository, part of it needs to be automatically copied and checked into another repository.

Hugo

This use case I use specifically with Hugo. Hugo is an MarkDown to HTML website rendering engine. It puts the rendered HTML into a folder named “public”. Yes I could use a sub-module to do this as well, but I chose to do it with github actions.

GitHub Action Code

In the source repository create a file “.github/workflows/publish.yaml”

  • replace GITUSER with the destination User or Organizaton that owns the destination repository
  • replace GITREPOSITORY with the destnation Repository you are publishing to
  • secrets.TOKEN is your github token the actions will be using (see below)
  • replace YOUR@YOUREMAIL.COM with the email you would like to see on the commit in the destination repository
name: publish
run-name: ${{ github.actor }} is publishing Registry
on: [push]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Source
        uses: actions/checkout@v3
        with:
          path: 'src'
          ref: 'main'
          persist-credentials: false

      - name: Checkout Destination
        uses: actions/checkout@v3
        with:
          path: 'dest'
          ref: 'main'
          repository: 'GITUSER/GITREPOSITORY'
          token: ${{ secrets.TOKEN }}
          persist-credentials: true

      - run: |
          # change to destination repository
          cd dest
          # copy a files or folders from source to destination
          cp -rf ../src/public/* .
          # this is useful for debugging
          git status
          # github.actor is the name of the login that checked in the commit in the source repository       
          git config --global user.name ${{ github.actor }}
          # configure your email address
          git config --global user.email "YOUR@EMAIL.COM"
          # add all the copied files to the destination repo for a commit
          git add .
          # check if any files actually changed, if files are different them commit them 
          # include the sha commit from the source repository as the commit in the destiation commit.
          git diff --quiet && git diff --staged --quiet || git commit -m "from ${{ github.sha }}" 
          # push the changes
          git push origin           

GitHub Secret Token

  1. Create a token for Actions to use. [https://github.com/settings/tokens](https://github.com/settings/tokens](https://github.com/settings/tokens). This token needs to allow the action to access the destination repository.
  2. Create a Actions Secret in the source repository named “TOKEN”, and place the token from step #1 into step #2 (see screenshot)

secret

GitHub Actions Demonstration

Simply check something into the source repository, and click “Actions” and you can watch the actions run and inspect the output of the code when it runs.

demo

demo