What are GitHub Actions?

What are GitHub Actions?

Understanding GitHub actions and writing your first GitHub action

ยท

4 min read

Hey everyone, If you are an open-source developer or a developer then you have probably heard and are familiar with GitHub. It offers many cool features which help you in your software development and management processes. One of those cool features in 'GitHub Actions'. So let's get into it.

๐Ÿค” What are GitHub actions?

image.png

According to GitHub Documentation:

It helps automate, customize, and execute your software development workflows right in your repository with GitHub Actions. Read more

So basically GitHub actions help you create a workflow and automate it in your repository itself. Workflow means steps you take while going building software and deploying it. Eg: When I get a pull request to my project, I run tests on it, I check if it's according to semantics, I check for any kinds of errors that might occur while deploying. Once done I merge it then update the version, re-build and deploy it. Now this seems to be a long process and takes a lot of time and energy out of me. What if there was a way to automate all these processes and cut it down to, I get a PR -> (They somehow gets checked with all sorts of things I want) -> I merge it -> (Somehow does all the re-build and deploy stuff). Here the 'somehow' work is done by GitHub actions.

You just have to make a .yml once and it will do all the tasks on every pull request and issue as you prefer. Also creating a .yml file is not a big task as it looks, there are lots of templates for different kinds of automation already available online. Simply copying and pasting them does the trick too (unless they have additional secrets etc to it).

Enough of theory and explanation. Let's get writing our first GitHub action.

โœ Writing your first GitHub action

So, by now I hope you have a clear understanding of what are GitHub actions and why are they so useful while building software. In this section, we will be writing our first and simple action. An action that welcomes and provides resources to people when they create a PR or issue. This action comes in handy as you possibly can't be only 24/7, but this will help your repo to be interactive to some extent.

๐Ÿ“‚ Creating a file:

You have to create a .yml file inside of a .github/workflows folder. (make sure the .github folder is in the root directory of your project.). You can name your action file anything you want. In this case, I will be naming it greetings.yml. Refer to the image below for a clear understanding of the folder structure. Folder structure

๐Ÿ›  Writing yml code

Copy and paste the following code into the greetings.yml file

name: Greetings

on: [pull_request, issues]

jobs:
  welcome:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v1
        - uses: EddieHubCommunity/gh-action-community/src/welcome@main
          with:
            github-token: ${{ secrets.GITHUB_TOKEN }}
            issue-message: '<h3>Hello ๐Ÿ‘‹, Thank you very much for raising an issue ๐Ÿ™Œ. The maintainers will get back to you soon for discussion over the issue!</h3>'
            pr-message: '<h3>Yeah! You did it ๐ŸŽ‰. Now, Relax ๐Ÿ˜‰ -> Grab a drink โ˜• -> And wait for the maintainers views on your contribution. Meanwhile you can discuss on other issues and solve them ๐Ÿ˜€</h3>'
            footer: 'If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our <a href="https://discord.gg/jvdcY2NkXa">Discord Server</a>- we help and encourage each other to contribute to open source little and often ๐Ÿค“ . Any questions let us know.'

The above code is pretty simple. We name the action using the name tag. Then we specify when should the action run. In this case, we want it to run on pull requests and issues hence mentioned in the on tag. Then we move ahead with jobs. You can add multiple jobs to action, you can also link jobs in each other to achieve the flow. We add the job name as a tag itself. Provide base os info in runs-on (To be honest, I generally just copy-paste this required stuff). Here we only have one job so we add the steps we need GitHub to follow after every pull request and issue. We link other deployed actions using uses property. And with that, we add messages for pull requests and issues in the form of key-value pair. And you are done ๐ŸŽ‰. Once this file is merged in the main or default branch your actions will start running.

You can create many such actions on your own. To know more about how you can create your own custom action and what all actions are offered by GitHub as a template can be found here

Thank you so much for reading ๐Ÿ’–

Like | Follow | Subscribe to the newsletter.

Catch me on my socials here: bio.link/kaiwalya

Did you find this article valuable?

Support Kaiwalya Koparkar by becoming a sponsor. Any amount is appreciated!

ย