Sharpen Your Jenkins Skills with Practical Scenario-Based Questions and Code Walkthroughs

  1. Scenario: You want to create a Jenkins pipeline that builds a Maven-based Java project and runs unit tests. Write a Jenkinsfile that executes these steps.

Code:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'mvn -B -DskipTests clean package'
      }
    }
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
  }
}
  1. Scenario: You want to create a Jenkins pipeline that builds a Docker image, pushes it to a Docker registry, and deploys it to a Kubernetes cluster. Write a Jenkinsfile that executes these steps.

Code:

pipeline {
  agent any
  environment {
    DOCKER_REGISTRY = 'my-docker-registry.com'
    KUBECONFIG = credentials('kubeconfig')
  }
  stages {
    stage('Build') {
      steps {
        script {
          def dockerImage = docker.build("my-image:${env.BUILD_NUMBER}")
          docker.withRegistry(env.DOCKER_REGISTRY, credentialsId: 'docker-hub') {
            dockerImage.push()
          }
        }
      }
    }
    stage('Deploy') {
      steps {
        sh "kubectl apply -f kubernetes/deployment.yaml --kubeconfig=\${KUBECONFIG}"
      }
    }
  }
}
  1. Scenario: You want to create a Jenkins pipeline that triggers a build when changes are pushed to a Git repository. Write a Jenkinsfile that sets up a webhook and executes the pipeline on push events.

Code:

pipeline {
  agent any
  triggers {
    githubPush()
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -B -DskipTests clean package'
      }
    }
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
  }
}
  1. Scenario: You want to create a Jenkins pipeline that deploys a Java application to a remote server. Write a Jenkinsfile that uses SSH to copy the artifact to the server and start the application.

Code:

pipeline {
  agent any
  environment {
    SERVER = 'my-server.com'
    USERNAME = credentials('ssh-username')
    PASSWORD = credentials('ssh-password')
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -B -DskipTests clean package'
      }
    }
    stage('Deploy') {
      steps {
        sshagent(['ssh-key']) {
          sh "sshpass -p \$PASSWORD scp target/my-app.jar \$USERNAME@\$SERVER:/home/\$USERNAME/my-app.jar"
          sh "sshpass -p \$PASSWORD ssh \$USERNAME@\$SERVER 'java -jar /home/\$USERNAME/my-app.jar &'"
        }
      }
    }
  }
}
  1. Scenario: You want to create a Jenkins pipeline that deploys a Python application to AWS Lambda. Write a Jenkinsfile that packages the application and deploys it to Lambda using the AWS CLI.

Code:

pipeline {
  agent any
  environment {
    AWS_ACCESS_KEY_ID = credentials('aws-access-key')
    AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
    AWS_DEFAULT_REGION = 'us-east-1'
  }
  stages {
    stage('Build') {
      steps {
        sh 'pip install -r requirements.txt -t build'
        sh 'cp app.py build/'
        sh 'cd build && zip -r ../lambda.zip . && cd ..'
      }
    }
    stage('Deploy') {
      steps {
        sh 'aws lambda update-function-code --function

Leave a Reply

Your email address will not be published. Required fields are marked *