Making PR to another repository using Github Actions

Published on · 4 min read · 1 view · 1 reading right now

GITHUB
ACTIONS
Github Action Logo

This post is not an introduction to Github Actions.

Github Actions is a tool to automate your workflow.

Introduction

So I came across this situation when working on an issue where you had to make a github action that will make PR to another repository with files present inside /data directory.

Having searched the Github Marketplace Actions and not getting any satisfactory result I set out to make my own workflow which will make a PR to another repository.

Workflow

Here's the full workflow file that should be present inside .github/workflows directory inside your repo.

name: Run workflow

on:
  push:

jobs:
  Make-PR:
    name: Copy files and make PR
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.API_TOKEN_GITHUB }}
    steps:
      - name: Checkout Current repository
        uses: actions/checkout@v2
        with:
          path: current
      - name: Checkout Another repository
        uses: actions/checkout@v2
        with:
          repository: aayushmau5/another
          path: another
          token: ${{ env.GITHUB_TOKEN }} # Optional(In case you get permission errors)
      - name: Make branch in Another repository & Copy files from Current Repo to Another
        working-directory: ./another
        run: |
          cd ./another
          git checkout -b new-push
          cp ../current/data/* ./another/data
          git add .
          git commit -m "Generated"
          git push https://${{ env.GITHUB_TOKEN }}@github.com/aayushmau5/another
      - name: Create PR
        working-directory: ./another
        run: |
          cd ./another
          gh pr create --title "Automated PR" --body "PR Generated by Github Actions" --head "new-push"

Explanation

Here's the breakdown of what the action is doing.

name: Run workflow

on:
  push:

We setup the name as well the event for which the action will be triggered.

This action will be triggered when we push(commit & push) in any branch of the repository.

jobs:
  Make-PR:
    name: Copy files and make PR
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.API_TOKEN_GITHUB }}

We now setup a job that will run which the action is triggered. In this case, we have a single job named Make-PR.

The name of the job is "Copy files and make PR" and it will run on the latest release of ubuntu OS.

We also setup the Environment variable with GITHUB_TOKEN which we get from the secrets. More info about secrets here.

This GITHUB_TOKEN environment variable will be used for Authentication(+ Authorization) when pushing changes or making a PR.

steps:
  - name: Checkout Current repository
    uses: actions/checkout@v2
    with:
      path: current
  - name: Checkout Another repository
    uses: actions/checkout@v2
    with:
      repository: aayushmau5/another
      path: another
      token: ${{ env.GITHUB_TOKEN }}

Every job consists of one or more steps that run sequentially.

We have 4 steps in our Make-PR job. Let's see what the first two steps do.

As the name suggests, we Checkout the current as well as Another repository using actions/checkout@v2 which is a community made Action which will checkout the provided repository.

Here we are fetching the current repository under the path current, so it will be accessible through ./current and we are also fetching a repo named another which will be stored inside another(./another) path. We have also provided token because of permission issues(as GH doesn't allows gh-action-bot to push commits, etc).

- name: Make branch in Another repository & Copy files from Current Repo to Another
  working-directory: ./another
  run: |
    git checkout -b new-push
    cp ../current/data/* ./another/data
    git add .
    git commit -m "Generated"
    git push https://${{ env.GITHUB_TOKEN }}@github.com/aayushmau5/another
- name: Create PR
  working-directory: ./another
  run: |
    gh pr create --title "Automated PR" --body "PR Generated by Github Actions" --head "new-push"

One thing you should know that the remote system on which Action runs(Ubuntu, in this case) comes with git and github cli already installed.

So, you can use github cli to create PR.

Here are the last three steps.

The 3rd step(1st & 2nd covered above) does as the name suggests

  • Creates a branch named new-push in another repo.

  • Copies files from current/data to another/data and commits it.

  • Pushes the commit to another repo.

And at last, we have the 4th step which creates a PR in our another repository using the github cli's gh pr create command.

So, this is it for creating a PR using Github Actions. You can create pretty neat workflows with actions. It's a great tool for developers.

0 likes

Other articles