Automate GitHub Pull Requests with GitHub Actions
Tags: githubactions, pullrequest, automation, github
Published: 2022-10-22 20:37:37

---
title: Automate GitHub Pull Requests with GitHub Actions
published: true
description: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk45cbi4a5bgrwqtq4cn.png
tags: #githubactions, #pullrequest, #automation, #github 

---

Whenever you catch yourself in a repetitive task, try this: step back and think about ways you might automate it. One tool CI/CD folks love to use for automation is GitHub Actions. 

GitHub Actions are widely used to run tests and perform checks on our code. But what happens when your automation changes the code, and you want to commit those changes? If you are thinking about running the script manually and opening a pull request, this is an option. But here's an even better option: automate the process by using GitHub Actions to open pull requests for you. This article shows you how, step by step.

## Scenario

At Aiven, we've got an interesting problem: we keep expanding our list of cloud providers. This is nice because it gives you the flexibility to choose whatever provider works for you. For us, maintainers of dev portal documentation, it's a challenge to ensure that the most up-to-date information about our cloud providers is available for our readers.

You can find a list of Aiven's cloud providers on [Developer.aiven.io], which is also the portal where we host the developer documentation. To quickly retrieve a list of currently-available cloud providers, my colleague wrote a Python script that pulls our cloud listing from the [Aiven API](https://api.aiven.io/doc/#operation/ListClouds) and [generates documentation](https://github.com/aiven/devportal/pull/675/files), which is really cool!

Having the script was great. Now, by running the script, we could quickly generate fresh documentation out of the Aiven API data. This didn't automate the process entirely, though. Getting the changes merged still required a number of manual actions on a regular basis: run the script, check if there are new changes to commit, create pull requests, etc.

This is a good example where you can configure a GitHub Actions workflow to run the script periodically and open a pull request when changes are detected. So if you have a similar challenge, below you'll find how to add an automation flow step by step.

## Configure GitHub Actions workflow

Time for some action - or better, GitHub Actions! You can follow these steps to configure your GitHub Actions workflow:

### 1) Check the script

Let's ensure that the script works correctly before using it for automation. So, here you can run your script a couple of times and check that it is working as it is supposed to.

