Choose Language

Apply โฑ 33 min

GitHub Actions Tutorial - Basic Concepts and CI/CD Pipeline with Docker

What You Will Learn

  • Automate developer workflows using GitHub Actions
  • Create a Continuous Integration (CI) pipeline for a Java Gradle project
  • Build and push a Docker image to a private Docker repository

Key Concepts

GitHub Actions is a platform to automate developer workflows, not just a CI/CD tool. It listens to events in your repository and triggers a chain of actions or a combination of actions, known as a workflow. A workflow consists of jobs, which group a set of actions that will be executed. Each action is a small task that can be triggered on an event, such as checking out code, setting up a Java environment, or building a Docker image.

Code Examples

name: Java Gradle Workflow
on:
  push:
    branches:
      - master
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

This code snippet defines a basic GitHub Actions workflow for a Java Gradle project, triggering on push events to the master branch.

- name: Build and push Docker image
  uses: docker/build-push-action@v2
  with:
    context: .
    push: true
    tags: ${{ secrets.DOCKER_USERNAME }}/my-java-app:latest
    credentials: ${{ secrets.DOCKER_CREDENTIALS }}

This code snippet uses the docker/build-push-action to build and push a Docker image to a private Docker repository.

Lesson Summary

In this lesson, we learned how to automate developer workflows using GitHub Actions. We started by understanding the basic concepts of GitHub Actions, including events, actions, and workflows. We then created a Continuous Integration (CI) pipeline for a Java Gradle project, which included checking out code, setting up a Java environment, building the project, and pushing a Docker image to a private Docker repository. We used pre-defined actions, such as actions/checkout and docker/build-push-action, to simplify the workflow. We also learned how to store secrets, such as Docker credentials, in the GitHub repository settings.

Practice Exercise

Create a new GitHub repository and add a Java Gradle project to it. Create a new GitHub Actions workflow that triggers on push events to the master branch and builds the project using Gradle. Use the actions/checkout action to check out the code and the gradle/build action to build the project.

What Is Next

In the next lesson, we will learn how to deploy a Docker image to a cloud or Kubernetes environment using GitHub Actions. We will also explore more advanced topics, such as testing and building with Node.js, and using multiple operating systems in a single workflow.