Building a CI/CD Ecosystem with Gitea and werf in Kubernetes

November 27, 2024, 4:25 pm
Giterministic CLI tool
DeliverySoftwareTools
Employees: 11-50
In the fast-paced world of software development, Continuous Integration and Continuous Delivery (CI/CD) are not just buzzwords; they are lifelines. They ensure that code changes are automatically tested and deployed, reducing the risk of errors and accelerating the release cycle. This article explores how to set up a robust CI/CD ecosystem using Gitea, a self-hosted Git service, and werf, an open-source tool designed for Kubernetes deployments.

### The Foundation: Gitea and werf

Gitea serves as the backbone of our CI/CD pipeline. It is lightweight, easy to set up, and offers a user-friendly interface for managing repositories. On the other hand, werf is a powerful tool that streamlines the process of building and deploying applications in Kubernetes. It acts as a bridge between your code and the Kubernetes cluster, ensuring that your applications are delivered efficiently.

### Setting Up Gitea

The first step is to install Gitea. This can be done on any server that meets the minimum requirements. Once installed, you can create an organization and set up repositories. For our example, we will create a simple "hello-world" repository. This repository will house our application code and configuration files.

After creating the repository, we need to add our application code. In this case, a basic HTML page will suffice. The simplicity of this example allows us to focus on the CI/CD process rather than the intricacies of application development.

### Introducing werf

With Gitea in place, we turn our attention to werf. This tool is essential for managing the lifecycle of our application in Kubernetes. It allows us to build Docker images, manage dependencies, and deploy applications seamlessly.

To get started with werf, we need to create a configuration file named `werf.yaml`. This file outlines how our application will be built and deployed. It specifies the base image, the files to include, and any commands to run during the build process. The beauty of werf lies in its ability to cache build stages, significantly speeding up the CI/CD pipeline.

### Creating the Helm Chart

Next, we need to prepare a Helm chart. Helm is a package manager for Kubernetes that simplifies the deployment of applications. The chart will define the resources our application needs, such as deployments, services, and ingress rules.

The structure of our Helm chart will include templates for deployment, service, and ingress configurations. Each of these templates will use placeholders that will be filled in during the deployment process. This allows for flexibility and reusability across different environments.

### Configuring Gitea Actions

With our application and Helm chart ready, we can now set up Gitea Actions. This feature allows us to automate our CI/CD pipeline directly within Gitea. The pipeline will consist of two main stages: build and deploy.

The build stage will trigger whenever a commit is pushed to the repository. During this stage, werf will build the Docker image based on the `werf.yaml` configuration. Once the image is built, it will be pushed to the container registry.

The deploy stage will follow the build stage. Here, werf will render the Helm chart and deploy the application to the Kubernetes cluster. This process is straightforward and can be customized based on the environment (development, staging, production).

### Managing Secrets and Access

Security is paramount in any CI/CD pipeline. To manage access to our container registry and Kubernetes cluster, we will create a service account in Kubernetes. This account will have the necessary permissions to pull images from the registry and deploy applications.

We will also generate a kubeconfig file, which allows our CI/CD runner to interact with the Kubernetes API. This file contains the credentials and endpoint information needed to authenticate and authorize requests.

### Testing the Pipeline

With everything set up, it’s time to test our CI/CD pipeline. We can push a change to our repository and observe the automated process in action. The build stage should trigger, creating a new Docker image. Following that, the deploy stage will render the Helm chart and deploy the application to the Kubernetes cluster.

### Conclusion

Setting up a CI/CD ecosystem with Gitea and werf in Kubernetes is a powerful way to streamline your development process. It allows for rapid iterations, automated testing, and seamless deployments. By leveraging these tools, teams can focus on writing code rather than managing infrastructure.

As we continue to embrace automation in software development, tools like Gitea and werf will play a crucial role in shaping the future of CI/CD. With the right setup, your team can achieve greater efficiency, reliability, and speed in delivering applications to users.

In the ever-evolving landscape of technology, staying ahead means embracing change. Gitea and werf offer a path forward, transforming how we build and deploy software. The journey may be complex, but the rewards are well worth the effort.