Showing posts with label Jenkins pipeline. Show all posts
Showing posts with label Jenkins pipeline. Show all posts

Tuesday, December 19, 2023

How to trigger a Jenkins job from another Jenkins job | Jenkins job Integrating another Jenkins Job | Jenkins Pipeline job triggering another Jenkins Job

Jenkins job can be triggered so many different ways. This article provides steps to trigger a Jenkins job from another Jenkins job. 


Pre-requisites:

Scenario #1(post build) - How to trigger a Jenkins job from another Free style Job?

1. Login to Jenkins instance.
2. Open your any existing freestyle build job.
3. Click on Configure




4. Go to post build action

5. Add post-build action --> click on Build other projects

6. Select job name(projects)  that you want to trigger by typing the name of the job and also check trigger only if build is stable


7. Save the job. 
8. Build job now, once the current job is built, it will trigger the next job immediately.
Check the console output of the current job, you will see id would trigger second job 

9. Go to the secondJob
Check console output. You will see the second job got triggered by first build job.

Scenario #2 (pre-build) - How to trigger a Jenkins job from another Free style Job?

1. Open your freestyle build job.
2. Click on Configure
3. Click on Build triggers
4. Check build other projects are built.
    


Select source job name which will be built first and then once the build is stable, it will be trigger this job. And also check trigger only if build is stable.

5. Save the job. 

6. Run the first job. once that job is successful, and then it will trigger this job.


Scenario #3 - How to trigger any Jenkins job from a pipeline Job:

pipeline {

    agent any

    stages {

        stage('Trigger Another Job') {

            steps {

                    build job: 'mySecondJob', wait: false

            }

        }

    }

}



Watch Steps in YouTube channel: 

Wednesday, November 29, 2023

Jenkins CI/CD Pipeline Optimization Best Practices | Optimizing Jenkins CI/CD pipelines

Optimizing Jenkins CI/CD pipelines is crucial for achieving faster, more efficient, and reliable software delivery. Here are some best practices and strategies for optimizing Jenkins pipelines:

