DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Automating Django Deployments: Integrating CI/CD With GitHub Actions and Heroku
  • Continuous Integration for iOS and macOS: Low-Code Self-Hosted Runner for Xcode Project Automation
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions
  • DIY DevOps, CI, and CD with GitHub, Docker and a VPS

Trending

  • Types of Data Breaches in Today’s World
  • AI-Driven API and Microservice Architecture Design for Cloud
  • The Future of Agile Roles: The Future of Agility
  • Why You Should Move From Monolith to Microservices
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Mastering GitHub Actions: A Complete Guide to CI/CD With Docker, Kubernetes, and KIND

Mastering GitHub Actions: A Complete Guide to CI/CD With Docker, Kubernetes, and KIND

"Mastering GitHub Actions" is a comprehensive guide on implementing CI/CD using GitHub Actions, with detailed steps for integrating Docker, Kubernetes, and KIND.

By 
Rajesh Gheware user avatar
Rajesh Gheware
·
Jan. 16, 24 · Opinion
Like (1)
Save
Tweet
Share
2.0K Views

Join the DZone community and get the full member experience.

Join For Free

In the ever-evolving landscape of software development, continuous integration and continuous deployment (CI/CD) are critical for rapid and reliable software delivery. GitHub Actions is a powerful tool that automates your software workflows, allowing for faster and more efficient processes. In this article, we'll explore how to implement GitHub Actions using a real-world Python application, weather-py, as an example.

Prerequisites

  • A basic understanding of Git and GitHub.
  • Familiarity with Docker and Kubernetes.
  • Access to the weather-py GitHub repository here.

Step 1: Understanding the Application

Before diving into GitHub Actions, let's understand our application:

  • Repository: weather-py, available at the provided link.
  • Dockerfile: Specifies the environment for running the app. View it here.
  • Application Description: Found in the README here.
  • Kubernetes Deployment Manifest: Outlines how the app is deployed in Kubernetes. Available here.

Step 2: Setting up GitHub Actions

  1. Create a Workflow File: In your repository, navigate to .github/workflows. Create a new file named ci.yml. You can view an example here.
  2. Define Workflow Triggers: Specify when the workflow should run. Common triggers are push or pull_request events.
on: [push, pull_request]


Step 3: Configuring Jobs

Setup the Job Environment: Define jobs and the operating system.

jobs:
  build:
    runs-on: ubuntu-latest


Define Steps for CI: Steps include checking out the code, setting up Python, building the Docker image, etc.

steps:
- uses: actions/checkout@v2
- name: Set up Python
  uses: actions/setup-python@v2
  with:
    python-version: '3.x'
- name: Build and Push Docker image
  run: |
    docker build -t weather-app .
    docker push weather-app


Step 4: Integrating With Docker and Kubernetes

  1. Docker Integration: Ensure the Dockerfile is properly set up for building the image. Add steps in your workflow to build and push the Docker image to a registry.
  2. Kubernetes Deployment: Use the deployment.yaml to define how your application is deployed in Kubernetes. Add steps in your workflow for deploying to Kubernetes, which might include setting up kubectl, applying the manifest, etc.

Step 5: Testing With KIND and Docker Hub Integration

Testing is a crucial part of any CI/CD pipeline, ensuring that your application performs as expected before it reaches production. KIND (Kubernetes IN Docker) is a tool for running local Kubernetes clusters using Docker container nodes, which is ideal for testing. Here’s how to integrate KIND-based testing in your GitHub Actions workflow:

Setting Up KIND Cluster

Install KIND: In your workflow, add a step to install KIND. This creates a local Kubernetes cluster for testing.

- name: Install KIND
  run: |
    curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-$(uname)-amd64
    chmod +x ./kind
    ./kind create cluster


Deploy to KIND Cluster: Use the Kubernetes manifest (deployment.yaml) to deploy your application to the KIND cluster.

- name: Deploy to KIND
  run: |
    kubectl apply -f deployment.yaml


Run Tests: Implement your testing strategy, whether it’s integration tests, end-to-end tests, or any other form of automated testing.

Docker Hub Integration

Using Docker Hub for storing Docker images is a common practice. Ensure to personalize the workflow to push the Docker image to your Docker Hub account.

  1. Modify Docker Image Tag: Change the image tag in your Dockerfile and Kubernetes manifest to reflect your Docker Hub username. In ci.yml, replace weather-app with <your-docker-hub-username>/weather-app. Also, make this change in your deployment.yaml file to pull the correct image.
  2. Add Docker Hub Credentials: Securely add your Docker Hub credentials to your GitHub project's settings. Go to your GitHub repository settings.Navigate to Secrets and add two new secrets: DOCKER_USERNAME and DOCKER_PASSWORD with your Docker Hub credentials. In your ci.yml, use these credentials to log in and push the image to Docker Hub.
- name: Login to Docker Hub
  run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Push to Docker Hub
  run: |
    docker tag weather-app <your-docker-hub-username>/weather-app
    docker push <your-docker-hub-username>/weather-app


Testing with a KIND cluster in GitHub Actions provides a reliable, isolated environment mimicking a real Kubernetes cluster. This setup, combined with Docker Hub integration, forms a robust foundation for your CI/CD pipeline, ensuring that your application is thoroughly tested before deployment.

Remember to modify your workflow file (ci.yml) and Kubernetes manifest (deployment.yaml) with your Docker Hub username and update the GitHub Secrets with your Docker Hub credentials for a smooth CI/CD process.

Conclusion

Implementing CI/CD with GitHub Actions enhances your development workflow by automating integration and deployment processes. By leveraging this powerful tool, teams can achieve faster, more reliable software releases.

Continuous Learning and Improvement

  • Experiment with different triggers and actions.
  • Continuously monitor and optimize your workflows for better performance and efficiency.

Further Exploration

  • Explore advanced GitHub Actions features like caching dependencies or setting up matrix builds for different environments.
  • Integrate with other tools and services to further streamline your CI/CD pipeline.

Sharing Knowledge and Insights

  • Share your learnings and improvements with your team and the broader community.
  • Contribute to open-source projects or write about your experiences to help others learn.

Remember, the key to effective CI/CD with GitHub Actions lies in continuous experimentation and learning. Stay curious and keep innovating!

GitHub Continuous Integration/Deployment

Published at DZone with permission of Rajesh Gheware. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Automating Django Deployments: Integrating CI/CD With GitHub Actions and Heroku
  • Continuous Integration for iOS and macOS: Low-Code Self-Hosted Runner for Xcode Project Automation
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions
  • DIY DevOps, CI, and CD with GitHub, Docker and a VPS

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: