helm , jenkins and argo CD example - unix1998/technical_notes GitHub Wiki

Helm charts are primarily used for defining, installing, and managing Kubernetes applications. They do not handle the process of building your Java application with Maven or Gradle, nor do they handle the building and pushing of Docker images to an image registry. These tasks are typically handled by CI tools such as Jenkins, GitLab CI, or GitHub Actions.

Here’s how you can integrate Helm with a CI/CD pipeline that includes these tasks:

CI/CD Pipeline with Helm and a CI Tool:

  1. Continuous Integration (CI) Process:

    • Code Changes and Commit:

      • You make changes to your Java code and commit them to the GitHub repository.
    • CI Tool (e.g., Jenkins, GitLab CI, GitHub Actions):

      • The CI tool detects the code changes and starts the build process.
      • The application is built (e.g., using Maven or Gradle for a Java Spring Boot application).
      • The built artifact (e.g., a JAR or WAR file) is used to create a Docker image.
      • The Docker image is tagged (often using a unique identifier such as the Git commit SHA or a version number).
      • The Docker image is pushed to a container registry (e.g., Docker Hub, AWS ECR, Google Container Registry).
  2. Updating Helm Chart Values:

    • CI Tool:
      • After pushing the Docker image to the registry, the CI tool updates the Helm chart values file with the new Docker image tag.
      • The updated Helm chart is then committed back to the GitHub repository.
  3. Continuous Delivery (CD) Process with Argo CD:

    • Argo CD Setup:

      • Argo CD is configured with a Git repository that contains the Helm chart and values files.
    • GitOps Workflow with Argo CD:

      • Argo CD monitors the Git repository for changes to the Helm chart or values files.
      • When a change is detected (e.g., the updated image tag), Argo CD synchronizes the cluster state with the desired state defined in the updated Helm chart.

Detailed Step-by-Step Process:

  1. CI Tool Setup:

    • Jenkins Pipeline Example:

      pipeline {
        agent any
        stages {
          stage('Checkout') {
            steps {
              git 'https://github.com/my-org/my-java-app-repo'
            }
          }
          stage('Build') {
            steps {
              sh 'mvn clean package'
            }
          }
          stage('Build Docker Image') {
            steps {
              script {
                def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                docker.build("myregistry/myapp:${commitHash}")
              }
            }
          }
          stage('Push Docker Image') {
            steps {
              script {
                docker.withRegistry('https://myregistry', 'registry-credentials') {
                  def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                  docker.image("myregistry/myapp:${commitHash}").push()
                }
              }
            }
          }
          stage('Update Helm Chart') {
            steps {
              script {
                def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                sh "sed -i 's|repository: myregistry/myapp:.*|repository: myregistry/myapp:${commitHash}|' helm/my-java-app/values.yaml"
                sh 'git add helm/my-java-app/values.yaml'
                sh 'git commit -m "Update image tag to ${commitHash}"'
                sh 'git push'
              }
            }
          }
        }
      }
      
  2. Helm Chart Setup:

    • Your Helm chart (Chart.yaml, values.yaml, and templates) should be stored in your Git repository.

    • The values.yaml file should include an entry for the Docker image repository and tag:

      image:
        repository: myregistry/myapp
        tag: latest
      
  3. Argo CD Setup:

    • Create an Argo CD Application:
      • Use the Argo CD UI or CLI to create an application specifying the Git repository and the path where your Helm chart is stored.

        apiVersion: argoproj.io/v1alpha1
        kind: Application
        metadata:
          name: my-java-app
          namespace: argocd
        spec:
          project: default
          source:
            repoURL: 'https://github.com/my-org/my-java-app-repo'
            targetRevision: HEAD
            path: 'helm/my-java-app'
          destination:
            server: 'https://kubernetes.default.svc'
            namespace: my-app-namespace
          syncPolicy:
            automated:
              prune: true
              selfHeal: true
        
  4. Enable Auto-Sync in Argo CD:

    • Ensure that the syncPolicy in your Argo CD Application manifest is set to automated with prune and selfHeal enabled to ensure automatic synchronization and self-healing.

Summary:

  • Jenkins (or another CI tool) handles the build, test, Docker image creation, and pushing the image to the registry.
  • Helm is used for managing the Kubernetes application deployment configurations.
  • Argo CD monitors the Git repository for changes to the Helm chart and values files, and automatically applies these changes to the Kubernetes/OpenShift cluster.

By using this approach, you ensure that your application is continuously integrated, built, and delivered in an automated fashion, leveraging the strengths of Jenkins, Helm, and Argo CD.