How to Auto-sync Update from One Github Repository to Other Repository Using Github Workflow

Wenn
The Startup
Published in
5 min readJun 27, 2020
Github Logo

Hi, it’s been a very long time for me to update new tech medium article since the last article published. Recently, I’m trying to explore a little a bit about Github workflow. According to Github, workflow is a configurable automated process made up of one or more jobs. So basically, by using workflow, it help us a lot in automating process upon updates (pull-request, push, etc) in our Github repository. Github Workflow often used in continuous integration (CI) process by running unit test and build it. I find it very interesting because it does very useful in my own use case. While exploring more about it, I thought it will be useful to share about how to use it as well.

Thus, I will share about my use case on why I have to use Github workflows to help my life easier. Currently, I have one private repository and one public repository for my codes at Github. So basically, my private repository have a lots of folder in it and I would like to make only one of its most updated folder as public repository. That’s why I have to copy the entire folder and make it a new public repository. The problem is I tends to update most codes in the private repository leaving the public repository to be outdated. It been a burden for me to have to copy all of the codes in the folder from private to public repository manually every time. Facing this problem, I have to think about solutions on how to automate updates every commits in my private repository to the other public repository. Thankfully, Github Workflow does sound interesting for me to implement it in my project.

First thing first, in order to make a Github workflow in your repository, you have to click on actions tab on your repository to initiate the first workflows in YML file. Once you arrive at actions tab, you have to click to “set up a workflow yourself”.

Github Actions Page

It will automatically create main.yml for you in .github/workflows folder. Here’s how main.yml will look like :

Example of main.yml in Github workflow

There’s a few of YAML syntax provided by Github for the usage of workflow. I will share some of the essentials syntax :

  1. name : It is the name of your workflow or jobs.
  2. on : It is the event that this workflow will listen to and will be triggered to run this workflow. The value can be push, pull request events and of course you can specify which branch of the events it will listen as well. Actually, there’s more of events that can trigger workflows, you can read more about it at this link.
  3. jobs : It contains a series of jobs that you can run sequentially or in parallel. The automated magic runs in it.
  4. steps: It the sequential order of tasks in running the jobs you have made.
  5. env : It helps you to store environment variables needed in your workflows.
  6. run : It is where you specify command line syntax to run your specific task.

In my own use-case, I want a specific flag provided by me that trigger updates. So, I will make use of commit message written to scan if it contain’s a certain word then it will run the jobs. Thus, we need an if syntax in the workflows. For example :

Example of if condition in Github workflow

Next thing we need to do is when it does contains the flag I stated , it will have to clone my other public repository to the Github workflow bash shell, then we will copy entire specified folder of private repository to these new public repository and then push it to the desired branch. In order to provide access for the bash terminal to read and write to repository, we will have to provide our Github’s Personal Access Token (PAT) in it. You can create your own Github’s PAT by going to your Github settings, click on Developer’s Setting , then Personal Access Tokens and finally click on Generate personal access tokens. In this page, you can configure authorisation’s that your token are allowed to do.

Note : By using personal access token, it should be treated as credentials because it serves as your key to access all of your repository.

Configuration page for Personal Access Tokens at Github

In order to be able to clone your repository in the bash shell, you have to include your token in the workflows. To make it secure and safe to use in your workflow, you can use secrets setting provided by Github to store credentials needed. It’s serves as your environment variables but it is more secure. You can access it by going to your repository (where the workflows yaml are) settings, click on secrets and create it and then you can access it by using this syntax ${{ secrets.KEY_NAME }}.

Back to the Github workflow, once you have created the secrets. You should include it at the very first steps of your jobs.

In order to clone it, It is recommended to use https over ssh. After cloning it, I copied all files that I needed from private repository folder to this new cloned folder and execute a few of simple git commands. The essential commands are :

  1. git remote set-url origin https://x-access-token:${{ secrets.MY_TOKEN }}@github.com/my/path/to/repository
  2. git add .
  3. git commit -m "COMMIT_MESSAGE"
  4. git push

Notes: Don’t forget to setup your git config first and remove your folder of the cloned repository at the end with the favourite command of rm -rf :D

That’s the end of this Github workflow tutorials. Hope it is useful. If you have any feedback about workflow, I’m open for discussing. Let’s learn together. Thank you !

References:

My Workflow YAML

My Github : https://github.com/WendyYanto

--

--

Wenn
The Startup

Google Certified Android Developer. Learning about Android, Backend technology and Algorithms.