Monday, 23 June 2025

Jenkins - 2

 

Jenkins Interview Questions with Answers and Code Examples


Basic (1–15)

1. What is Jenkins?
Jenkins is an open-source automation server for building, testing, and deploying software.

2. How do you install Jenkins?
Jenkins can be installed via native system packages, Docker, or running as a standalone Java application.

3. What is a Jenkins pipeline?
A pipeline is a series of steps that define a CI/CD workflow in Jenkins, either declaratively or scripted.

4. What are freestyle projects?
Traditional Jenkins jobs configured via GUI for basic build steps.

5. What is the difference between Declarative and Scripted Pipeline?
Declarative uses a simplified syntax with pipeline {}; Scripted is Groovy-based and more flexible.

6. How do you create a simple Declarative pipeline?

groovy

pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } } }

7. What is a Jenkinsfile?
A text file stored in source control that defines the pipeline.

8. How do you trigger a Jenkins job?
Via SCM commit hooks, scheduled cron jobs, or manual triggers.

9. How do you configure environment variables in Jenkins pipeline?

groovy

pipeline { environment { MY_VAR = 'value' } stages { ... } }

10. How do you run shell commands in Jenkins pipeline?

groovy

steps { sh 'echo Hello Jenkins' }

11. What is the purpose of the agent directive?
Specifies where the pipeline or stage will run, e.g., any available node or a specific label.

12. How do you checkout source code in a pipeline?

groovy

steps { checkout scm }

13. How do you archive artifacts?

groovy

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

14. How do you pass parameters to a Jenkins job?

groovy

pipeline { parameters { string(name: 'BRANCH', defaultValue: 'main', description: 'Git branch') } stages { ... } }

15. How to send email notifications?

Use the emailext plugin in pipeline:

groovy

post { failure { emailext( subject: "Build failed", body: "Check console output at ${env.BUILD_URL}", to: "team@example.com" ) } }

Intermediate (16–35)

16. What are Jenkins plugins?
Plugins extend Jenkins functionality like source control, notifications, build steps, etc.

17. How to define parallel stages?

groovy

stage('Parallel') { parallel { stage('Test 1') { steps { echo 'Testing 1' } } stage('Test 2') { steps { echo 'Testing 2' } } } }

18. How do you use credentials in Jenkins?

groovy

withCredentials([usernamePassword(credentialsId: 'my-creds', usernameVariable: 'USER', passwordVariable: 'PASS')]) { sh 'echo $USER' }

19. How do you configure pipeline triggers?

groovy

triggers { cron('H/5 * * * *') }

20. What is stash and unstash?

Temporary storage of files between stages or nodes.

groovy

stash includes: 'target/*.jar', name: 'builtJar' unstash 'builtJar'

21. How to handle build failures?

groovy

post { failure { echo 'Build failed!' } }

22. How do you implement input approval in pipeline?

groovy

stage('Approval') { steps { input 'Approve Deployment?' } }

23. How do you call other jobs from a pipeline?

groovy

build job: 'other-job', parameters: [...]

24. How do you run pipeline on a specific node?

groovy

pipeline { agent { label 'my-node-label' } stages { ... } }

25. How do you check out multiple repositories?

groovy

steps { checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo1.git']]]) checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo2.git']]]) }

26. How do you define global environment variables?
Via Jenkins UI under Manage Jenkins → Configure System or via environment block in Jenkinsfile.

27. What is the difference between post and steps?
steps define the main build commands; post defines actions after the build like cleanup or notifications.

28. How do you run Groovy scripts in pipeline?

groovy

script { def message = "Hello" echo message }

29. What is a Jenkins agent?
A machine that runs Jenkins jobs.

30. How do you pass variables between stages?

Use environment variables or store them in files and use stash.

31. How do you secure Jenkins credentials?
Use Jenkins Credentials plugin and access via withCredentials.

32. What is Blue Ocean?
A modern Jenkins UI for better visualization of pipelines.

33. How do you run tests and publish reports?

groovy

steps { junit 'reports/**/*.xml' }

34. How do you integrate Docker with Jenkins?
Use Docker Pipeline plugin to build and push images.

groovy

docker.build('my-image')

35. How do you archive and fingerprint artifacts?

groovy

archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true

Expert (36–50)

36. How do you implement a multi-branch pipeline?
Use Multibranch Pipeline job type that auto-discovers branches with Jenkinsfiles.

37. How do you dynamically generate stages?

groovy

def stagesList = ['Build', 'Test', 'Deploy'] pipeline { agent any stages { stagesList.each { stageName -> stage(stageName) { steps { echo "Running ${stageName}" } } } } }

38. How do you trigger pipelines from external tools?
Use Jenkins REST API or plugins like Generic Webhook Trigger.

39. How do you secure Jenkins?
Use authentication, authorization, SSL, and least privilege principles.

40. How do you rollback deployments in Jenkins pipeline?
Implement rollback logic in pipeline steps or call rollback jobs.

41. How to implement parallelism with dynamic branches?

groovy

def branches = ['dev', 'staging', 'prod'] parallel branches.collectEntries { branch -> ["Deploy to ${branch}": { stage("Deploy ${branch}") { steps { echo "Deploying to ${branch}" } } }] }

42. How do you manage pipeline libraries?
Create Shared Libraries stored in SCM, imported via @Library annotation.

43. How do you upgrade Jenkins safely?
Backup configurations, plugins, and test upgrades in a staging environment.

44. How to handle Jenkins pipeline timeouts?

groovy

timeout(time: 10, unit: 'MINUTES') { // steps }

45. How do you monitor Jenkins jobs?
Use plugins like Monitoring, Email Extension, or external tools.

46. How do you implement conditional stages?

groovy

stage('Deploy') { when { branch 'main' } steps { ... } }

47. How do you create custom Jenkins plugins?
Develop Java plugins using Jenkins Plugin SDK and upload to Jenkins.

48. How to use Jenkins with Kubernetes?
Use Jenkins Kubernetes Plugin to dynamically provision agents in k8s clusters.

49. How do you pass parameters between Jenkins pipelines?
Use build step with parameters and read them in downstream jobs.

50. How to handle secrets in Jenkins pipelines?
Use Jenkins Credentials Plugin and mask sensitive data in logs.

No comments:

Post a Comment