GitHub Action to Automatically Push to Another Repository
It’s time to let GitHub action automatically push for you!
Have you ever had the need for some of your repository to automatically push the content to another repository?
There may be a couple of reasons for this. Perhaps your main repository is not yet shareable or you would like to keep some content out of the other repository, maybe 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 posts 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 posts, I have been using the Github action from Brian Mayo
to automatically publish the post to Medium. All I have to do is just write 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
The post in Medium would be originated from .md
files while the rest of the content under posts/<number>/files
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
The yaml file for Github workflow in mock-medium-source
is as below, (remember to create secrets for the SSH_DEPLOY_KEY
)
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 frommock-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 ofmock-medium-source
would only be triggered when there is changes on the directory underposts
. 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!
If you enjoyed this article, please follow me on Medium to show your appreciation. Thanks and hope this is helpful for you!