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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay