Jenkins- Interview Questions
1. Explain the master-slave architecture in Jenkins
The master-slave architecture is also known as the controller-agent architecture.
This architecture is commonly used to distribute the workload and to scale Jenkins for larger and more complex continuous integration (CI) and continuous delivery (CD) pipelines.
- Jenkins Master: Central server managing Jenkins, scheduling jobs, and overseeing the environment.
- Jenkins Slave/Agent: Worker machine executing build jobs dispatched by the master.
- Communication: The Master communicates with slaves over a network, selecting available agents for job execution.
- Job Execution: Slave nodes execute build tasks as per job configurations, such as compiling code or running tests.
- Status Reporting: Slaves report job status and progress back to the master for monitoring and analysis.
2. Write a CI/CID Jenkins pipeline used in your project
pipeline {
agent any
environment {
// Define environment variables for AWS credentials, ECR repository, EKS cluster, etc.
AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
AWS_REGION = 'your-aws-region'
ECR_REPO = 'your-ecr-repository'
EKS_CLUSTER = 'your-eks-cluster'
}
stages {
stage('Build and Test') {
steps {
// Checkout source code from version control
git 'https://github.com/your/repository.git'
// Build and test using Maven
sh 'mvn clean package'
}
}
stage('Build Docker Image') {
steps {
// Build Docker image
script {
docker.build("${ECR_REPO}:${env.BUILD_NUMBER}")
}
}
}
stage('Push to ECR') {
steps {
// Authenticate with ECR
withCredentials([string(credentialsId: 'ecr-credentials', variable: 'DOCKER_CREDENTIALS')]) {
sh "echo $DOCKER_CREDENTIALS | docker login -u AWS --password-stdin https://${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
}
// Tag and push Docker image to ECR
script {
docker.withRegistry("https://${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com", 'ecr-credentials') {
docker.image("${ECR_REPO}:${env.BUILD_NUMBER}").push()
}
}
}
}
stage('Deploy to EKS') {
steps {
// Deploy image to EKS
sh "kubectl apply -f kubernetes/deployment.yaml --kubeconfig=kubeconfig.yaml"
}
}
stage('Monitor Logs') {
steps {
// Monitoring setup using Prometheus and Grafana
// Example: Configuration and deployment of Prometheus and Grafana resources
sh "kubectl apply -f kubernetes/prometheus.yaml --kubeconfig=kubeconfig.yaml"
sh "kubectl apply -f kubernetes/grafana.yaml --kubeconfig=kubeconfig.yaml"
}
}
}
}
- Build and Test: Checks out the source code from the repository and builds/test using Maven
- Build Docker Image: Build a Docker image using the Dockerfile in the repository.
- Push to ECR: Authenticates with Amazon ECR tags the Docker image, and pushes it to the specified ECR repository.
- Deploy to EKS: Deploy the Docker image to Amazon EKS by applying the Kubernetes deployment configuration.
- Monitor Logs: Set up monitoring using Prometheus and Grafana by deploying Prometheus and Grafana configurations to the Kubernetes cluster.
3. Explain the two types of pipelines in Jenkins
- Scripted Pipelines:
- Definition: Groovy-based scripting language directly within Jenkins.
- Syntax: Imperative scripting syntax for sequential execution of commands and statements.
- Flexibility: Allows dynamic generation of stages, parallel execution, and advanced logic.
- Example: Imperatively defines stages and commands using Groovy.
- Use Cases: Suitable for complex build and deployment requirements, and for teams familiar with Groovy scripting.
node {
stage('Build') {
sh 'mvn clean package'
}
stage('Test') {
sh 'mvn test'
}
stage('Deploy') {
sh 'ansible-playbook deploy.yml'
}
}
2. Declarative Pipelines:
- Definition: YAML-based syntax focusing on defining the desired state of the pipeline.
- Syntax: Predefined keywords and blocks for readability and maintainability.
- Structure: Fixed structure with predefined sections for stages, steps, and post-build actions.
- Example: Declaratively defines stages and steps using YAML.
- Use Cases: Ideal for standardizing configurations, enforcing best practices, and promoting collaboration.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh 'ansible-playbook deploy.yml'
}
}
}
}
4. How can you find log files in Jenkins
Jenkins log files reside within the Jenkins server’s file system. Their specific location varies depending on the operating system and the installation method. Typical locations are:
- Linux:
/var/log/jenkins/jenkins.log
- Windows:
C:\Program Files (x86)\Jenkins\jenkins.out
Alternatively, you can access the logs via the Jenkins web interface. Navigate to “Manage Jenkins” > “System Log” or “Log Recorder” to view and manage various log files.
5. How to trigger a build in Jenkins manually?
To trigger a build manually in Jenkins:
- Log in to Jenkins.
- Navigate to the project/job you want to build.
- Click on the project/job name.
- On the left sidebar, click on the “Build Now” or “Build” button.
- Jenkins will start the build process immediately.
In Declarative Pipelines,
pipeline {
agent any
parameters {
// Define any parameters needed for the build, if applicable
}
stages {
// Define stages of your pipeline
stage('Build') {
steps {
// Steps to build your project
}
}
// Add more stages as needed
}
post {
always {
// Any post-build actions or cleanup tasks
}
success {
// Actions to perform when the build is successful
}
failure {
// Actions to perform when the build fails
}
unstable {
// Actions to perform when the build is unstable
}
}
// Manual input to trigger the build
triggers {
userInput('Deploy to Production?') {
// Customize the message displayed to the user
message 'Do you want to deploy to production?'
// Define parameters if needed
parameters {
// Define parameters for user input, if applicable
}
// Define options for the input step
ok 'Deploy'
submitter 'admin' // Specify user who can approve the input
}
}
}
6. What is the default path for the Jenkins password when you install it?
The default path for the Jenkins initial administrative password is:
- Unix-like systems (e.g., Linux):
/var/lib/jenkins/secrets/initialAdminPassword
- Windows:
C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword
7. How to integrate Git with Jenkins?
- Install Git Plugin: If not already installed, navigate to “Manage Jenkins” > “Manage Plugins” > “Available” tab, search for “Git Plugin”, and install it.
2. Configure Global Git Settings (if necessary):
- Navigate to “Manage Jenkins” > “Configure System”.
- Under the “Git” section, specify the path to the Git executable if Jenkins cannot locate it automatically.
- Optionally, configure additional global settings such as user.name and user.email.
3. Set Up Jenkins Job:
- Create a new Jenkins job or open an existing one.
- Configure the job according to your project requirements (e.g., freestyle project, pipeline).
- In the “Source Code Management” section, select “Git”.
- Enter the repository URL (e.g., HTTPS or SSH).
- Optionally, specify credentials if authentication is required to access the Git repository.
4. Configure Build Triggers (optional):
- In the job configuration, navigate to the “Build Triggers” section.
- Choose how you want Jenkins to trigger builds, such as polling the repository for changes, triggering builds remotely, or using webhooks.
5. Define Build Steps:
- Navigate to the “Build” section of the job configuration.
- Define the build steps, such as compiling code, running tests, or deploying artifacts.
- Use Git-related commands (e.g., git clone, git checkout) as needed within the build steps to interact with the repository.
6. Save and Run the Job:
- Save the job configuration.
- Manually trigger a build or wait for Jenkins to automatically trigger a build based on the configured build triggers.
7. View Build Results:
- Monitor the build progress and view the build results in the Jenkins web interface.
- Check the console output for any Git-related actions performed during the build process.
pipeline {
agent any
triggers {
// Trigger the pipeline whenever a code change is pushed to the Git repository
scm '*/5 * * * *' // Polls Git every 5 minutes (adjust interval as needed)
}
stages {
stage('Checkout') {
steps {
// Checkout the code from the Git repository
git 'https://github.com/your/repository.git'
}
}
// Add more stages for build, test, deploy, etc.
}
}
8. What does Poll SCM mean in Jenkins?
Poll SCM is a feature in Jenkins that allows Jenkins to periodically check the source code management system (like Git) for changes.
9. How you will check the generated artifact/logs/Zar file in Jenkins?
If the Jenkins job has generated any artifacts then, it has an Artifacts section in that
Alternatively, you can ssh into the Jenkins server, and from JENKINS_HOME/workspace/<Your_Job_Name>/ path you can download the artifact.
10. How to automate the access token rotation in the GitLab and apply the new token in Jenkins?
Automate GitLab access token rotation:
- Generate a new token in GitLab.
- Update Jenkins job to use the new token.
- Optionally, automate token rotation with GitLab API.
- Optionally, update Jenkins pipeline script.
- Test the integration.
pipeline {
agent any
stages {
stage('Rotate GitLab Access Token') {
steps {
// Your script to rotate the GitLab access token using GitLab API
// This step generates a new access token and stores it securely
}
}
stage('Update Jenkins Job') {
steps {
// Update the Jenkins job to use the new access token
script {
// Load GitLab access token from Jenkins credential store
withCredentials([string(credentialsId: 'gitlab-access-token', variable: 'GITLAB_ACCESS_TOKEN')]) {
// Update job configuration with new access token
sh 'sed -i "s/OLD_ACCESS_TOKEN/$GITLAB_ACCESS_TOKEN/g" job_config.xml'
}
}
}
}
stage('Build') {
steps {
// Your build steps here
}
}
}
}
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!