DEV Community

Cover image for Streamlining Development Workflow: Automating Tasks with GitHub Actions
Lucas Andrade
Lucas Andrade

Posted on

Streamlining Development Workflow: Automating Tasks with GitHub Actions

Github Actions Icon

During the entire development process, from code to deployment, we encounter many repeated processes: updating snapshots, running unit tests, deploying to staging, completing production deployments... all these types of processes will inevitably become part of every task developed, without exception.

And like everything in life, it doesn't make sense to do something repetitive when you can automate it. Moreover, it doesn't make sense to work on improving other people's experiences without making our own lives easier, saving our time.

There are MANY tools that help automate your development process (a practice known as CI/CD), and one of them is directly tied to the main platform for those who work with software: GitHub.

Therefore, in this article, we will learn a bit more about GitHub Actions, so we can create, customize, and share automated workflows directly in the repository.

What is GitHub Actions?

GitHub Actions is a CI/CD service provided by GitHub. It allows you to automate various tasks in software development, such as testing, building, deployment, and much more. GitHub Actions is event-based, where each action is triggered by specific events, such as code pushes, pull request creations, or predefined schedules.

CI/CD interface

In this example above, we have an Express project with a MongoDB database. Here, as soon as we open a pull request, the pipeline defined with the help of GitHub Actions will run all the tests created in the API, in different versions of Node and the MongoDB driver (first and second number in parentheses, respectively).

Workflows

Workflows are the main units of automation in GitHub Actions. They are defined through YAML files that can be configured to respond to specific events or execute schedules. The file specifies the actions to be executed, in which environment, and in what order. These workflows can be easily configured and customized to meet the specific needs of a project.

Workflow example

Actions

Actions are the reusable components that make up a workflow. They are the fundamental building blocks of GitHub Actions. Actions can be created by the community or the development team and shared with other users. There are pre-made actions available on the GitHub Marketplace, as well as other custom actions that can be developed internally. Actions can be combined to create complex and customized workflows.

*Creating Our Workflow
*

  • In a project, let's start by going to the "Actions" tab and clicking on "set up a workflow yourself";

Github Actions Interface

  • Now, we can define the details of the YAML file on the next screen, that is, the definition of our workflow. On the right, we can see that GitHub Actions has a marketplace with several well-known and pre-made workflows (consider using this option to find a useful one and enhance it);

Github Actions Interface

  • We can define the events that will trigger the workflow. For example, to trigger the workflow whenever a push occurs on the main branch, we can do something like this:
on:
  push:
    branches:
      - main
Enter fullscreen mode Exit fullscreen mode

Then, we add the steps that will be executed in the workflow. For example, let's add a step to install the project's dependencies and another to run the tests. In this case, we would do:

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test
Enter fullscreen mode Exit fullscreen mode

Where:

  • jobs defines the tasks to be executed in the workflow. You can have multiple jobs within a workflow, allowing different actions to be executed simultaneously;
  • build is the name given to the job. You can give a descriptive name to each job in your workflow, such as "build", "test", "deploy", among others;
  • runs-on specifies the environment in which the job will be executed. In this example, we are using "ubuntu-latest", which is an Ubuntu operating system image in the most recent environment available on GitHub Actions. There are other options available, such as Windows and macOS;
  • steps defines the individual steps that will be executed within the job;
  • name is the name given to each step. It serves to identify the step in the workflow execution log, making it easier to understand what happens at each step;
  • uses specifies a marketplace action that will be used in the step. In the given example, we are using the actions/checkout@v2 action, which allows checking out the repository's source code; run executes commands or scripts in the step. In the given example, we are using the npm install command to install the project's dependencies and npm test to run the tests.

The definition of the YAML file can also be done directly in your development environment; I just exemplified it with the more user-friendly approach.

GitHub Actions usage examples

The test example is the most basic, but there are countless ways to leverage the power of GitHub Actions to automate tasks in a development workflow. Some common examples include:

  • Continuous deployment to a production environment after a pull request approval;
  • Automatic generation of documentation from the source code;
  • Notifying the team about changes or issues in the repository;
  • Performing routine tasks such as database cleaning or backup creation.

Github Actions Pros

  • Seamless integration with GitHub: As a native GitHub feature, GitHub Actions integrates perfectly with the existing workflow and allows direct automation in the repository;
  • Flexible configuration: Workflows can be easily configured and customized to fit the specific requirements of the project;
  • Wide variety of actions: The GitHub Marketplace has a wide range of pre-made actions that can be used, covering various technologies and use cases;
  • Efficient collaboration: Automated workflows facilitate collaboration among team members.

Simple, right? Did you like the tip? Leave your suggestion or comment and see you next time!

Top comments (1)

Collapse
 
akshay_navale_2003 profile image
Akshay Navale

Helpful Thankyou!!