/cicd-tooling-skills:jenkins-declarative-pipeline-per-repofrom cicd-tooling-skills
Authors and troubleshoots a per-repo Jenkinsfile using Jenkins Declarative Pipeline syntax — agent, stages, steps, post, environment, and when blocks — for a single repository's own build/test/deploy flow.
Ships in cicd-tooling-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A per-repo Jenkinsfile puts pipeline-as-code directly in the repository it
builds: it's reviewed in the same pull request as the code change, versioned
alongside it, and doesn't require a Jenkins administrator to touch a
separate job configuration. This skill covers Jenkins Declarative
Pipeline syntax specifically — the agent/stages/steps/post block
structure, environment and when conditionals, and the concrete
constructs (not generic CI concepts, which are covered in
ci-cd-pipeline-design)
needed to write a correct, maintainable Jenkinsfile that lives in a single
repo. It also covers the specific trade-off of the per-repo model versus
centralizing logic in a shared library, so a team can make an informed
choice rather than defaulting to copy-paste.
agent label, a when condition that
never matches).post block for Slack/email notification on failure, or a
always cleanup step (e.g. deleteDir()), to an existing pipeline.sh, script, or
a plugin step is idiomatic for a given task.workflow-aggregator), which ships Declarative Pipeline support.Jenkinsfile in the repo
root (or a path configured in the job) — the Jenkinsfile is not useful
without a job/multibranch config that reads it from SCM.docker
or linux-x64 label matching what the Jenkinsfile's agent block
requests. Confirm labels via Manage Jenkins → Nodes.${JENKINS_CRED_ID}), it never stores the
secret value itself.Docker Pipeline plugin installed if using agent { docker { ... } }.Start from the top-level pipeline block — Declarative Pipeline
requires exactly one pipeline { } block; you cannot mix bare Scripted
Pipeline steps at the top level.
pipeline {
agent any
options {
timestamps()
timeout(time: 30, unit: 'MINUTES')
disableConcurrentBuilds()
}
environment {
IMAGE_NAME = 'registry.example.com/myapp'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
}
}
Pin agent precisely, not just agent any. Use a label for a
specific node pool, or a docker agent for a reproducible toolchain
pinned by image tag:
agent {
docker {
image 'node:20.11-bullseye'
args '-v $HOME/.npm:/home/node/.npm'
}
}
agent any is fine for a small team with a homogeneous fleet; pin a
label or Docker image once tooling versions must be reproducible or
multiple agent types exist.
Model real stages, not one giant script block. Each logical phase
(checkout, build, test, package, deploy) is its own stage, so the
Jenkins UI/API shows per-stage pass/fail and timing:
stages {
stage('Build') {
steps { sh 'npm ci && npm run build' }
}
stage('Test') {
steps { sh 'npm test -- --reporter=junit --output=reports/junit.xml' }
post {
always {
junit 'reports/junit.xml'
}
}
}
stage('Package') {
steps {
sh 'docker build -t ${IMAGE_NAME}:${GIT_COMMIT} .'
}
}
stage('Deploy to staging') {
when {
branch 'main'
}
steps {
withCredentials([usernamePassword(credentialsId: '${JENKINS_CRED_ID}',
usernameVariable: 'REG_USER',
passwordVariable: 'REG_PASS')]) {
sh '''
echo "$REG_PASS" | docker login registry.example.com -u "$REG_USER" --password-stdin
docker push ${IMAGE_NAME}:${GIT_COMMIT}
'''
}
}
}
}
Use when for conditional stages instead of shell-level if guards
so skipped stages show clearly as "skipped" in the UI rather than as a
confusing green no-op:
when {
allOf {
branch 'main'
not { changeRequest() }
}
}
Drop into script { } only for real Groovy logic (loops,
conditionals on computed values) that the declarative DSL can't express
directly — keep these blocks small; anything non-trivial belongs in a
shared library function, see
jenkins-centralized-shared-library.
steps {
script {
def version = sh(script: 'git describe --tags --always', returnStdout: true).trim()
env.BUILD_VERSION = version
}
}
Add post for outcome-based actions — notification, cleanup,
artifact archiving — always at the pipeline level (and per-stage where
needed):
post {
success {
slackSend(channel: '#builds', color: 'good', message: "Build ${env.BUILD_NUMBER} succeeded")
}
failure {
slackSend(channel: '#builds', color: 'danger', message: "Build ${env.BUILD_NUMBER} failed: ${env.BUILD_URL}")
}
always {
archiveArtifacts artifacts: 'dist/**', allowEmptyArchive: true
cleanWs()
}
}
Validate syntax before pushing using the Jenkins CLI or the
Jenkinsfile linter endpoint:
curl -s -X POST -u ${JENKINS_USER}:${JENKINS_API_TOKEN} \
-F "jenkinsfile=<Jenkinsfile" \
https://jenkins.example.com/pipeline-model-converter/validate
Catching a WorkflowScript: 12: Expected one of ... error here is far
faster than waiting for a real build to fail at parse time.
script {} only
for logic the DSL genuinely can't express, and keep those blocks under
~10 lines — anything bigger is a sign the logic belongs in a shared
library function instead of inline Groovy (see
jenkins-groovy-scripting-best-practices).options { timeout(...) } and disableConcurrentBuilds() (or
options { skipDefaultCheckout() } where appropriate) explicitly — a
Jenkinsfile with no timeout can hang a stuck agent indefinitely, tying up
an executor.withCredentials, credentialsId:),
never inline — Jenkins masks values referenced this way in console
output automatically; a value assigned to a plain env var from a
credential is not masked.junit/archiveArtifacts/recordIssues (Warnings Next Generation
plugin) to surface structured results in the Jenkins UI rather than only
a pass/fail exit code — this mirrors the "fail fast and make failures
actionable" guidance in
ci-cd-pipeline-design.Symptom: Pipeline fails immediately with
WorkflowScript: 3: Expected one of "pipeline", ... @ line 3, column 1.
Fix: Declarative Pipeline requires the single top-level pipeline {}
block with no bare Groovy statements or Scripted-style node {} outside
it; move any Scripted-style code inside a script {} block within a
steps block.
Symptom: deploy stage runs on every branch, including feature
branches, even though it's "supposed to" only run on main.
Fix: Add an explicit when { branch 'main' } (or when { allOf { branch 'main'; not { changeRequest() } } }) to the stage — without it, a
Multibranch Pipeline job runs every stage on every discovered branch.
Symptom: A credential value shows up in plaintext in the build
console log.
Fix: Never assign a credential to a plain environment variable via
environment { TOKEN = credentials('id') } followed by manual echo, and
never sh "curl -H Authorization: ${TOKEN}" without quoting through
withCredentials's masked bindings — use withCredentials([...]) and
keep the secret variable inside its scope; Jenkins only masks values it
knows are secret-bound in that block.
Symptom: Build hangs for hours consuming an executor after an agent
goes unresponsive mid-sh step.
Fix: Add options { timeout(time: 30, unit: 'MINUTES') } at the
pipeline or stage level so a stuck step is killed and the executor freed
instead of blocking indefinitely.
Symptom: Two builds of the same branch run concurrently and race on
a shared resource (e.g. both push to the same tag).
Fix: Add options { disableConcurrentBuilds() }, or narrow it with
disableConcurrentBuilds(abortPrevious: true) (Jenkins 2.263+) so a new
build cancels the superseded one rather than running in parallel.
Scenario: A single Node.js service repo needs its own Jenkinsfile:
lint/test on every branch and PR, Docker build + push to a registry only on
main, with Slack notification on failure.
Jenkinsfile (repo root):
pipeline {
agent {
docker { image 'node:20.11-bullseye' }
}
options {
timestamps()
timeout(time: 20, unit: 'MINUTES')
disableConcurrentBuilds(abortPrevious: true)
}
environment {
IMAGE_NAME = 'registry.example.com/myapp'
}
stages {
stage('Install & Lint') {
steps {
sh 'npm ci'
sh 'npm run lint'
}
}
stage('Test') {
steps {
sh 'npm test -- --reporter=junit --outputFile=reports/junit.xml'
}
post {
always { junit 'reports/junit.xml' }
}
}
stage('Build & Push image') {
when { branch 'main' }
steps {
sh 'docker build -t ${IMAGE_NAME}:${GIT_COMMIT} .'
withCredentials([usernamePassword(credentialsId: '${JENKINS_CRED_ID}',
usernameVariable: 'REG_USER',
passwordVariable: 'REG_PASS')]) {
sh '''
echo "$REG_PASS" | docker login registry.example.com -u "$REG_USER" --password-stdin
docker push ${IMAGE_NAME}:${GIT_COMMIT}
'''
}
}
}
}
post {
failure {
slackSend(channel: '#builds', color: 'danger',
message: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER} (${env.BUILD_URL})")
}
always {
cleanWs()
}
}
}
This runs identically whether triggered by a PR or a main push, only
gating the image push stage with when { branch 'main' }, and keeps the
registry credential scoped to the one withCredentials block that needs it.
script {} blocks.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
jenkins, jenkinsfile, declarative-pipeline, ci-cd, pipeline-as-code
13 skills in this plugin
/aws-codepipeline-and-codedeployDesigns AWS CodePipeline stage/action structure and integrates it with CodeDeploy deployment groups and appspec.yml for EC2/on-prem, ECS, or Lambda deploys. Use when the user asks to "set up CodePipeline," "add a CodeDeploy deployment group," "write an appspec.yml," "wire CodePipeline to CodeDeploy," "troubleshoot a stuck/failed CodePipeline action," or "add a manual approval action before an AWS deploy."
/azure-pipelines-yaml-and-multi-stageDesigns Azure Pipelines YAML (azure-pipelines.yml), multi-stage pipelines with environments/approvals, templates for reuse across repos, and self-hosted vs. Microsoft-hosted agent pools. Use when the user asks to "set up Azure Pipelines," "write an azure-pipelines.yml," "add a multi-stage pipeline with approvals," "create a reusable pipeline template," "configure a self-hosted agent pool," or "troubleshoot a stuck/failed Azure DevOps pipeline run."
/bamboo-pipeline-yaml-and-java-specsAuthors Atlassian Bamboo pipeline definitions using either Bamboo Specs (Java DSL, compiled and run to create/update a plan) or Bamboo's YAML specs format, covering plan/job/stage structure and how a Specs definition links to and updates a live Bamboo plan. Use when the user asks to "write a Bamboo Specs plan," "convert our Bamboo plan to code," "add a stage/job to a Bamboo build," "use Bamboo YAML specs," or "troubleshoot a Bamboo Specs plan not updating the linked plan."
/complete-cicd-pipeline-deployment-for-kubernetes-from-scratchBuilds a complete CI/CD pipeline for a Kubernetes-targeted service from an empty repo — source checkout, container image build, SCA/SAST security gates, registry push, and a deploy step that ends at a GitOps handoff (committing a new image tag to a manifests repo) rather than a direct kubectl apply or helm upgrade against the cluster. Use when the user asks to "build a full CI/CD pipeline for Kubernetes from scratch," "set up CI/CD that ends in a GitOps commit, not a cluster deploy," "wire a container build pipeline into Argo CD/Flux," or "go from an empty repo to a Kubernetes service deployed via GitOps end-to-end."
/complete-cicd-pipeline-deployment-for-serverless-from-scratchBuilds a complete CI/CD pipeline for a serverless (AWS Lambda-style) function from an empty repo — source checkout, zip/layer packaging (not a container image), SCA/SAST security gates, package upload, and a SAM/Serverless-Framework/CDK-style deploy step with alias-based canary traffic shifting, distinct from Kubernetes rollout mechanics. Use when the user asks to "build a full CI/CD pipeline for a Lambda function from scratch," "set up serverless CI/CD with canary traffic shifting," "wire SAM/Serverless Framework deploys into a pipeline," or "go from an empty repo to a function deployed with an alias-based canary."
/complete-cicd-pipeline-for-vm-based-workloads-from-scratchBuilds a complete CI/CD pipeline for a VM-based workload from an empty repo — source checkout, build, immutable machine-image baking (Packer-style AMI, not a container image or a zip), SCA/SAST security gates, and a deploy step that's either a blue-green instance-group swap or a config-management push (Ansible) to existing long-lived VMs, distinct from both container and serverless deploy mechanics. Use when the user asks to "build a full CI/CD pipeline for EC2/VM workloads from scratch," "bake an AMI in CI and roll it out blue-green," "push Ansible-managed config to existing VMs from a pipeline," or "go from an empty repo to a VM fleet deployed with a golden image and Auto Scaling Group swap."
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin cicd-tooling-skillsOther plugins' skills for the same job