Make changes on Repository A while modifying Repository B? Github Actions is here
Tags: github, git
Published: 2020-10-27 18:17:50

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](https://dev-to-uploads.s3.amazonaws.com/i/3l7vxh3g4s7r239fjpbb.png)

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:

Push
![Push](https://dev-to-uploads.s3.amazonaws.com/i/sfu1escg5q62dxxri1j8.png)

Clone ProjectA and ProjectB
![Clone ProjectA and ProjectB](https://dev-to-uploads.s3.amazonaws.com/i/evcby7rupv0mpnrg9rnm.png)

Replicate the file from ProjectA to ProjectB and push them to ProjectB
![Replicate the file from ProjectA to ProjectB and push them to ProjectB](https://dev-to-uploads.s3.amazonaws.com/i/0otb8cm2iuay0til5pnb.png)

```
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:

```yaml
on: push

jobs:
    replicate-file:
        runs-on: ubuntu-latest
        name: Replicate File
        
        steps:
        - name: Checkout Project A
          uses: actions/checkout@v2
          with:
            path: ./projectA

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

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

        - name: Push Project B
          run: |
            cd ./projectB
            git add .
            git config user.name github-actions
            git config user.email github-actions@github.com
            git commit -am "File Replicated from Project A"
            git push
```

## 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](https://dev-to-uploads.s3.amazonaws.com/i/pe6px9opsvnl7s3ybh0w.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/gtx8bnu0y9i4q5c7n5sd.png)

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](https://github.com/tomassirio/projectA)

[Project B](https://github.com/tomassirio/projectB)