From the course: Docker for Developers: Create and Manage Docker Containers

Adding a GitHub Actions workflow

- [Instructor] Automating Docker image builds and pushes with GitHub Actions is a game changer for development workflows. GitHub Actions is a CICD tool built into GitHub that lets you automate tasks like testing, building, and deploying your code based on events in your repository. Instead of building and pushing images manually, you can let GitHub handle it for you every time you create a pull request for a specific branch, such as the main branch. I'm looking at the cheat sheet in this branch for this video, 08_02. Here I have an example GitHub Workflow. GitHub workflows describe when and how your automation runs, such as on push, pull request, or a schedule. It can include one or more jobs made up of multiple steps. GitHub Actions is the platform or feature that runs workflows. A workflow is defined in a YML file, so let's copy this code and then create the workflow file. The location for our workflow is GitHub Workflows. This is the default location that'll trigger the workflow. Let's create a new file and name it Docker.yml, then paste the code from the cheat sheet. First, we're setting the name of the workflow. It'll show up on the Actions tab of your GitHub repository under this name. The on configuration tells GitHub to trigger the workflow only when a pull request is opened or updated, and the destination branch is main. Jobs defines a job called build and push. Jobs are group of steps that run together on a GitHub-hosted runner. Runs on specifies the virtual machine that will run this job. In this case, it's using the latest Ubuntu environment. The checkout code step pulls down your repository's code so the runner can use it to build the Docker image. Next, we have set up Docker Build X. Docker Build X is a Docker CLI plugin that extends the Docker build command. It provides more powerful and flexible way to build docker images, particularly for multi architecture support. The next step logs into Docker hub using the username and password stored in GitHub Secrets. This is required to push the image to your Docker Hub account. We'll create these secrets in a moment. And finally, build and push Docker image builds a Docker image using the Docker file in the root of the repository, which is represented by context and then period, then pushes it to Docker Hub. You need to update this command to use your own username. We're also setting the image tag version five, which should not exist in your image repository yet. That's it. Now you've created a GitHub workflow. In the next video, we'll create the GitHub Secrets needed to run this workflow.

Contents