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)
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
- 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.
Setup — install Jenkins
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
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)
- Install the Pipeline plugin. Go to Manage Jenkins → Plugins → Available, search "Pipeline", install. (The suggested plugins already include it.)
- Add Git. On the Jenkins server, make sure git is installed so Jenkins can fetch your code.
- 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.
- 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
- Open Claude (the chat app, or the Claude extension in VS Code).
- Describe your project in plain English. Say the language, the build tool, and the test command. Use the prompt below.
- Read what Claude writes. Check the stages make sense and no secret is hard-coded.
- Save the file as Jenkinsfile in the root of your project, then push it to Git.
- Create the job in Jenkins: New Item → Pipeline → name it → Pipeline from SCM → Git → paste your repo URL. Set Script Path to Jenkinsfile. Save.
- 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
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.
Worked examples (simplest first)
Example 1 — the smallest pipeline
Start here to prove Jenkins works. It just prints a message.
Jenkinsfile · hellopipeline {
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 + testpipeline {
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.
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.' }
}
}
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
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 / tool | How hard | Best for | Note |
|---|---|---|---|
| Freestyle job | Easiest | One or two clicks, tiny tasks | Clicks only; not saved in Git, hard to share |
| Declarative pipeline | Easy | Most projects | Clear stages blocks; the one Claude should write |
| Scripted pipeline | Harder | Complex, custom logic | Full 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
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.
- 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.
- 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. - 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.
- Use a smaller model for easy jobs. A light model is fine for a hello pipeline; save the strong model for tricky fixes.
- 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).
What is a Jenkinsfile?
Declarative vs Scripted
stages blocks. Easy. Use this.