Automate testing and deployment (CI/CD) using GitHub Actions

Shweta Pardeshi
4 min readMar 24, 2023

--

Image Source: https://voltrondata.com/resources/new-contribution-to-github-actions-increases-devops-flexibility-performance

Overview

Continuous Integration and Continuous Delivery (CI/CD) has become critical components in modern software development practices. This allows developers to rapidly build, test, and deploy applications. One effective way to achieve this is through the use of GitHub Actions, a robust automation platform that can help automate your CI/CD pipeline and optimize your development workflow.

This tutorial will walk you through the process of configuring GitHub Actions for your project, providing you with the tools you need to streamline your development process and deploy your applications with ease.

Step 1: Create a Github Actions Workflow

A workflow is a set of steps that define how your application is built, tested, and deployed. You can either write your workflow file from scratch or can use a template present in the Actions tab in your GitHub repository.

Let’s create a new YAML (*.yml)file in the .github/workflows directory of your repository. Following this directory structure is essential. Here's an example workflow file:

name: CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'

Step 2: Define Your Workflow Steps

Once you have created your workflow, you can add the steps of your CI/CD pipeline. The YAML file specifies the name of the step, the commands to run, and any inputs that the step requires. For example, you might have steps for installing dependencies, building the application, running tests, and deploying the application to a production environment.

Here’s an example step that installs dependencies:

- name: Install dependencies
run: |
python -m venv env
source env/bin/activate
pip install -r requirements.txt

This step creates a virtual environment, activates it, and installs the dependencies listed in requirements.txt.

Here’s an example step that runs tests:

- name: Run tests
run: |
source env/bin/activate
pytest TEST_FOLDER_PATH

Step 3: Configure Your Workflow Triggers

GitHub Actions allows you to trigger workflows based on specific events. For example, you might want to trigger your workflow every time a new pull request is opened or every time a new commit is pushed to a particular branch.

You can configure your workflow triggers using the “on” keyword. By specifying the right triggers, you can ensure that your workflow is executed when and where it is needed. This will make the development process efficient. You can learn in-depth about workflow triggers here.

  1. Trigger on push to a specific branch:
on:
push:
branches: [develop, main]
paths:
- "PATH_TO_FOLDER/**"

2. Trigger on the pull request:

on:
pull_request:
branches:
- main

3. Trigger on a schedule:

on:
schedule:
- cron: '0 0 * * *'

jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Backup
run: echo "Backing up data..."

Step 4: Add Environmental Variables and GitHub secrets

If your application needs any environmental variables you can add them to your workflow file like this:

env:
ENV_VAR_1: "changeme"
ENV_VAR_2: "changeme"

If you need to use sensitive information such as API keys or access tokens in your GitHub Actions workflow, you should store them securely. To do this, you can use Github Secrets.

Here’s how to add a new secret:

  1. Go to your GitHub repository and click on the “Settings” tab.
  2. Click on “Secrets” in the left sidebar.
  3. Click on “New Repository Secret” and enter a name and value for your secret.
  4. Click on “Add Secret” to save it.

Once you’ve added a secret, you can reference it in your workflow using the ${{ secrets.SECRET_NAME }} syntax.

env:
ENV_VAR_1: ${{ secrets.ENV_VAR_1 }}
ENV_VAR_2: ${{ secrets.ENV_VAR_1 }}

Step 4: Use GitHub Actions to Deploy Your Application

Once you have defined your workflow, you can use Github Actions to deploy your application to a production environment. GitHub Actions supports various deployment strategies, for example deploying to a cloud provider like AWS or Azure or using a containerization platform like Docker.

To deploy your application, you can use one of the many deployment actions available in the GitHub Marketplace. These actions provide pre-built steps for deploying your application to different environments.

Here is an example of the deployment of a Python application using Docker containers and Kubernetes through GitHub Actions.

name: Deploy to Kubernetes
on:
push:
branches:
- main

env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DEPLOYMENT_NAME: flask-app
NAMESPACE: my-namespace

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Login to Docker registry
uses: docker/login-action@v1
with:
registry: <your-registry>
username: $DOCKER_USERNAME
password: $DOCKER_PASSWORD

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: <your-registry>/<your-image>:latest

- name: Apply K8s Files
uses: appleboy/kubectl-action@master
with:
args: apply -f deployment.yaml
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}

- name: Rollout Deployment
uses: appleboy/kubectl-action@master
with:
args: rollout restart deployment/$DEPLOYMENT_NAME -n $NAMESPACE
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}

In this workflow, you define a job called build-and-deploy that runs on a push to the main branch. The job checks out your code, logs in to your Docker registry, builds and pushes your Docker image docker/build-push-action. Then the job deploys your application to a Kubernetes cluster and rollout the deployment using the kubectl-action action.

To make this workflow work, you need to configure your secrets in the repository settings as mentioned before.

Conclusion

GitHub Actions is an incredibly effective tool for automating your CI/CD pipeline, enabling you to seamlessly define workflows that build, test, and deploy your applications. This powerful platform streamlines your deployment process, saves time, and reduces the risk of errors, allowing you to concentrate on developing high-quality code.

References

--

--

Shweta Pardeshi

Master's student at UCSD | Educative Author | 35k+ views on Medium | Analytics Vidhya Author | IIT Gandhinagar | https://www.buymeacoffee.com/shwetapar1