1. Parallelization:

  • Parallel Stages: Break down your pipeline into stages and parallelize independent stages to run concurrently. This can significantly reduce the overall pipeline execution time.

  • stages { stage('Build') { steps { script { parallel( unit_tests: { // Run unit tests }, integration_tests: { // Run integration tests } ) } } } // Other stages... }

2. Artifact Caching:

  • Use Caches: Utilize Jenkins' built-in caching mechanisms to store and retrieve build artifacts between different pipeline runs. This reduces the time spent on redundant build steps.

  • pipeline { options { // Enable build caching buildDiscarder(logRotator(numToKeepStr: '5')) caches { gradle 'gradle-wrapper' } } // Pipeline stages... }

3. Agent Utilization:

  • Node Pools: Distribute builds across multiple Jenkins agents or node pools to leverage available resources effectively. Adjust the number of executors on each agent based on workload.

  • pipeline { agent { label 'docker' } // Pipeline stages... }

4. Incremental Builds:

  • Only Build Changes: Set up your pipeline to trigger builds only for changes in relevant branches. Use tools like Git SCM polling or webhooks to trigger builds on code changes.

5. Artifact Promotion:

  • Promote Artifacts: Promote artifacts from one environment to another instead of rebuilding them. This helps in maintaining consistency across environments and reduces build times.

6. Pipeline DSL Optimization:

  • Code Reusability: Use shared libraries and functions to avoid duplicating code across multiple pipeline scripts. This promotes code reusability and simplifies maintenance.

7. Conditional Execution:

  • When Conditions: Use the when directive to conditionally execute stages based on certain criteria, such as branch names or environment variables.

  • stage('Deploy to Production') { when { expression { params.DEPLOY_TO_PROD == 'true' } } steps { // Deployment steps } }

8. Artifact Cleanup:

  • Clean Workspace: Include a step to clean up the workspace at the end of each build to avoid accumulation of unnecessary artifacts and files.
  • post { always { cleanWs() } }

9. Pipeline Visualization:

  • Blue Ocean: Consider using the Blue Ocean plugin for Jenkins, which provides a more visually appealing and intuitive view of your pipeline.

10. Monitoring and Analytics:

  • Collect Metrics: Implement monitoring and analytics to collect data on pipeline performance. Identify bottlenecks and areas for improvement.

11. Pipeline as Code:

  • Declarative Syntax: Use the declarative syntax for Jenkins pipeline scripts whenever possible. It is more concise and easier to read.

12. Use Jenkins Shared Libraries:

  • Library Usage: If you have common functionality across multiple pipelines, consider moving that logic into a shared library. This promotes code reuse and centralizes maintenance.

13. Artifact Signing and Verification:

  • Security Checks: Integrate security checks into your pipeline, including artifact signing and verification steps, to ensure the integrity and authenticity of your artifacts.

14. Automated Testing:

  • Automated Tests: Include automated tests for your pipeline scripts to catch issues early. Jenkins provides testing frameworks like Jenkins Pipeline Unit for this purpose.

15. Infrastructure as Code:

  • Infrastructure Automation: Treat your Jenkins infrastructure as code. Use tools like Docker and Kubernetes for scalable and reproducible Jenkins environments.

Thursday, June 15, 2023

How to Deploy Springboot Microservices App into AKS cluster using Jenkins Pipeline and Helm | Deploy Microservices into AKS cluster using Helm Charts and Jenkins Pipeline

We are going to learn how to Automate build and deploy of Springboot Microservices App into Azure Kubernetes Cluster(AKS) using Helm and Jenkins pipeline.


Sample springboot App Code:

I have created a sample Springboot App setup in GitHub. Click here to access code base in GitHub. 

Jenkins pipeline will:

- Automate maven build(jar) using Jenkins
- Automate Docker image creation
- Automate Docker image upload into Azure container registry
- Automate Springboot docker container Deployments to Azure Kubernetes Cluster using Helm charts


Pre-requisites:
2.  Install Docker in Jenkins and Jenkins have proper permission to perform Docker builds
3. Install Azure CLI on your Jenkins machine. (We will be creating AKS cluster from Jenkins machine running in Azure Cloud)
4. Helm installed on Jenkins instance
5. Install Kubectl on Jenkins instance
6. AKS cluster needs to be up running. You can create AKS cluster, ACR Repo using shell script provided in my website.
7. Make sure to Install Docker, Docker pipeline 


8. ACR is also setup in Azure cloud. 
10. Dockerfile created along with the application source code for springboot App.

The Code for this video is here:

Implementation steps:

  1. Create a resource group, AKS cluster and Azure container registry 
  2. Provide pull access for AKS to pull image from ACR 
  3. Create a namespace for helm deployment
  4. Create a helm chart for spring boot app
  5. Create a Jenkins pipeline with below stages:
    • checkout
    • Build Jar
    • Build docker image
    • Upload image to ACR
    • Deploy to AKS using helm
  6. Run the pipeline to deploy springboot app into AKS
  7. Verify deployments in the namespace in AKS
  8. Access the app in the browser
Create AKS cluster from Jenkins Virtual machine
Login to Jenkins virtual machine. switch as jenkins user
sudo su - jenkins

Authenticate to Azure Cloud by typing:
az login


Now to the browser, type https://microsoft.com/devicelogin
enter the code as received from previous step:

Enter your microsoft credentials, click continue per below screen:

You will be shown below screen:

 
Create a shell script based on the script provided here

vi create-aks.sh
copy and paste entire script provided in this link.

Execute shell script to create the following in Azure clid:
  • Resource group
  • AKS cluster
  • ACR repo
  • provide pull access to AKS for pulling docker image from ACR
  • namespace in AKS cluster for deploying our springboot app
sh create-aks.sh
This will take 2 to 5 mins to create the resources in Azure cloud.

Make sure cluster is up and running with worker nodes
kubectl get nodes


Create Helm chart using helm command
Go to your root of repo where you have source code for your springboot application. Create helm chart by executing below command:

helm create mychart
tree mychart
Execute the above command to see the files created.



Add Docker image details to download from ECR before deploying to EKS cluster
open mychart/values.yaml. change per below values:

image:
repository: myacrrepo531.azurecr.io/myacrrepo531
tag: ""


Enter service type as LoadBalancer
And also
open mychart/templates/deployment.yaml and change containerPort to 8080


Save the files, commit and push into repo.
Step # 1 - Create Maven3 variable under Global tool configuration in Jenkins
Make sure you create Maven3 variable under Global tool configuration. 



Step #2 - Create Credentials to connect to ACR from Jenkins

Go to Azure Portal console, go to container registry
Settings--> Access keys
Get the username and password 
Go to Jenkins-> Manage Jenkins. Create credentials.


Enter ID as ACR and enter some text for description and Save.


Step # 3 - Create a pipeline in Jenkins
Create a new pipeline job.

Step # 4 - Copy the pipeline code from below
Make sure you change values as per your settings highlighted in yellow below:

pipeline {
  tools {
        maven 'Maven3'
    }
    agent any
        environment {
        //once you create ACR in Azure cloud, use that here
        registryName = "myacrrepo531"
        //- update your credentials ID after creating credentials for connecting to ACR
        registryCredential = 'ACR'
        dockerImage = ''
        registryUrl = 'myacrrepo531.azurecr.io'
    }
    
    stages {
        stage('checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'check_out_from_your_repo_after_forking_my_repo']]])
            }
        }
        
        stage ('Build Jar') {
        steps {
            sh 'mvn clean install'           
        }
     }
     
    stage ('Build Docker image') {
        steps {
                script {
                    dockerImage = docker.build registryName
                }
            }
        }
        
    // Uploading Docker images into ACR
        stage('Upload Image to ACR') {
         steps{   
             script {
                docker.withRegistry( "http://${registryUrl}", registryCredential ) {
                dockerImage.push("$BUILD_NUMBER")
                }
            }
          }
        }
        
        stage ('Helm Deploy') {
          steps {
            script {
                sh "helm upgrade first --install mychart --namespace helm-deployment --set image.tag=$BUILD_NUMBER"
                }
            }
        }
    }
}

Step # 5 - Build the pipeline

Step # 6 - Verify deployments in AKS
Execute the below command to list the helm deployments:
helm ls -n helm-deployment


kubectl get pods -n helm-deployment

kubectl get services -n helm-deployment

Steps # 7 - Access Springboot App Deployed in AKS cluster
Once deployment is successful, go to browser and enter above load balancer URL mentioned above

You should see page like below:

Clean up the Cluster:

To avoid charges from Azure, you should clean up unneeded resources. When the cluster is no longer needed, use the az group delete command to remove the resource group, container service, and all related resources. 

az group delete --name resource-group-name --yes --no-wait


Watch steps in YouTube Channel: