Ravindra BagaleCourses & study guides Track your progress
AI CODING · PER-TASK SERIES

How to Create a Jenkins Pipeline Using Claude

Build a working Jenkins pipeline step by step. You write plain English, Claude writes the pipeline file, you run it. No hard words. Real prompts and a real working file are below.

In short: a Jenkins pipeline runs your build and tests by itself every time you push code. Claude is an AI assistant that can write the pipeline file for you. You check it, paste it, and run it.

Why use a pipeline (and why let Claude help)

WHY

Doing build and test by hand is slow and easy to forget. A pipeline does the same steps the same way every time. That means fewer mistakes and faster releases.

Writing the pipeline file can feel scary for a beginner. Claude turns your plain words into the correct file. You still read it and stay in control — Claude helps, you decide.

What these words mean

WHAT
  • Jenkins: a free, open-source tool that runs your build and test steps automatically. It is called a CI/CD server.
  • CI/CD: Continuous Integration / Continuous Delivery. Simple meaning: every code change is built, tested, and made ready to ship, automatically.
  • Pipeline: the list of steps Jenkins runs in order, for example: get code → build → test → deploy.
  • Jenkinsfile: a text file in your project that describes the pipeline. Jenkins reads it.
  • Stage: one named part of the pipeline, like "Build" or "Test".
  • Claude: an AI assistant. You type a request; it writes code or text back.

The pipeline, top to bottom

Here is the flow you will build. The dot moves down to show the order the stages run.

1 · Checkout get the latest code 2 · Build install and compile 3 · Test run automatic tests 4 · Deploy ship if tests pass

Setup — install Jenkins

SETUP · INSTALL

Pick one way. The Docker way is fastest for practice.

Install · Docker (fastest)
# run Jenkins in one container; opens on http://localhost:8080
docker run --name jenkins -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts
Install · Ubuntu (long-term server)
sudo apt-get update
sudo apt-get install -y openjdk-17-jre   # Jenkins needs Java
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key \
  | sudo tee /usr/share/keyrings/jenkins-keyring.asc >/dev/null
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/" \
  | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt-get update && sudo apt-get install -y jenkins
sudo systemctl enable --now jenkins   # start now and on boot
First unlock: open http://localhost:8080. It asks for a password. Get it with:
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
# or on Ubuntu:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Then choose Install suggested plugins and create your admin user.

Settings you need (one time)

SETUP · SETTINGS
  1. Install the Pipeline plugin. Go to Manage Jenkins → Plugins → Available, search "Pipeline", install. (The suggested plugins already include it.)
  2. Add Git. On the Jenkins server, make sure git is installed so Jenkins can fetch your code.
  3. Add credentials. Go to Manage Jenkins → Credentials → System → Global → Add Credentials. Add your Git token or deploy key here. Never type secrets inside the Jenkinsfile.
  4. Point to your repo. You will create a "Pipeline" job and give it your Git URL in the next section.

How to build it with Claude

HOW
  1. Open Claude (the chat app, or the Claude extension in VS Code).
  2. Describe your project in plain English. Say the language, the build tool, and the test command. Use the prompt below.
  3. Read what Claude writes. Check the stages make sense and no secret is hard-coded.
  4. Save the file as Jenkinsfile in the root of your project, then push it to Git.
  5. Create the job in Jenkins: New Item → Pipeline → name it → Pipeline from SCM → Git → paste your repo URL. Set Script Path to Jenkinsfile. Save.
  6. Click "Build Now". Watch the stages turn green. If one is red, open its log and go to the fix step below.

The prompt to give Claude

COPY THIS PROMPT

You are helping me write a Jenkins declarative Jenkinsfile. My project is a Node.js app. Build tool: npm. Install with npm ci. Test with npm test. I deploy only on the main branch by running npm run deploy. Please: 1) Use a declarative pipeline with stages Checkout, Build, Test, Deploy. 2) Run Deploy only when the branch is main. 3) Use a Jenkins credential id deploy-token as an environment variable, do not hard-code any secret. 4) Add short comments so a beginner understands each stage. Return only the Jenkinsfile.

Change three things for your own project: the language (Node, Python, Java…), the install/build command, and the test command. Keep the "no hard-coded secret" line every time.

Worked examples (simplest first)

Example 1 — the smallest pipeline

Start here to prove Jenkins works. It just prints a message.

Jenkinsfile · hello
pipeline {
  agent any
  stages {
    stage('Say hello') {
      steps {
        echo 'Hello from my first pipeline!'
      }
    }
  }
}

Create a Pipeline job, paste this in the box (or in a Jenkinsfile), click Build Now. You should see one green stage.

