Declarative pipeline

is an example of a Declarative Pipeline that can be used to build, test, and deploy a Java application using Maven, SonarQube, Docker, and Ansible:

pipeline {
    agent {
        docker {
            image 'maven:3.6.3-jdk-11'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
                sh 'sonar-scanner'
            }
        }
        stage('Containerize') {
            steps {
                sh './containerize.sh'
            }
        }
        stage('Deploy') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'kubernetes-deploy', passwordVariable: 'KUBERNETES_PASSWORD', usernameVariable: 'KUBERNETES_USERNAME')]) {
                    sh 'ansible-playbook deploy.yml -e "kubernetes_username=${KUBERNETES_USERNAME} kubernetes_password=${KUBERNETES_PASSWORD}"'
                }
            }
        }
    }
}

In this example, the pipeline has four stages: Build, Test, Containerize, and Deploy. The Build stage runs the Maven clean and package goals, the Test stage runs the Maven test goal and runs a SonarQube scan, the Containerize stage runs a script called containerize.sh to create a Docker container, and the Deploy stage uses Ansible to deploy the container to Kubernetes.

This pipeline assumes that the necessary credentials for deploying to Kubernetes are stored in Jenkins and are configured with the ID kubernetes-deploy. It also assumes that the sonar-scanner command is available on the PATH and that an Ansible playbook called deploy.yml exists in the Jenkins workspace.