Make changes on Repository A while modifying Repository B? Github Actions is here

Tomas Sirio
2 min readOct 27, 2020

--

Last week at work I had an Idea which would require some files to be replicated on another repositories after I pushed them (or merged them through a pull request) on my working repository.

The example would look something like this:

As you can see, I wanted the File ‘file’ to be replicated on Repository B every single time a new copy or a modification of the existing one is pushed.

So I decided to use Github Actions for this issue. Github Actions provides you with a tool to do something on a Github event. In our case, the event would be a Push to my repository.

Github actions only requires you to create a .yaml file on the folder ./github/workflows on the route of your project. Dividing the ‘actions’ through steps, I decided to take the next approach:

Clone ProjectA and ProjectB

Replicate the file from ProjectA to ProjectB and push them to ProjectB

Push -> Clone ProjectA -> Clone ProjectB -> Copy from ProjectA the file and paste it in ProjectB -> Commit and Push on ProjectB

Let’s take a look to the .yaml file:

Things that you can skip if you are not interested in the technical side of Github Actions

As you can see. It’s a basic .yaml file. The first line determines which event you want the Action to be triggered on. Next you define the jobs (this can be modularized). Then on each job you define the steps the job will take.

In this case we ask for an ubuntu container to be created. In that container, on path ./projectA we will checkout our projectA repository while on path ./projectB we will clone our projectB repo. IMPORTANT You’ll have to define a PAT (Personal Access Token) on Github and create a secret on your projectA repository with the PAT as it’s value.

The third and fourth steps are pretty much self explanatory. If the file exists, we copy it from projectA to projectB, if it doesn’t exist (for whatever reason) we create it on projectB. Lastly we use git con projectB to push the file.

Checking the Action

Lastly, once the event is triggered, you’ll want to check out how Github is working behind the scenes. So head to the Actions tab on Github and if your workflow was rightly formatted (fingers crossed) you’ll get a window like this:

Of course, you’ll want to check if the file was replicated on your projectB repository (Because of course no one believes their solution to work on the first 100 times)

But this time, mine was :D

I’ll leave the repositories here if you want to check it out, clone it and play with it:

Originally published at https://dev.to on October 27, 2020.

--

--

Tomas Sirio
Tomas Sirio

Written by Tomas Sirio

I’m a Barbie Dev, In a Barbie World.Wide.Web — Backend developer — Master of memes.

No responses yet