DEV Community

Reaper
Reaper

Posted on

Github actions 101

Introduction

GitHub Actions is one a feature that enables developers to create custom workflows and automate various tasks such as building, testing, and deploying code. In this blog, we'll take a closer look at GitHub Actions and how it can be used to automate your software development workflows.

What are workflows

A workflow is a set of automated steps that can be triggered based on certain events, for example

  • a code push
  • pull request
  • scheduled time

A workflow can contain one or more jobs, each of which contains one or more steps. Each step is a set of instructions that perform a specific action, such as running a command, calling an API, or deploying code.

Usually these configuration files are in the following

  • json
  • xml
  • YAML

Getting started

Actions can be created using YAML files that define the workflow, jobs, and steps. These YAML files can be stored in a repository alongside the code, making it easy to version control and collaborate on workflows.

To get started with GitHub Actions, you'll need a GitHub account and a repository. Once you have a repository, you can create a new workflow by creating a YAML file in the .github/workflows directory of your repository. This file should define the workflow, jobs, and steps that make up your action.

An example of a simple workflow that runs a build and tests a Node.js application:

name: Node.js CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14.x'
    - name: Install dependencies
      run: npm install
    - name: Run tests
      run: npm test
Enter fullscreen mode Exit fullscreen mode

Lets break down this example down!
The workflow is named Node.js CI and is triggered when code is pushed to or a pull request is created against the main branch. The workflow contains one job, named build-and-test, which runs on an Ubuntu-based runner. The job contains four steps, which checkout the code, install Node.js, install dependencies, and run tests.

Using Actions from the Marketplace

GitHub Actions can also be created and shared by other developers in the GitHub Marketplace. The Marketplace contains a wide range of actions that can be used to automate various tasks, including building and testing code, deploying applications, and more.

To use an action from the Marketplace, you can simply reference it in your workflow YAML file using the uses keyword.

For example, to use the docker/build-push-action action to build and push a Docker image, you can add the following step to your workflow:

- name: Build and push Docker image
  uses: docker/build-push-action@v2
  with:
    context: .
    push: true
    tags: my-image:latest
Enter fullscreen mode Exit fullscreen mode

Conclusion

GitHub Actions is a great tool for increasing productivity and streamlining software development workflows. Whether you're working on a small project or a large-scale enterprise application, GitHub Actions can help you automate repetitive tasks and focus on building high-quality code.

Top comments (0)