Automate your builds with GitHub Actions

Prerequisites

Complete all the previous sections of this guide, starting with Containerize a Ruby on Rails application. You must have a GitHub account and a Docker account to complete this section.

If you didn't create a GitHub repository for your project yet, it is time to do it. After creating the repository, don't forget to add a remote and ensure you can commit and push your code to GitHub.

  1. In your project's GitHub repository, open Settings, and go to Secrets and variables > Actions.

  2. Under the Variables tab, create a new Repository variable named DOCKER_USERNAME and your Docker ID as a value.

  3. Create a new Personal Access Token (PAT) for Docker Hub. You can name this token docker-tutorial. Make sure access permissions include Read and Write.

  4. Add the PAT as a Repository secret in your GitHub repository, with the name DOCKERHUB_TOKEN.

Overview

GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) automation tool built into GitHub. It allows you to define custom workflows for building, testing, and deploying your code when specific events occur (e.g., pushing code, creating a pull request, etc.). A workflow is a YAML-based automation script that defines a sequence of steps to be executed when triggered. Workflows are stored in the .github/workflows/ directory of a repository.

In this section, you'll learn how to set up and use GitHub Actions to build your Docker image as well as push it to Docker Hub. You will complete the following steps:

  1. Define the GitHub Actions workflow.
  2. Run the workflow.

1. Define the GitHub Actions workflow

You can create a GitHub Actions workflow by creating a YAML file in the .github/workflows/ directory of your repository. To do this use your favorite text editor or the GitHub web interface. The following steps show you how to create a workflow file using the GitHub web interface.

If you prefer to use the GitHub web interface, follow these steps:

  1. Go to your repository on GitHub and then select the Actions tab.

  2. Select set up a workflow yourself.

    This takes you to a page for creating a new GitHub Actions workflow file in your repository. By default, the file is created under .github/workflows/main.yml, let's change it name to build.yml.

If you prefer to use your text editor, create a new file named build.yml in the .github/workflows/ directory of your repository.

Add the following content to the file:

name: Build and push Docker image

on:
  push:
    branches:
      - main

jobs:
  build_and_push:
    runs-on: ubuntu-latest
    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          push: true
          tags: ${{ vars.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest

Each GitHub Actions workflow includes one or several jobs. Each job consists of steps. Each step can either run a set of commands or use already existing actions. The action above has three steps:

  1. Login to Docker Hub: Action logs in to Docker Hub using the Docker ID and Personal Access Token (PAT) you created earlier.

  2. Set up Docker Buildx: Action sets up Docker Buildx, a CLI plugin that extends the capabilities of the Docker CLI.

  3. Build and push: Action builds and pushes the Docker image to Docker Hub. The tags parameter specifies the image name and tag. The latest tag is used in this example.

2. Run the workflow

Let's commit the changes, push them to the main branch. In the workflow above, the trigger is set to push events on the main branch. This means that the workflow will run every time you push changes to the main branch. You can find more information about the workflow triggers here.

Go to the Actions tab of you GitHub repository. It displays the workflow. Selecting the workflow shows you the breakdown of all the steps.

When the workflow is complete, go to your repositories on Docker Hub. If you see the new repository in that list, it means the GitHub Actions workflow successfully pushed the image to Docker Hub.

Summary

In this section, you learned how to set up a GitHub Actions workflow for your Ruby on Rails application.

Related information:

Next steps

In the next section, you'll learn how you can develop your application using containers.