Ravindra BagaleCourses & study guides मराठी

Chapter 31: Backup, Configuration as Code and Jenkins Operations

Chala mitrano, aaj aapan backup/restore JENKINS_HOME, HA honesty, CasC, plugin upgrades, monitoring, REST/CLI, Script Console warning. Ghabru naka.

What you will learn in this chapter

  • Backup and restore-test JENKINS_HOME
  • Why two controllers on one balancer is not HA
  • Configuration as Code snippet
  • Plugin upgrades, monitoring, logs
  • REST/CLI job trigger; Script Console is admin-only

Concepts in this chapter

  1. 31.1Backup JENKINS_HOME
  2. 31.2Restore test
  3. 31.3High availability is not two controllers on one balancer
  4. 31.4Configuration as Code
  5. 31.5Plugin upgrades and performance
  6. 31.6Monitoring and logs
  7. 31.7REST and CLI that start a job
  8. 31.8Script Console

The chapter recap is at the end of the last concept page.

Practice Tasks

About this scenario

Suppose we are on the DevOps team at Razorpay, the payments company, working from Bengaluru. Vikram Shetty leads the team, and we look after the payment links API. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Razorpay is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.

0 / 12

Easy Declarative Jenkinsfile: build and test

Why: Pipeline as code lives in Git with the app, so every change is reviewed.

What to do: Write a declarative Jenkinsfile: agent any; stage Build runs sh 'mvn -B package'; stage Test runs sh 'mvn test'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Run the build inside a Docker agent

Why: A Docker agent gives a clean, versioned toolchain for every build.

What to do: Declarative pipeline with agent docker { image 'maven:3.9-eclipse-temurin-21' } and stage Build running sh 'mvn -B verify'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Pipeline environment variables

Why: Values set once in environment are reused by every stage.

What to do: Declarative pipeline, agent any, environment with IMAGE = 'razorpay/payment-links' and TAG = '1.7.5', and stage Image running sh 'docker build -t $IMAGE:$TAG .'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Always archive, alert on failure

Why: post blocks run after the stages, whatever the result.

What to do: Declarative pipeline, agent any, one stage Build with sh 'make'. Add post: always → archiveArtifacts artifacts: 'build/**'; failure → echo 'Build failed'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Deploy only from main

Why: Feature branches are built and tested but never deployed.

What to do: Declarative pipeline, agent any, stage Deploy with when { branch 'main' } and steps sh './deploy.sh ap-south-1'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Make a compressed backup

Why: A .tar.gz file packs a whole folder into one small file you can copy anywhere.

What to do: Create a gzip-compressed archive named razorpay-site.tar.gz from the folder /var/www/razorpay.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Click Check answer.

Medium Extract a backup

Why: To restore, you unpack the archive into a folder.

What to do: Extract razorpay-site.tar.gz into the folder /tmp/restore.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Click Check answer.

Medium Zip a folder

Why: Zip files open easily on Windows, so they are good for sharing with office staff.

What to do: Create invoices.zip containing the folder invoices and everything in it.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Click Check answer.

Hard Dated backup with cleanup

Why: Backups need a date in the name and old ones removed, or disks fill up.

What to do: Write a bash script: D=$(date +%F); tar -czf /backup/razorpay-$D.tar.gz /var/www; then find /backup -name "*.tar.gz" -mtime +7 -delete.

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. Click Check answer.

Hard Use Jenkins credentials safely

Why: withCredentials masks the password and removes it after the block.

What to do: Inside stage Push steps, use withCredentials with usernamePassword(credentialsId: 'reg-cred', usernameVariable: 'U', passwordVariable: 'P') and run sh 'echo $P | docker login -u $U --password-stdin registry.razorpay.example.com'. Wrap it in a full declarative pipeline with agent any.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Hard Manual approval before production

Why: A human sign-off stops an accidental production release.

What to do: Declarative pipeline, agent any, stage Prod with input message 'Deploy to production?' and steps sh './deploy.sh prod'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Hard Parallel test stages

Why: Running unit and lint at the same time halves feedback time.

What to do: Declarative pipeline, agent any, stage Checks containing parallel with stage Unit (sh 'npm test') and stage Lint (sh 'npm run lint').

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Ravi Sir's Cards: 19 revision, interview and comparison cards for this chapter · मराठी cards