How to automate a pull request on Github

Luan Castheloge
3 min readMay 26, 2020

--

My GitHub guide at the development process.

Nowadays, remote work is becoming common and normal (thank god!). But neither every development team knows how to keep the communication active and mainly clear about the tasks projects. I think it is time for you to learn that Github can be more than a code version.

I, as a big fan of remote work, organization, and automation will give you a tip that may help.

Secrets

Encrypted secrets allow you to store sensitive information, such as access tokens, in your repository, or organization. So yes, you can create secret variables for each repository and use them. We can talk more about it in another article.

To set up a secret, go to your Repository Settings page, then select Secrets. When you create a secret you must set a name and the value of your secret. You’ll not be able to recover this value (not in an easy way), pick your name clear and explanatory.

Just in case, there is a part of Github API that you can manage all your secrets by request. If you would like to know more, click here.

Here, we will focus on using them inside the GitHub workflow. And we will need our GitHub credentials (with secrets) to create an automatization.

Github Actions

It is one of my favorite Github features, with Actions we can make it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

For being created by Github, the Actions understand every GitHub event: push, merge, tag a label, create a branch, manage your pull request, and a lot of things. Yeah, amazing indeed.

The great thing is all actions are plug and play, like components. You just have to checkout and will be ready to use it.

Create the automation

Let’s create a simple action. This action will tag a pull request. For example, when we create a pull request and changed a file inside a specific folder our action must triage and set a label int this pull request.

  • Go to a repository and create a file, called labeler.yml, inside the .github/ folder. Place this code:
bug: example/*

bug: is the name of an existent label inside your repo.

example/: is which folder will reflect the changes and activate the automation. For example, if there is a file inside the example folder and your PR change then, our rules will be applied.

  • Now you need to create a workflow that use your labeler.yml. So create a file inside .github/workflows/ called labeler.yml and place this:
  • Now you can create a folder named example;
  • Create a markdown file inside the example folder called test.md, insert something and save.
  • Commit and push everything
  • Edit test.md, commit adding a new branch.
  • Open a pull request with the new branch
  • See the magic happens.

Note at line 13, we use secrets to grant access and allow the automation tag a label. All GitHub account has this secret, even if it doesn’t appear in your secrets section. As you can see, the syntax to use secrets in a workflow is:

${{secrets.NAME_OF_YOUR_SECRET}}

I hope that you guys realize it is just the tip of the iceberg. You can deploy, test with jest, format code with prettier, and others great things. If you would like to see more articles about this, clap and comment below.
Bye :)

--

--

Luan Castheloge
Luan Castheloge

Written by Luan Castheloge

Front-end engineer in activity. Data enthusiast in stand-by.

No responses yet