An Introduction to GitHub Actions for CI/CD

An Introduction to GitHub Actions for CI/CD

A beginner's guide to GitHub Actions. Learn the core concepts of workflows, events, jobs, and steps to automate your build, test, and deployment pipelines directly within your GitHub repository.

In modern software development, automating the process of building, testing, and deploying your code is essential. This practice, known as Continuous Integration and Continuous Deployment (CI/CD), ensures that your code is always in a deployable state and that you can release new features to your users quickly and reliably.

While there are many CI/CD tools available, GitHub Actions has rapidly become one of the most popular choices because it's built directly into the platform that most developers already use every day: GitHub.

What is GitHub Actions?

GitHub Actions is an automation platform that allows you to automate your software development workflows. You can create workflows that build, test, and deploy your code right from your GitHub repository. You can also use it to automate other tasks, like labeling pull requests or sending notifications.

Core Concepts of GitHub Actions

To understand GitHub Actions, you need to know a few key terms.

  1. Workflow: A workflow is an automated process that you define in a YAML file. It's made up of one or more jobs and is triggered by an event.

  2. Event: An event is a specific activity in your repository that triggers a workflow run. Common events include push (when code is pushed to a branch), pull_request (when a pull request is opened), or schedule (to run a workflow at a specific time).

  3. Job: A job is a set of steps that execute on a runner. By default, jobs run in parallel. You can also configure jobs to depend on each other.

  4. Runner: A runner is a server that runs your jobs. GitHub provides hosted runners for Linux, Windows, and macOS. You can also host your own self-hosted runners.

  5. Step: A step is an individual task that can run commands or an action.

  6. Action: An action is a reusable piece of code that you can use in your workflow. There is a rich marketplace of actions created by the community for common tasks, like checking out code, setting up a specific version of a language, or deploying to a cloud provider.

Your First Workflow: A Simple CI Pipeline

Let's create a simple CI workflow for a .NET application that builds and tests the code whenever a change is pushed to the main branch.

To create a workflow, you create a YAML file in the .github/workflows directory of your repository.

.github/workflows/dotnet-ci.yml

name: .NET CI

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

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.x'

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --no-restore

    - name: Test
      run: dotnet test --no-build --verbosity normal

Let's break this down:

  • name: The name of the workflow, which will be displayed in the GitHub UI.
  • on: Defines the events that trigger the workflow. In this case, it runs on any push or pull_request to the main branch.
  • jobs: Defines the jobs in the workflow. We have one job named build.
  • runs-on: ubuntu-latest: Specifies that this job will run on a GitHub-hosted runner using the latest version of Ubuntu.
  • steps: This is the sequence of steps for the build job.
    • uses: actions/checkout@v2: This step uses a pre-built action from the marketplace to check out the code from your repository onto the runner.
    • uses: actions/setup-dotnet@v1: This step uses another action to set up a specific version of the .NET SDK.
    • run: dotnet restore: This step runs a command-line command to restore the project's dependencies.
    • The build and test steps run the standard dotnet CLI commands.

With this file in your repository, GitHub will automatically run this workflow every time you push a new commit, giving you immediate feedback on whether your changes have broken the build or any tests.

Conclusion

GitHub Actions provides a powerful and convenient way to automate your development workflows. By integrating CI/CD directly into your repository, it lowers the barrier to entry for automation and makes it easy to build, test, and deploy your code with confidence. The rich ecosystem of community-built actions means that you can often find a pre-built solution for almost any task, allowing you to build complex pipelines with minimal effort.