ArgoCD need work with Jenkins or other cicd tool - unix1998/technical_notes GitHub Wiki

Here's how the complete CI/CD pipeline would look:

Complete CI/CD Pipeline:

  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. Continuous Delivery (CD) Process:

    • Argo CD Setup:

      • Argo CD is configured with a Git repository that contains Kubernetes manifests or Helm charts defining your application's deployment configuration.
      • These manifests specify the Docker image to use for the application.
    • GitOps Workflow with Argo CD:

      • The CI tool updates the Kubernetes manifest or Helm chart with the new Docker image tag after the image is pushed to the registry.
      • This updated manifest is committed to the Git repository.
    • Argo CD Auto-Sync:

      • Argo CD, configured to monitor the Git repository, detects the changes to the manifest.
      • Argo CD automatically syncs the cluster state with the desired state defined in the updated manifest.
      • The application in the Kubernetes cluster is redeployed using the new Docker image.

Detailed Step-by-Step Process:

  1. Install Argo CD on OpenShift/Kubernetes:

  2. Set Up CI Tool (e.g., Jenkins):

    • 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 Kubernetes Manifests') {
            steps {
              script {
                def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                sh "sed -i 's|image: myregistry/myapp:.*|image: myregistry/myapp:${commitHash}|' k8s-manifests/deployment.yaml"
                sh 'git add k8s-manifests/deployment.yaml'
                sh 'git commit -m "Update deployment with image myregistry/myapp:${commitHash}"'
                sh 'git push'
              }
            }
          }
        }
      }
      
  3. Create Argo CD Application:

    • Use the Argo CD UI or CLI to create an application specifying the Git repository and the path where your Kubernetes manifests are 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: 'k8s-manifests'
      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, and Docker image creation.
  • Argo CD manages the deployment of the application in the Kubernetes/OpenShift cluster by monitoring the Git repository for changes to the Kubernetes manifests.
  • When the Java code changes and is committed to GitHub, Jenkins builds a new Docker image and pushes it to the registry. Jenkins then updates the Kubernetes manifest with the new image tag and commits this change to the Git repository.
  • Argo CD detects this change and redeploys the application using the new Docker image.

In this way, Jenkins and Argo CD work together to achieve a complete CI/CD pipeline.