DEV Community

Cover image for Getting Started With Github Actions
Urmalveer Singh
Urmalveer Singh

Posted on

Getting Started With Github Actions

In the fast-paced world of software development, being able to automate tasks servers as a great advantage. Tasks such as building, testing and deploying can be automated by setting up a continuous integration and delivery (CI/CD) pipeline. There are many CI/CD tools out there but one is built in right into our favorite Github and yes you guessed it right... it's Github Actions.

Github Actions makes it breeze to build a secure and reliable CI/CD pipeline allowing you to focus more on the actual development of the software. In this article We are going to understand basics of Github Actions and also try to automate testing for a nodejs app.

Understanding Github Actions

GitHub Actions is a CI/CD platform that allows you to create workflows that build and test every pull request to your repository, or deploy merged pull requests to production. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule. The components of GitHub Actions include workflows, actions, events, jobs, runners and steps.

workflows

At the core of GitHub Actions are workflows, which define the automation process for your repository. They include various actions.

Actions

Actions are what they sound like. They are reusable units of work within a workflow. They are individual tasks or steps that can be combined to create a custom automation sequence. GitHub provides a marketplace of pre-built actions, and you can also create your own custom actions to suit your specific needs.

Events

Events acts as triggers for workflows. They determine when a workflow will run. For example on a push to specific repository, pull requests, etc.

Jobs

Workflows consist of one or more jobs. Each job represents a unit of work that runs on a specific runner, which can be a virtual machine, a container, or a hosted runner provided by GitHub.

runners

Runners are the execution environments for jobs in GitHub Actions. You can use GitHub-hosted runners, which are provided by GitHub, or set up your own self-hosted runners for more specialized tasks.

steps

Steps are the individual tasks within a job that define the actions to be executed. Each job can consist of one or more steps, and these steps are executed sequentially.

Now that we have basic understanding of Github Actions and its components. Lets try to build a workflow which triggers on a push or pull requests and runs tests for our nodejs app.

Creating A Workflow

Pre-Requisites

  • A github repository
  • Nodejs app (code) in the repository
  • Tests written for the nodejs code

Further steps assumes that all pre-requisites are met.

To create a Github workflow follow these steps

  • Create github/workflows directory in root of your folder
  • Create a .yml file in this directory. You can name this file anything such as ci.yml
  • Copy the following code into ci.yml file
name: Node.js CI/CD

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2 # This allows you to perform actions on the code in repository

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: 18 # You can specify your desired Node.js version here

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test

Enter fullscreen mode Exit fullscreen mode

Thats it. You have created a simple workflow which will trigger on a push or pull request to master branch of your repository. The workflow above runs on a Github-hosted ubuntu runner.

You can check out more on what each line in the workflow means and understand syntax at Github Actions docs

Triggering Our Workflow

Now that we have created our workflow to automate testing lets try to trigger it by pushing code into repository.

  • Go to the Actions page of your repository and you will see the list of all your workflows

actions page

  • Make changes to the code and push it to the github using git push to trigger the workflow. You will see that workflow has been triggered and is performing actions specified in ci.yml file

workflow running

  • You can click on each run and see the actions and their results

workflow complete

🎉 Voilà! You have successfully automated your testing and deployed a workflow using Github Actions.

Conclusion

As you have seen it is super easy to automate tasks such as testing using Github Actions. You should now have basic understanding of Github Actions and ready to incorporate this knowledge in future projects to make your coding journey a bit automated and easy. I hope you liked the article. Stay tuned and Happy Coding 🌟.

Top comments (0)