DEV Community

Cover image for Mirror a sub-directory GitHub repo with CircleCI
Yoan Pratama Putra for KodeFox, Inc.

Posted on

1

Mirror a sub-directory GitHub repo with CircleCI

Last time, I was talking about Mirroring a Github Repo with CircleCI. That article was about mirroring the whole repository using git clone --bare command. By using that git command, we not just mirror the whole content but also all branches from the repository.

There are times that we want to only mirror a sub-directory from the main repository. We can achieve that easily by using a special command by "git".

So today I will share what I learned mirroring a sub-directory from a GitHub repository using CircleCI.

This time we don't need to generate an SSH key. We will use CircleCI's checkout command as we need the CI to clone our repository normally, not bare.

Add the code below into your "config.yml" and CircleCI will do the magic for you.

version: 2.1
jobs:
mirror:
docker:
- image: circleci/node:lts
steps:
- checkout
- run:
name: Prepare for mirroring
command: |
git config --global user.email "your.github@email.com"
git config --global user.name "Your Name"
# Need to create .ssh dir in case it doesn't exists
mkdir -p ~/.ssh
# This too
touch ~/.ssh/known_hosts
# To avoid prompt when doing ssh connection
ssh-keyscan 'github.com' >> ~/.ssh/known_hosts
- run:
name: Mirroring repo
command: |
git filter-branch --prune-empty --subdirectory-filter ${SUB_DIR_NAME} ${BRANCH}
git remote add mirror git@github.com:oshimayoan/something-mirror.git
git push mirror master
workflows:
main:
jobs:
- mirror:
filters:
branches:
only: master
view raw config.yml hosted with ❤ by GitHub

We're done! Actually, this is a lot easier than the previous one as there is almost no setup this time.

You can push a commit now to make the CircleCI mirror your sub-directory into another repository.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →