Ravindra BagaleCourses & study guides मराठी

Chapter 10: Jenkins Introduction

DevOps

10.8d Folders and credentials scope

Why: the problem

Suppose an HDFC Bank Jenkins serves three teams: cards, loans and mobile. All jobs sit on one dashboard. The production server key is a global credential. Any job, from any team, can use it. One intern copies a job and accidentally deploys loans code to the cards server. Folders fix this. A folder groups jobs like a directory. Credentials saved inside a folder can only be used by jobs in that folder. With a permission plugin, each team sees only its own folder. Now the cards key cannot leak to the loans jobs. Auditors in banks ask for exactly this: who can use which secret.

What we will build

You will create team folders, add folder-level credentials, use them from a job inside the folder, see a job outside fail to find them, and give folder permissions with Role-based access.

What: every term in plain English

  • Folder — A container for jobs, like a directory. Needs the Folders plugin (installed with suggested plugins).
  • Folder credential — A credential saved in a folder. Only jobs in that folder or its sub-folders can use it.
  • Global credential — A credential every job can use. Keep these few.
  • Credential domain — A smaller group inside a store, often by hostname. Optional.
  • Role-based Authorization Strategy — A plugin to give users roles, like cards-dev, with patterns like cards/.*.
  • Item role — Permissions on jobs that match a pattern.
  • Full name — A job path, like cards/payments-api.
  • Least privilege — Give each job and person only what they need.
Create folders Move a job Add a folder credential Install role plugin Make a role
Team folders: jobs, folder credentials and roles stay inside

How: step by step

  1. Create folders

    New Item > cards > Folder > OK; repeat for loans
    

    What it does: Each team gets one folder.

    Output

    cards  loans
    
  2. Move a job

    Open payments-api > Move > cards
    

    What it does: The job path becomes cards/payments-api.

    Output

    cards » payments-api
    
  3. Add a folder credential

    cards > Credentials > Folder > Global credentials > Add: SSH key, ID cards-prod-ssh
    

    What it does: Lives only in cards.

    Output

    cards-prod-ssh
    
  4. Install role plugin

    Manage Jenkins > Plugins > Role-based Authorization Strategy
    

    What it does: Gives folder-level user permissions.

    Output

    Installed
    
  5. Make a role

    Manage Jenkins > Manage and Assign Roles > Item roles: cards-dev, pattern cards/.*, Build + Read
    

    What it does: Then assign user priya to cards-dev.

    Output

    priya: cards-dev
    

Example 1: Use a folder credential from a job inside the folder

Stack. Folders, SSH Agent

Goal. Use a folder credential from a job inside the folder.

cards/payments-api Jenkinsfile

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                sshagent(credentials: ['cards-prod-ssh']) {
                    sh 'ssh -o StrictHostKeyChecking=accept-new deploy@10.10.5.11 "hostname && systemctl is-active payments"'
                }
            }
        }
    }
}

What each line does

  • credentials: ['cards-prod-ssh'] — Found, because the job is inside cards.
  • deploy@10.10.5.11 — A deploy-only user on the cards server.
  • systemctl is-active payments — Checks the app is up.

Green output

[ssh-agent] Using credentials deploy (cards-prod-ssh)
+ ssh deploy@10.10.5.11 hostname && systemctl is-active payments
cards-prod-01
active
Finished: SUCCESS

Expected result. The cards job deploys with the cards key.

Example 2: The same step from the loans folder fails

Stack. Folder scope test

Goal. The same step from the loans folder fails.

loans/loan-api Jenkinsfile

pipeline {
    agent any
    stages {
        stage('Try cards key') {
            steps {
                sshagent(credentials: ['cards-prod-ssh']) {
                    sh 'echo should not get here'
                }
            }
        }
    }
}

What each line does

  • credentials: ['cards-prod-ssh'] — Not visible from loans.
  • sshagent — Fails before any command runs.

Green output

ERROR: Failed to run ssh-agent: credentials not found: [cards-prod-ssh]
Finished: FAILURE

Expected result. This red build is good news. The folder kept the key safe.

Example 3: Folder properties and nested folders

Stack. Nested folders, role patterns

Goal. Folder properties and nested folders.

Layout

cards/                     (folder credential: cards-registry)
  payments-api             (Pipeline)
  prod/                    (sub-folder credential: cards-prod-ssh)
    payments-deploy        (Pipeline, only seniors can build)
loans/
  loan-api

cards/prod/payments-deploy Jenkinsfile

pipeline {
    agent any
    stages {
        stage('Push and deploy') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'cards-registry', usernameVariable: 'U', passwordVariable: 'P')]) {
                    sh 'echo "$P" | docker login registry.example.com -u "$U" --password-stdin'
                }
                sshagent(credentials: ['cards-prod-ssh']) { sh 'ssh deploy@10.10.5.11 "sudo systemctl restart payments"' }
            }
        }
    }
}

What each line does

  • cards-registry in cards/ — Sub-folders inherit parent folder credentials.
  • cards-prod-ssh in cards/prod/ — Only prod jobs see the prod key.
  • Role pattern cards/prod/.* — Give Build on this pattern only to seniors.

Green output

Login Succeeded
+ ssh deploy@10.10.5.11 sudo systemctl restart payments
Finished: SUCCESS

Expected result. Normal cards jobs can push images; only prod jobs can touch production.

Verify: did it work?

  1. Check which credentials a job can see

    Open the job > Pipeline Syntax > withCredentials > Credentials drop-down
    

    What it does: Lists only visible ids.

    Output

    cards-registry
    cards-prod-ssh
    
  2. Check a user view

    Log in as priya
    

    What it does: She sees only the cards folder.

    Output

    Dashboard: cards
    

When it is red 1

ERROR: Could not find credentials entry with ID 'cards-registry'

Cause. The job is outside the folder that holds the credential.

Fix, step by step

  1. Check the job path: it must be inside cards/.
  2. Move the job, or add the credential to the right folder.

When it is red 2

priya is missing the Job/Build permission

Cause. Her role pattern does not match the job.

Fix, step by step

  1. Patterns are regex: use cards/. not cards/.
  2. Assign the role to the user.
  3. Log out and in.

When it is red 3

After moving a job the webhook stops working

Cause. The job URL changed to /job/cards/job/payments-api.

Fix, step by step

  1. Update the webhook URL or token.
  2. Test with curl.

When it is red 4

Folder 'cards' credentials page shows no Add button

Cause. You lack the Credentials/Create permission on that folder.

Fix, step by step

  1. Ask an admin to give your role Credentials Create on the pattern cards.*.
  2. Or let the admin add the credential for you.
  3. Never ask for full Administer just to add one key.

Cleanup

  1. Delete the test loans job

    loans > loan-api > Delete Pipeline
    

    What it does: Removes the test.

    Output

    (deleted)
    

Ravindra Bagale's Tip

Every team gets its own folder and its own credentials. Do not give the prod key to everyone!

Practice tasks

Easy Make a sub-folder

Why: Split prod.

What to do: Create cards/prod.

How:
  1. New Item inside cards.

Check: Path cards/prod exists.

Medium Role test

Why: Prove access control.

What to do: Create user test1 with only loans access.

How:
  1. Log in as test1.

Check: cards folder is hidden.

Hard Audit globals

Why: Clean up risk.

What to do: List all global credentials and move team ones into folders.

How:
  1. Update jobs.

Check: Global list is short.

Easy Say folders and credential scope in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.

What to do: Write two short lines about folders and credential scope for a fresher who knows only Linux, Git and Docker basics.

How:
  1. Write the problem it solves.
  2. Write the one Jenkins screen, Jenkinsfile line or command you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Keep a throwaway lab Jenkins

Why: Practice needs a Jenkins you can break. If the lab gets messy, you delete the container and volume and start again.

What to do: Run a lab Jenkins in Docker on your laptop with a named volume jenkins_home.

How:
  1. docker volume create jenkins_home
  2. docker run -d --name jenkins -p 8080:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk17
  3. Open http://localhost:8080 and finish the setup wizard.

Check: The Jenkins dashboard opens and shows "Welcome to Jenkins!".

Medium Do it on a second stack

Why: A pipeline idea must work for Node.js, Python and Java. Only the build and test lines should change.

What to do: Repeat this lesson with a different language than the one you used first.

How:
  1. If you used Python, use Node.js or Maven now.
  2. Change only the sh lines for build and test.
  3. Keep the same stage names.

Check: Same green result, and the stage names did not change.

Medium Break it on purpose

Why: You must know the red console log by face. In a real outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message from Console Output into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers Jenkins menus. A short page saves the on-call person.

What to do: Write a one-page runbook for this lesson.

How:
  1. Title, when to use, exact clicks or Jenkinsfile lines, how to check, how to undo.

Check: A teammate can follow it alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the Jenkinsfile lines you wrote, and one error you hit.

Check: Your answer has a real Jenkinsfile line and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows the order of work and which stage waits for which.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at git push or Build Now, go through each stage, end at the green build or the deployed app.

Check: Each box matches a real line in your Console Output.

Homework: try at home

Try at home

At home, make cards and loans folders, add a fake folder credential in cards, and prove a loans job cannot use it.

Recap

  • Folders group jobs per team.
  • Folder credentials are visible only inside the folder.
  • Sub-folders inherit parent credentials.
  • Role patterns limit what people can see and build.
  • Keep global credentials to a minimum.

FAQ

Does moving a job break history?

No, history moves with it, but URLs change.

Can a folder have its own library?

Yes, folders can define pipeline libraries too.

Matrix or Role-based?

Both work. Role-based is easier with many teams.

Do folders cost performance?

No.

Can I hide a folder?

Yes, users without Read on it do not see it.

Can a folder credential override a global one?

Yes. If both have the same id, the nearest one, in the folder, wins. Use clear ids to avoid confusion.

Where are folder credentials stored?

Encrypted in the folder config inside JENKINS_HOME/jobs, so back up that folder too.

Interview questions

  1. How do you isolate credentials between teams in Jenkins?

    Use folders with folder-scoped credentials and role-based permissions on folder patterns.

  2. What is credential scope?

    Where a credential is visible: global, folder, or system.

  3. How do you stop juniors deploying to prod?

    Put prod jobs in a sub-folder with its own credentials and give Build only to seniors.

  4. Do sub-folders inherit credentials?

    Yes.

  5. What happens to webhooks after moving a job?

    The job URL changes, so update them.

Quick quiz

Q1. What does a folder credential limit?

Which jobs can use it.

Q2. Do sub-folders inherit?

Yes, from parent folders.

Q3. Which plugin gives folder permissions?

Role-based Authorization Strategy (or Matrix with folder settings).

Q4. Pattern for all cards jobs?

cards/.*

Q5. Why few global credentials?

Every job can use them.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards