Github action to automatically push to another repository
Tags: blogging
Published: 2022-09-18 09:24:08


# Github action to automatically push to another repository

![cover](https://cdn-images-1.medium.com/max/800/0*QBe0R0F4jfMeucKz)

Have you ever had the need for some of your repository to automatically push the content to another repository?

There may be couple of reason for this, perhaps your main repository is not yet shareable or you would like to keep some content out of the other repository, perhaps you wish to keep the main repository private while acting as a centralized repository to control contents that are private and partially public.

Since I have just started to write post in [Medium](https://pinloon.medium.com) recently, I had the need to do so when I wish to organize the posts in a better manner. To ease my life of writing post, I have been using Github [action](https://github.com/protiumx/blogpub) from `Brian Mayo` to automatically publish the post to Medium. All I have to do is just writting down the post using Markdown format and push to Github. The blog repository has always been publicly available as it is also acting as a storage space for the sample codes used in the post.

Nevertheless, when I realized there might be someday when some of the post may be only accessible by Medium members, I should keep the content private in a separate repository while enabling the codes used in the post to be publicly available. After considering git submodule, encrypted markdown, I have decided to keep current blog repository (that contains both post and code) private,  while having an additional Github's action to export only the shared code to another public repository.

That is when I found Github [action](https://github.com/cpina/github-action-push-to-another-repository) from `Carles Pina Estany` useful for the case.

To better illustrate, I have created [mock-medium-source](https://github.com/pllee4-Experimental/mock-medium-source) and [mock-medium-output](https://github.com/pllee4-Experimental/mock-medium-output) to mock my use case.


From the `mock-medium-source`, the file structure is as below

```
├── .github
│   ├── workflows
│   │   ├── medium-publish.yml
├── posts
│   ├── 001
│   │   ├── files
│   │   │   ├── Makefile
│   │   │   ├── mock.cpp
│   │   ├── post1.md
│   ├── 002
│   │   ├── files
│   │   │   ├── Makefile
│   │   ├── note.txt
│   │   ├── post2.md
├── README.md
```

The post in Medium would be originated from `.md` files while the rest of the content under `posts/<number>` would be reference code or information about the post.

Hence, the thing that I shall keep private would be the markdown file as the shared code would be less meaningful without the content of the post. Although [Github Gist](https://gist.github.com/) is a popular way to share code and snippets, at this moment I still prefer to keep code in a single place together with `Makefile` or `CMakeLists.txt` to ease compilation and running of code. I did not use any fancy operation to filter out those private content from `mock-medium-source` before pushing `mock-medium-output`, currently it is just a simple deleting of those markdown files before pushing to other repository.

The expected file structure of `mock-medium-output` repository would be just
```
├── posts
│   ├── 001
│   │   ├── files
│   │   │   ├── Makefile
│   │   │   ├── mock.cpp
│   ├── 002
│   │   ├── files
│   │   │   ├── Makefile
│   │   ├── note.txt
```

The yaml file for Github workflow in `mock-medium-source` is as below, (remember to create secrets for the `SSH_DEPLOY_KEY`)

```
name: Publish to Medium

"on":
  push:
    branches:
      - master
    paths:
      - 'posts/**'

jobs:
  post:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Creates output folder and remove markdown
        run:  |
              mkdir output
              rm -rf posts/*/*.md
              cp -r posts output
      - name: Pushes to public repository
        id: push_directory
        uses: cpina/github-action-push-to-another-repository@ssh-deploy-key
        env:
          SSH_DEPLOY_KEY: ${{ secrets.WORKFLOW_DEPLOY_KEY }}
        with:
          source-directory: output/
          destination-github-username: 'pllee4-Experimental'
          destination-repository-name: 'mock-medium-output'
          user-email: pinloon_0428@hotmail.com
          commit-message: pushed from $GITHUB_REF
          target-branch: master
```

For this yaml files, there are two things to be noted down
- The public repo `mock-medium-output` must be created before it is able to be pushed from `mock-medium-source`
  *("created" means it is pushed with at least one commit in order to have a valid branch as target branch for the source repository)*
- From the Github workflows, the path is `posts/**`, that means the running of the workflows of `mock-medium-source` would only be triggered when there is changes on the directory under `posts`. This makes sense for my usage, however you could remove this part if you wish to.

That is it! I can continue writing post using markdown format and have the workflows automatically push the relevant public content to be shared with you!

Thanks and hope this is helpful for you!