DEV Community

Cover image for Make changes on Repository A while modifying Repository B? Github Actions is here
Tomas Sirio
Tomas Sirio

Posted on • Updated on

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

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:

Alt Text

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
Clone ProjectA and ProjectB

Replicate the file from ProjectA to ProjectB and push them to 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
Enter fullscreen mode Exit fullscreen mode

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

on: push

        runs-on: ubuntu-latest
        name: Replicate File

        - name: Checkout Project A
          uses: actions/checkout@v2
            path: ./projectA

        - name: Checkout Project B
          uses: actions/checkout@master
            repository: tomassirio/projectB
            token: ${{ secrets.ACTIONS_TOKEN }}
            path: ./projectB

        - name: Copy/Create file
          run: |
            if [ -f "$FILE" ]; then
              echo "Copying $FILE"
              cp -R ./projectA/file ./projectB
              echo "Creating $FILE"
              touch ./projectB/file

        - name: Push Project B
          run: |
            cd ./projectB
            git add .
            git config github-actions
            git config
            git commit -am "File Replicated from Project A"
            git push
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

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)

Alt Text

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:

Project A

Project B

Top comments (1)

sohag033 profile image
Sohag Samajpati

Thanks for such an excellent job.
I am trying to use your process, but it provides an error during the push like the below---
remote: Permission to gitREPo denied to github-actions[bot].
fatal: unable to access '': The requested URL returned error: 403

could you please help me to get rid of it