Friday, February 7, 2025

Sample steps in Automating jenkins pipeline Builds

 



Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboard.

Steps to Automate Jenkins Pipeline Builds

To automate Jenkins pipeline builds, follow these structured steps:

Step 1: Install and Configure Jenkins

  1. Install Jenkins (if not already installed)

    • On Ubuntu/Debian:
      #!/bin/bash
      sudo apt update sudo apt install openjdk-11-jdk wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins
    • Start Jenkins:
      sudo systemctl start jenkins sudo systemctl enable jenkins
    • Access Jenkins via http://your-server-ip:8080
  2. Install Required Plugins

    • Navigate to Manage Jenkins > Manage Plugins
    • Install plugins like:
      • Pipeline (for defining Jenkins pipelines)
      • Git (for source control integration)
      • Kubernetes (if deploying to Kubernetes)
      • Job DSL (for automated job creation)

Step 2: Create a Jenkins Pipeline Job

  1. Go to Jenkins Dashboard > New Item
  2. Select Pipeline and give it a name (e.g., MyPipelineJob).
  3. In the Pipeline Definition, select:
    • Pipeline script from SCM (if using a Jenkinsfile in the repo)
    • Pipeline script (if writing the script inside Jenkins)

Step 3: Define the Pipeline in a Jenkinsfile

Create a Jenkinsfile in the root of your repository.

Example: Simple CI/CD Pipeline

#sample-groovy-script

pipeline { agent any environment { DOCKER_IMAGE = "myapp:latest" } stages { stage('Checkout') { steps { git 'https://github.com/user/repo.git' } } stage('Build') { steps { sh 'mvn clean package' } } stage('Test') { steps { sh 'mvn test' } } stage('Docker Build & Push') { steps { sh 'docker build -t $DOCKER_IMAGE .' sh 'docker tag $DOCKER_IMAGE myrepo/$DOCKER_IMAGE' sh 'docker push myrepo/$DOCKER_IMAGE' } } stage('Deploy') { steps { sh 'kubectl apply -f deployment.yaml' } } } }

Step 4: Automate Build Triggers

To automate builds based on events like code commits:

1. Poll SCM (Scheduled Builds)

  • Go to Job > Configure > Build Triggers
  • Enable Poll SCM and set a cron schedule (e.g., every 5 minutes):

    H/5 * * * *

2. Webhooks (Trigger on GitHub/GitLab Push)

  • Configure a webhook in GitHub:

    • Go to GitHub Repo > Settings > Webhooks
    • Set Payload URL: http://your-jenkins-server/github-webhook/
    • Content Type: application/json
    • Select Just the push event
    • Save and test the webhook.
  • In Jenkins:

    • Install GitHub Plugin
    • Enable GitHub hook trigger for GITScm polling under Build Triggers.

Step 5: Enable Notifications (Optional)

To notify teams of build status:

1. Email Notifications

  • Install the Mailer Plugin
  • Configure under Manage Jenkins > Configure System > Email Notification
  • Add this to Jenkinsfile:
    #sample-groovy-script

    post { failure { mail to: 'team@example.com', subject: "Build Failed: ${env.JOB_NAME}", body: "Check Jenkins for details: ${env.BUILD_URL}" } }

2. Slack Notifications

  • Install the Slack Plugin
  • Get Slack Webhook URL and add:
    #sample-groovy-script

    post { success { slackSend channel: '#deployments', message: "Build Successful!" } failure { slackSend channel: '#deployments', message: "Build Failed! Check Jenkins." } }

Step 6: Secure and Optimize Jenkins

  1. Use Credentials Manager

    • Store Docker registry, Kubernetes credentials, or SSH keys securely.
  2. Use Jenkins Shared Libraries

    • For reusable pipeline scripts across multiple jobs.
  3. Enable Parallel Execution

    #sample-groovy-script

    parallel { stage('Unit Tests') { steps { sh 'mvn test' } } stage('Integration Tests') { steps { sh 'mvn verify' } } }

Step 7: Monitor and Debug Builds

  • View Build Logs under Console Output
  • Re-run failed builds with retry():
    #sample-groovy-scrip

    retry(3) { sh 'mvn clean package' }
  • Archive build artifacts:
    #sample-groovy-script

    post { always { archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } }

No comments:

Post a Comment

CloudFront Functions Vs Lambda@Edge | Plus Real World Use Cases.

  Here’s twtech clear breakdown of CloudFront Functions vs . Lambda@Edge , plus the best real-world use cases for each. 1. Quick Differenc...