Example 2 — build and test a Node.js app

Now do real work: install packages and run tests.

Jenkinsfile · build + test
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps { checkout scm }      // get the code from Git
    }
    stage('Build') {
      steps { sh 'npm ci' }       // clean install of packages
    }
    stage('Test') {
      steps { sh 'npm test' }     // run automatic tests
    }
  }
}

Example 3 — full pipeline with deploy and a secret (the real one)

This is the complete, working file Claude gives for the prompt above. Deploy runs only on main, and the secret comes from Jenkins credentials, not from the file.

Jenkinsfile · complete working example
pipeline {
  agent any

  // read a stored secret by its credential id; never write the real value here
  environment {
    DEPLOY_TOKEN = credentials('deploy-token')
  }

  options {
    timestamps()                 // add time to every log line
    timeout(time: 20, unit: 'MINUTES')
  }

  stages {
    stage('Checkout') {
      steps { checkout scm }
    }
    stage('Build') {
      steps { sh 'npm ci' }
    }
    stage('Test') {
      steps { sh 'npm test' }
    }
    stage('Deploy') {
      when { branch 'main' }     // only deploy from the main branch
      steps {
        sh 'npm run deploy'      // this script uses $DEPLOY_TOKEN
      }
    }
  }

  post {
    success { echo 'Pipeline passed. Build is ready.' }
    failure { echo 'Pipeline failed. Open the red stage log.' }
  }
}
Safety: notice the token is read with credentials('deploy-token'). Do not paste your real token into the Jenkinsfile or into Claude. Share only placeholders like deploy-token.

When a stage turns red — ask Claude to fix it

COPY THIS PROMPT

My Jenkins "Test" stage failed. Here is the last 20 lines of the log: [paste log]. Here is my Jenkinsfile: [paste file]. In simple words, tell me the likely cause and give me the smallest fix.

Which pipeline style should you pick?

Jenkins has more than one way to write jobs. For almost everyone, use Declarative.

Style / toolHow hardBest forNote
Freestyle jobEasiestOne or two clicks, tiny tasksClicks only; not saved in Git, hard to share
Declarative pipelineEasyMost projectsClear stages blocks; the one Claude should write
Scripted pipelineHarderComplex, custom logicFull Groovy code; only when Declarative is not enough

Rule of thumb: ask Claude for a Declarative Jenkinsfile unless you have a special reason.

Quick quiz

1. What does a Jenkins pipeline do for you?

It runs your steps (get code, build, test, deploy) the same way, automatically, every time.

2. Where should a secret like a deploy token live?

In Jenkins Credentials, read with credentials('id'). Never inside the Jenkinsfile.

3. Which pipeline style should a beginner ask Claude to write?

Declarative. It is clear and easy to read.

4. What makes the Deploy stage run only on main?

The line when { branch 'main' } inside the Deploy stage.

Save tokens and money

SMART AI USE

Every word you send and get back costs tokens (small pieces of text). Fewer tokens means lower cost and faster answers. Here is how to stay cheap while building pipelines.

  1. Ask for one thing. "Write a declarative Jenkinsfile for a Node app" beats a long story. A clear, short ask gives a shorter, better answer.
  2. Paste only what matters. For a failed stage, send the last 20 log lines, not the whole log. Share placeholders like deploy-token, never real secrets.
  3. Start a new chat per task. Long chats resend old text every time, so tokens add up fast. A fresh chat keeps the context short and cheap.
  4. Use a smaller model for easy jobs. A light model is fine for a hello pipeline; save the strong model for tricky fixes.
  5. Practise with a local model. Run Ollama on your own machine for endless free tries, then use a paid or free-tier API only for the final check.

Save these cards

Three cards to revise fast (same style as the Excel and cyber cards).

INTERVIEW CARDAI Coding

What is a Jenkinsfile?

Answer: a text file in your project that lists the pipeline stages (Checkout, Build, Test, Deploy). Jenkins reads it and runs the stages in order.
Say this too: keep it in Git so the whole team shares the same steps.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Declarative vs Scripted

Declarative: clear stages blocks. Easy. Use this.
Scripted: full Groovy code. Powerful but harder.
Pick: Declarative for almost every project.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Pipeline with Claude in 3 steps

1 Describe: tell Claude your language, build and test commands.
2 Check: read the Jenkinsfile; no secret hard-coded.
3 Run: push the file, make a Pipeline job, click Build Now.
Ravindra Bagale · ravindrabagale.com/ai-coding
Next in this series: the same task with ChatGPT, Gemini, Grok and DeepSeek — then a side-by-side "same task, 5 models" page with a verdict card.