To make the process easier, you can add the commands to run the script in a Makefile. Makefiles are a helpful way to organize code compilation, and it helps to run long commands in a shorter version using the ``make`` command. Following our example, here is what we add to the [Makefile](https://github.com/aiven/devportal/blob/main/Makefile#L96-L97):

    ```
    SOURCEDIR     = .
    # (Re)Generate cloud listing
    cloud-list:
        python "$(SOURCEDIR)/scripts/aiven/clouds.py" "includes/clouds-list.rst"
    ```

Now we can simply run the script with:

    ```
    make cloud-list
    ```

This step ensures that the script you use to change the code produces the changes you want to commit in a pull request. Mistakes happen. If your script isn't working as expected, you may need to debug your code before proceeding. If the script is working, then you can go ahead and create the GitHub workflow.

### 2) Create a GitHub workflow

In this step, you need to add a YAML file to define the workflow configuration. First, make sure you have created a ``.github/workflows/`` folder; if not, you can go ahead and create the directory. Then, create a YAML file to define the workflow configuration, for example, ``.github/workflows/cloud-list.yaml``. In this file, you can define the name of the workflow. In our example, we start by giving a name to our workflow:

    ```
    name: Cloud - Create PR to update available list
    ```

Pick a name for a file and for the workflow that is related to the changes you are bringing to your code. This can help your code and to identify your workflows. Check out this quick guide from GitHub in [how to create your first workflow](https://docs.github.com/en/actions/quickstart). 
### 3) Choose a trigger

GitHub Actions enable you to trigger your workflow to run based on a certain event or events. In the example, the job needs to run periodically. For that, configure it to run ``on schedule`` according to a [cron job](https://ostechnix.com/a-beginners-guide-to-cron-jobs/).

The cron syntax can be confusing sometimes. To double-check your cron syntax, you can use an online and free tool called [crontab guru](https://crontab.guru/). Besides this, the page contains many [examples](https://crontab.guru/examples.html) that can help understand the syntax better.

For example, this workflow is configured to run every Tuesday at 6 AM UTC (8 AM CEST) time:

    ```
    on:
    schedule:
        - cron: "0 6 * * 2"
    ```

On GitHub Actions, the time is based on UTC, so you may want to consider this during the setup.

> **_NOTE:_**  On GitHub Actions, the shortest interval you can run scheduled workflows is once every 5 minutes.
If you want to run your job in every pull request or have a different use case, you can check out all the available triggers to run your workflow on the [GitHub documentation](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow).

### 3) Install dependencies and run the script

A GitHub workflow is composed of one or more jobs. A job is a set of steps executed in a certain environment defined on the ``runs-on``. In the example below, the ``CloudList`` job runs on the latest ubuntu image. After defining the environment, you need to ``checkout`` from a branch, for example the main branch, and decide on the Python version. Finally, you need to add a step to install the dependencies and run your script.

    ```
    jobs:
    CloudList:
        runs-on: ubuntu-latest
        steps:
        - name: Checkout the repo
            uses: actions/checkout@v2
        - name: Set up Python 3.8
            uses: actions/setup-python@v2
            with:
            python-version: "3.8"
        - name: Install dependencies
            run: |
            python -m pip install --upgrade pip
            pip install -r requirements.txt
        - name: Update available cloud list
            run: make cloud-list
    ```

With this, the job already runs periodically and generates the file. However, the final goal is to open a pull request so maintainers can check the changes and merge the code. So let's see how you can add the pull request step to your workflow.

### 4) Create Pull request

You can create a pull request by using the [Create Pull request](https://github.com/peter-evans/create-pull-request) action in our workflow. A cool feature here is that you can add some customization related to your pull request that can help in the pull request review process, including labels and a defined branch name, for example:

    ```
    - name: Create Pull Request
    uses: peter-evans/create-pull-request@v4
    with:
        commit-message: Cloud - Update available list
        title: Cloud - Update available list
        body: Cloud - Update available list
        base: main
        labels: automated-pr, Clouds & Regions
        branch: cloud-update-advanced-params
        delete-branch: true
    ```

The workflow runs the specified jobs according to the scheduled time even if the action is not merged.

![Workflow running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pitk5u0k2u2lq6dkbm9c.png)

Besides periodically running a workflow, you can combine it with the option to manually trigger your workflow, which can help you to test it. You can check out this [GitHub Actions manual](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/) for that.

When the action takes place, this is what the shiny new pull request looks like ✨:

![Automated open pull request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzi036kv4xkiqjtqh497.png)

So every week, the workflow runs and keeps our Aiven [cloud list](https://docs.aiven.io/docs/platform/reference/list_of_clouds.html) always updated for our beloved users. 🧡
## Conclusion

As devportal maintainers, we are happy to use GitHub Action in our CI/CD process to keep our Aiven [cloud list](https://docs.aiven.io/docs/platform/reference/list_of_clouds.html) always up-to-date. 


In this article, we reviewed the workflow code one segment at a time. The complete script looks like this:

File: ``.github/workflows/cloud-list.yaml``

```
name: Cloud - Create PR to update available list
on:
  schedule:
    - cron: "0 6 * * 2"
jobs:
  CloudList:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the repo
        uses: actions/checkout@v2
      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Update available cloud list
        run: make cloud-list
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          commit-message: Cloud - Update available list
          title: Cloud - Update available list
          body: Cloud - Update available list
          base: main
          labels: automated-pr, Clouds & Regions
          branch: cloud-update-advanced-params
          delete-branch: true
```

## Read more

Besides periodically running a workflow, you can also add the option to trigger your workflow manually. You can check out this [GitHub Actions manual](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/) with all the information.


If what you have in mind for your pull request looks different, you can check out more [examples](https://github.com/peter-evans/create-pull-request/blob/main/docs/examples.md) on how to customize the ``create-pull-request`` actions to fit your use case.

Here you can find more [examples](https://github.com/aiven/devportal/pull/945/files) to learn how to use GitHub Actions to open pull requests for you. Have fun automating your pull requests ⚙️!

---
I'm a Cloud Engineer at Nordcloud.