This documentation provides a detailed step-by-step guide on implementing the CI/CD workflow for the fluxcd/flux2-kustomize-helm-example project. The CI/CD setup ensures that any modifications to Kubernetes manifests or the repository structure are verified prior to acceptance into the main branch.

Prerequisites

Before proceeding with the CI/CD workflow, ensure that you have the following:

  1. A GitHub account with access to fork the repository.
  2. A Kubernetes cluster for deployment (e.g., Kind, EKS).
  3. kubectl, flux, kubeconform, and kustomize installed locally.

Setting Up GitHub Actions Workflows

This repository contains two essential GitHub Actions workflows that facilitate the CI process:

  1. Test Workflow: Validates Kubernetes manifests and Kustomize overlays using kubeconform.
  2. End-to-End (e2e) Workflow: Spins up a Kubernetes cluster in CI and tests the staging setup with Flux running in Kubernetes Kind.

CI Configuration

To implement the CI workflows effectively, you need to set up these actions in your .github/workflows directory.

Example: CI Test Workflow

Create a file named ci-test.yaml in .github/workflows directory with the following content:

name: CI

on:
  pull_request:
    branches:
      - main

jobs:
  validate:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '1.16'

    - name: Install dependencies
      run: |
        go get k8s.io/kubelet@latest
        go get sigs.k8s.io/controller-runtime@latest

    - name: Validate Kustomize overlays
      run: ./scripts/validate.sh

Running the CI Workflow

Upon creating a pull request to the main branch, the defined workflows will automatically trigger. The CI pipeline will clone the repository, install necessary dependencies, and validate the Kustomize overlays using the provided validate.sh script.

Example: End-to-End Workflow

Create another file named ci-e2e.yaml in the same directory with the following content:

name: E2E Test

on:
  pull_request:
    branches:
      - main

jobs:
  e2e:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Kind
      uses: engineerd/[email protected]

    - name: Create Kind cluster
      run: kind create cluster

    - name: Install Flux
      run: |
        flux install

    - name: Run e2e tests
      run: |
        # Add your e2e test commands here

This workflow sets up a Kubernetes cluster using Kind and installs Flux, allowing for the execution of end-to-end tests.

Validating Changes

The validation script validate.sh, located in the scripts directory, performs critical checks on the Kustomize overlays and Kubernetes manifests. The script should be executed both locally and as part of the CI check to ensure the integrity of the changes before merging.

Example: validate.sh Script

#!/usr/bin/env bash

set -o errexit
set -o pipefail

# Validate Kustomize overlays
echo "INFO - Validating kustomize overlays"
find . -type f -name kustomization.yaml -print0 | while IFS= read -r -d $'\0' file; do
    echo "INFO - Validating kustomization ${file/%kustomization.yaml}"
    kustomize build "${file/%kustomization.yaml}" | kubeconform
    if [[ ${PIPESTATUS[0]} != 0 ]]; then
      exit 1
    fi
done

Conclusion

The provided CI/CD setup ensures that any changes to the Kubernetes manifests are rigorously validated before deployment. The integration of GitHub Actions allows for seamless testing and validation of the repository’s contents, thereby automating the deployment process and ensuring reliable application delivery.

If CI/CD is not yet set up, consider following the steps above, starting with setting up GitHub Actions workflows, and ensure the validation script operates as intended in your local and CI environments.

References

  • Source for information: README.md, scripts/validate.sh.