Mastering CI/CD Pipeline Automation with GitHub Action

Pratik GotePratik Gote
6 min read

GitHub Actions

Welcome back to our complete DevOps course! Today, we're going to talk about GitHub Actions. Can you believe it?

What is GitHub Actions?

GitHub Actions is a CI/CD solution that automates your build, test, and deployment pipeline. Integrated with GitHub, it’s convenient for developers using GitHub for version control.

You can create workflows triggered by events like code pushes, pull requests, or schedules. These workflows are defined using YAML files, allowing for customization and flexibility.

GitHub Actions features an extensive marketplace with pre-built actions from the community, saving you time. It also supports matrix builds to run tests across multiple environments simultaneously.

Overall, GitHub Actions is a powerful, integrated, and user-friendly tool for automating your software development lifecycle.rm.

How to write a GitHub Actions file

Writing a GitHub Actions file is relatively simple. You can create a .github/workflows folder in your repository and add a YAML file with your pipeline configuration. The YAML file follows a specific syntax, and you can use plugins to perform various tasks, such as checking out code, setting up a Python environment, and running tests.

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

name: My First GitHub Actions

on:
  push:
    branches:
      - main

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.8'
      - name: Install dependencies
        run: |
          python -m pip install -r requirements.txt
      - name: Run tests
        run: |
          python -m pytest

Advantages of GitHub Actions

One of the biggest advantages of GitHub Actions is its ease of use. You don't need to worry about hosting or maintaining a Jenkins instance, and you can focus on writing your pipeline configuration. GitHub Actions also provides a free execution environment for public projects, which can be a significant cost savings.

Comparison with Jenkins

GitHub Actions and Jenkins are both CI/CD solutions, but they have some key differences. GitHub Actions is scoped to the GitHub platform, so if you plan to move your project to a different platform in the future, Jenkins might be a better choice. On the other hand, GitHub Actions is easier to use and provides a free execution environment for public projects.

GitHub Actions Self-Hosted Runners & GitHub-Hosted Runner

In this Section, we'll explore the world of GitHub Actions and self-hosted runners. We'll discuss the differences between GitHub hosted runners and self-hosted runners, and walk through a step-by-step guide on how to set up a self-hosted runner on an AWS EC2 instance.

What is a GitHub-Hosted Runner?

A GitHub-hosted runner is a virtual machine (VM) provided by GitHub that runs your GitHub Actions workflows. When you use a GitHub-hosted runner, GitHub provisions a VM with a specific operating system (such as Ubuntu or Windows) and configuration, and runs your workflow on that VM.

  • Here are some key characteristics of GitHub-hosted runners:

    • Managed by GitHub: GitHub provisions and manages the VM, so you don't need to worry about setting up or maintaining the infrastructure.

    • Pre-configured environment: The VM comes with a pre-configured environment, including the operating system, dependencies, and tools, so you can focus on writing your workflow code.

    • Scalability: GitHub-hosted runners can scale to meet the demands of your workflow, so you don't need to worry about running out of resources.

    • Security: GitHub-hosted runners are secure by default, with features like encryption and access controls to protect your code and data.

What is a Self-Hosted Runner?

A self-hosted runner is a machine that you own and manage, which runs your GitHub Actions workflows.

In Jenkins, we have agents or worker nodes that execute our jobs. Similarly, in GitHub Actions, we have self-hosted runners that can be used to execute our CI/CD pipelines. But what exactly is a self-hosted runner?

A self-hosted runner is a machine that you own and manage, which can be an EC2 instance, a Docker container, or even your personal laptop. You can use this machine to execute your GitHub Actions pipeline, giving you more control and flexibility over the environment.

Why Use Self Hosted Runners?

There are three main reasons why you might want to use self-hosted runners:

  1. Private Projects: If you're working on a private project, you might not want to use GitHub's hosted runners, which are public and shared with other users.

  2. Custom Requirements: If your project requires specific dependencies or configurations that are not available on GitHub's hosted runners, you can use a self-hosted runner to meet those requirements.

  3. Security: With self-hosted runners, you have more control over the environment and can ensure that your code is executed in a secure and isolated environment.

Configuring a Self Hosted Runner

To configure a self-hosted runner, you'll need to create an EC2 instance (or use an existing one) and install the GitHub Actions runner software on it. You'll also need to configure the inbound and outbound traffic rules to allow communication between your EC2 instance and GitHub.

Here's a step-by-step guide to configuring a self-hosted runner:

  1. Launch an EC2 instance and select the desired operating system (e.g., Ubuntu).

  2. Configure the inbound and outbound traffic rules to allow communication between your EC2 instance and GitHub.

  3. Install the GitHub Actions runner software on your EC2 instance.

  4. Configure the runner to listen for jobs from GitHub.

Want to try this project at your end ? Fork the GitHub repository:

Using a Self Hosted Runner with GitHub Actions

Once you've configured your self-hosted runner, you can use it to execute your GitHub Actions pipeline. Here's an example of how to update your .yml file to use a self-hosted runner:

name: My GitHub Actions Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: self-hosted
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run script
        run: |
          python script.py

In this example, we're telling GitHub Actions to use a self-hosted runner to execute the pipeline.

Comparison with Jenkins

So, how does GitHub Actions compare to Jenkins? Well, if you're working on a public project, GitHub Actions is the clear winner, as it provides free servers and a simple, easy-to-use interface. However, if you're working on a private project, Jenkins might be a better option, as it provides more flexibility and control over the environment.

Conclusion

In this blog, we've covered the basics of GitHub Actions, including how to write a GitHub Actions file, the advantages of using GitHub Actions, and a comparison with Jenkins. We also delved into the world of self-hosted runners, discussing their benefits, configuration, and usage. By understanding these concepts, you can leverage GitHub Actions to automate your CI/CD pipeline effectively, whether you're working on public or private projects. Thanks for reading, and I'll see you in the next blog!


____________________________________________________________________________________

0
Subscribe to my newsletter

Read articles from Pratik Gote directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Gote
Pratik Gote

About Me πŸ‘‹ Greetings! I'm Pratik Gote, a dedicated software developer with a strong passion for staying at the forefront of technological advancements in the industry. Armed with a solid foundation in Computer Science, I excel in creating innovative solutions that push boundaries and deliver tangible results. What I Do I specialize in: Full-Stack Development: Crafting scalable applications using cutting-edge frameworks such as React, Vue.js, Node.js, and Django. πŸ’» Cloud Computing: Harnessing the capabilities of AWS, Azure, and Google Cloud to architect and deploy robust cloud-based solutions. ☁️ DevOps: Implementing CI/CD pipelines, Docker containerization, and Kubernetes orchestration to streamline development workflows. πŸ”§ AI and Machine Learning: Exploring the realms of artificial intelligence to develop intelligent applications that redefine user experiences. πŸ€– My Passion Technology is dynamic, and my commitment to continuous learning drives me to share insights and demystify complex concepts through my blog. I strive to: Demystify Emerging Technologies: Simplify intricate ideas into accessible content. πŸ“š Share Practical Insights: Offer real-world examples and tutorials on state-of-the-art tools and methodologies. πŸ› οΈ Engage with Fellow Enthusiasts: Foster a collaborative environment where innovation thrives. 🀝 Get in Touch I enjoy connecting with like-minded professionals and enthusiasts. Let's collaborate on shaping the future of technology together! Feel free to connect with me on LinkedIn : https://www.linkedin.com/in/pratik-gote-516b361b3/ or drop me an email at pratikgote69@gmail.com. Let's explore new horizons in technology! πŸš€