Using GitHub Actions for CI/CD Automation

Tutorial 4 of 5

Introduction

Goal

In this tutorial, our goal is to learn how to use GitHub Actions for Continuous Integration and Continuous Deployment (CI/CD) automation. This will enable us to automate and streamline the software development process directly in our GitHub repository.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the basics of GitHub Actions
- Set up a workflow in GitHub Actions
- Use GitHub Actions for CI/CD automation

Prerequisites

  • Basic knowledge of Git and GitHub
  • A GitHub account
  • Familiarity with software development workflows

Step-by-Step Guide

Understanding GitHub Actions

GitHub Actions is a feature in GitHub that allows you to automate your software development workflows. It allows you to write individual tasks, called "actions", and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any project on GitHub.

Setting Up a Workflow

To set up a workflow in GitHub Actions, you need to create a YAML file in the .github/workflows directory of your repository. The name of the YAML file will be the name of your workflow.

Here's an example of a simple GitHub Actions workflow:

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Run a one-line script
      run: echo Hello, world!

Code Examples

Example 1: Simple Workflow

The following code snippet is an example of a simple GitHub Actions workflow:

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Run a one-line script
      run: echo Hello, world!

Summary

In this tutorial, we've covered the basics of GitHub Actions, how to set up a workflow, and how to use GitHub Actions for CI/CD automation.

Next Steps

Continue learning about GitHub Actions by exploring more complex workflows and integrating with other tools such as Docker, AWS, and more.

Additional Resources

Practice Exercises

Exercise 1: Simple Workflow

Create a simple workflow that runs a script which prints "Hello, world!" whenever a push or pull request is made.

Exercise 2: Complex Workflow

Create a more complex workflow that builds a Docker image and pushes it to Docker Hub whenever a push is made.

Solutions

Solution 1: Simple Workflow

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Run a one-line script
      run: echo Hello, world!

Solution 2: Complex Workflow

name: Docker

on: [push]

jobs:

  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Login to DockerHub
      uses: docker/login-action@v1 
      with:
        username: ${{ secrets.DOCKER_HUB_USERNAME }}
        password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

    - name: Build and push
      uses: docker/build-push-action@v2
      with:
        context: .
        push: true
        tags: user/repo:tag

Note: Don't forget to replace user/repo:tag with your Docker Hub username, repository, and tag. You also need to add your Docker Hub username and access token to the GitHub secrets.