CloudOpsGuide
jenkins

Jenkinsfile Generator: CI/CD Pipelines from Zero

Beginner
12 minutes
October 2026
CloudOpsGuide Team

Jenkinsfile Generator: CI/CD Pipelines from Zero

Understand Jenkinsfile syntax and structure from scratch — declarative pipelines, stages, agents, and real examples you can copy.

Table of Contents

Declarative vs Scripted

Two Jenkinsfile syntaxes:

DeclarativeScripted
SyntaxStructured, opinionatedRaw Groovy
Learning curveEasyHard
Flexibility90% of use casesFull control
RecommendationStart hereOnly if needed

This guide uses Declarative — it's what most teams use.

Pipeline Anatomy

pipeline {
    agent any                    // where it runs

    environment {                // env vars for all stages
        APP_NAME = 'my-app'
    }

    options {                    // pipeline options
        timeout(time: 30, unit: 'MINUTES')
    }

    stages {                     // the actual work
        stage('Build') {
            steps {
                sh 'make build'
            }
        }
    }

    post {                       // always runs at the end
        always {
            cleanWs()
        }
    }
}

Essential Directives

agent — Where Stages Run

agent any                                    // any available agent
agent none                                   // set per-stage instead
agent { label 'docker' }                     // agent with a label
agent {
    docker {                                 // inside a container
        image 'node:20-alpine'
        args '-v /cache:/cache'
    }
}
agent {
    kubernetes {                             // K8s pod agent
        yaml '''
          spec:
            containers:
            - name: node
              image: node:20
        '''
    }
}

environment — Variables

environment {
    NODE_ENV = 'production'
    CREDS = credentials('my-secret-id')     // auto-injects CREDS_USR / CREDS_PSW
}

parameters — User Input

parameters {
    string(name: 'VERSION', defaultValue: '1.0.0')
    choice(name: 'ENV', choices: ['dev', 'staging', 'prod'])
    booleanParam(name: 'DEPLOY', defaultValue: false)
}
// Access via params.ENV

when — Conditional Stages

stage('Deploy') {
    when {
        allOf {
            branch 'main'
            expression { params.DEPLOY }
        }
    }
    steps { sh './deploy.sh' }
}

post — After Pipeline

post {
    always  { echo 'runs regardless' }
    success { echo 'only on success' }
    failure { echo 'only on failure' }
    cleanup { cleanWs() }              // runs last, always
}

Complete Examples

Minimal Node.js App

pipeline {
    agent {
        docker { image 'node:20-alpine' }
    }
    stages {
        stage('Install') {
            steps { sh 'npm ci' }
        }
        stage('Test') {
            steps { sh 'npm test' }
        }
        stage('Build') {
            steps { sh 'npm run build' }
        }
    }
}

Parallel Stages

stage('Checks') {
    parallel {
        stage('Lint')    { steps { sh 'npm run lint' } }
        stage('Types')   { steps { sh 'npm run typecheck' } }
        stage('Tests')   { steps { sh 'npm test' } }
    }
}

Multi-Environment Deploy

stage('Deploy Dev') {
    when { branch 'develop' }
    steps { sh './deploy.sh dev' }
}
stage('Deploy Prod') {
    when { branch 'main' }
    input { message 'Deploy to production?' ok 'Deploy' }
    steps { sh './deploy.sh prod' }
}

Credentials Handling

stage('Push') {
    steps {
        withCredentials([usernamePassword(
            credentialsId: 'registry-creds',
            usernameVariable: 'REG_USER',
            passwordVariable: 'REG_PASS'
        )]) {
            sh 'echo $REG_PASS | docker login -u $REG_USER --password-stdin'
        }
    }
}

Common Patterns

Retry on Flaky Steps

steps {
    retry(3) {
        sh 'npm ci'   // network flakiness
    }
}

Save Artifacts

post {
    always {
        archiveArtifacts artifacts: 'dist/**', fingerprint: true
        junit 'reports/**/*.xml'
    }
}

Shared Stash Between Stages

stage('Build') {
    steps {
        sh 'npm run build'
        stash includes: 'dist/**', name: 'built'
    }
}
stage('Deploy') {
    steps {
        unstash 'built'
        sh './deploy.sh'
    }
}

Debugging Pipelines

Check Syntax Before Committing

# Jenkins validates Jenkinsfile via HTTP API
curl -X POST -F "jenkinsfile=<Jenkinsfile" \
  "$JENKINS_URL/pipeline-model-converter/validate"

Blue Ocean / Pipeline Steps View

Click a failing build → see exactly which stage/step failed with timing.

Replay a Build

Jenkins → failed build → Replay — edit the Jenkinsfile inline and re-run without pushing a commit. Huge for debugging.

Common Errors

ErrorCause
No such DSL methodTypo in directive, or missing plugin
WorkflowScript missingSyntax error at top level
agent none + no stage agentEvery stage needs its own agent
Scripts not permittedNeeds script approval (admin)

Related Articles


Last Updated: October 2026
Author: CloudOpsGuide Team
Difficulty: Beginner
Estimated Reading Time: 12 minutes