DEV Community

Pin Loon Lee
Pin Loon Lee

Posted on

Github action to automatically push to another repository

Github action to automatically push to another repository

cover

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 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 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 from Carles Pina Estany useful for the case.

To better illustrate, I have created mock-medium-source and 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
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (1)

Collapse
 
brendaferreira profile image
BrendaFerreira

This really is a splendid post. Thanks a ton for taking the time to describe all of this out for folks. dua to get anything you want