MUO logo

Getting Started With GitHub Actions

The GitHub logo on a three-dimensional white tile.
No Attribution - Unsplash Link
Sign in to your MUO account

Defining workflows to automate tasks can vastly accelerate your software development. Many strategies exist to streamline workflows, with the choice often hinging on the specific tool and environment in use.

A popular solution that you can use to automate your development workflows is GitHub Actions. GitHub Actions allows for seamless integration and automation of software development processes. Using GitHub Actions, you can define, share, and automatically run custom workflows.

What Is GitHub Actions?

GitHub Actions
image credit: Ukeje Goodness

GitHub Actions is a flexible and scalable platform for automating tasks and workflows in software projects. It eliminates the need for complex external CI/CD systems by providing a centralized space to create, share, and reuse workflows.

Critical features of GitHub Actions include workflow automation, a vast ecosystem of pre-built actions, and collaboration with existing workflows. GitHub Actions provides customizability through variables, secrets, and inputs. It also offers cross-platform compatibility for various programming languages and operating systems.

GitHub Actions simplifies continuous integration and deployment and manages dependencies. It also ensures testing and quality assurance and streamlines continuous delivery and deployment.

It offers essential use cases like continuous integration and testing, automated code review and analysis, release automation, and cloud infrastructure provisioning. These features enhance development workflows and foster productivity and collaboration.

Getting Started With GitHub Actions

Before delving into the intricacies of GitHub Actions, you must enable it for your GitHub repository.

You can enable GitHub Actions for your repository by following these steps:

  1. Navigate to your repository on GitHub and click on the "Actions" tab in the repository menu.
  2. If this is your first time accessing GitHub Actions for the repository, you will get a prompt to set up a workflow. Choose an appropriate workflow template or create a new one from scratch.
    [画像:GItHub Actions tab Screenshot]
    image credit: Ukeje Goodness
  3. Setting up the workflow will enable GitHub Actions for the repository.

Understanding GitHub Actions: Keywords and Concepts

GitHub Actions revolves around several fundamental concepts. These concepts form the building blocks of its automation capabilities. You’ll need to understand the following keywords to make the most of GitHub Actions.

Workflows

Workflows are a crucial concept; they let you define custom automated processes. These automated processes run whenever specific events occur in your repository.

You’ll define workflows with YAML files and specify the jobs that they consist of.

Jobs

A workflow contains one or more jobs. Each job represents a unit of work that can run concurrently or sequentially with other jobs in a workflow. You’ll define jobs with a YAML file and you’ll typically define them as a series of steps.

Steps

Steps are the individual tasks or actions that make up a job. They represent the smallest unit of work within a workflow. Steps can perform a wide range of actions such as running commands, calling external APIs, or deploying code.

Actions

Actions are reusable units of code that encapsulate specific functionality. You can use actions across multiple workflows.

Actions include building code, deploying applications, sending notifications, or interacting with external services. There are three types of actions available:

  • Pre-built Actions: The actions created by GitHub or the community are available in the GitHub Actions Marketplace. You can use them in your workflows by specifying the action name and version in the uses field (e.g. uses: actions/checkout@v2).
  • Docker Container Actions: You can define actions using Docker containers. This allows you to encapsulate a set of commands or scripts in a container image and use it as an action. You can specify the Docker image in the uses field, just like a pre-built action (e.g. uses: docker://node:14).
  • Composite Run Steps: Composite run steps allow you to define custom actions directly within your workflow file. These steps can include a sequence of shell commands or reference external scripts. They help create small, reusable actions that are specific to your workflow.

Actions are the building blocks of GitHub Actions since they enable you to extend and customize your automation workflows.

Events

Events trigger workflows to start. Events can be specific actions, like repository pushes, pull request creation, or scheduled actions. GitHub provides a wide range of events that you can leverage to trigger your workflows based on the desired conditions.

Runners

Runners are virtual machines or containers that execute jobs within a workflow. GitHub provides hosted runners that support a variety of operating systems and environments. You can also set up self-hosted runners to meet specific requirements.

Creating Workflows With GitHub Actions

You’ll define workflows in a YAML (Yet Another Markup Language) file with YAML syntax. YAML provides a human-readable and easily understandable way of defining configurations and workflows.

Create a .github/workflows directory in your repository to host YAML files that represent your workflow.

Here’s the content of a simple workflow YAML file:

name: Go Workflow
on:
 push:
 branches:
 - main
 pull_request:
 branches:
 - main
jobs:
 build:
 runs-on: ubuntu-latest
 steps:
 - name: Checkout code
 uses: actions/checkout@v2
 - name: Set up Go
 uses: actions/setup-go@v2
 with:
 go-version: '1.16'
 - name: Build project
 run: go build ./...
 - name: Run tests
 run: go test ./...

This YAML program defines a workflow named "Go Workflow" for a Go project.

The workflow defines two events that trigger it: push and pull_request on the main branch. This means it will run whenever there’s a push or pull request to the main branch.

The build job runs on an ubuntu-latest runner, a GitHub-hosted virtual machine that you can specify to run the job.

The steps component contains a list of actions to run in order. In this case, the job first checks out the code using the actions/checkout action and then builds the project by running go build ./... and the tests by running the go test ./... command.

Once you have defined your workflow file, you can commit and push it to your repository. GitHub Actions detects and runs the new workflow whenever the specified events occur.

You can check your repository’s Actions tab for information about your GitHub Actions and Workflows.

[画像:The Actions tab after creating a workflow]
image credit: Ukeje Goodness

Deploy React Applications to Firebase With GitHub Actions

There’s a lot you can do with GitHub Actions. For example, it’s incredibly useful for automatically deploying React apps to Firebase.

You can set up a CI/CD pipeline that builds, tests, and deploys your React application to Firebase whenever you push changes to your repository. Integrating GitHub Actions and Firebase can help you with efficient and reliable deployments.

AltStyle によって変換されたページ (->オリジナル) /