Bird
Raised Fist0
Jenkinsdevops~5 mins

Declarative pipeline syntax in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you want to automate software building and testing, you need a clear way to tell Jenkins what steps to run. Declarative pipeline syntax lets you write these steps in a simple, organized way that Jenkins understands easily.
When you want to automate building your app every time you save code.
When you want to run tests automatically after building your software.
When you want to deploy your app to a server after tests pass.
When you want to keep your automation steps easy to read and change.
When you want to share your build process with your team in a clear format.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the app'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying the app'
      }
    }
  }
}

pipeline: The main block that defines the whole process.

agent any: Runs the pipeline on any available machine.

stages: Groups the steps into phases like Build, Test, and Deploy.

stage: Defines each phase with a name.

steps: Lists the commands or actions to run in each stage.

Commands
This command uploads the Jenkinsfile to Jenkins to create or update the pipeline job. It tells Jenkins what steps to run.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command starts the pipeline job named 'my-pipeline' to run the steps defined in the Jenkinsfile.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1
This command shows the output logs of the first run of the 'my-pipeline' job so you can see what happened during the build.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the app [Pipeline] echo Running tests [Pipeline] echo Deploying the app [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Declarative pipeline syntax organizes your automation steps clearly so Jenkins can run them reliably.

Common Mistakes
Writing pipeline steps outside the 'stages' block
Jenkins expects steps inside stages; putting them outside causes errors or ignored commands.
Always place your build, test, and deploy steps inside named stages within the 'stages' block.
Not specifying an agent
Without an agent, Jenkins doesn't know where to run the pipeline, causing failures.
Include 'agent any' or specify a particular agent to tell Jenkins where to run the pipeline.
Using scripted pipeline syntax inside a declarative pipeline
Declarative syntax has strict rules; mixing scripted code can cause parsing errors.
Stick to declarative syntax rules or use scripted pipelines separately.
Summary
Write your automation steps inside a Jenkinsfile using declarative syntax.
Use 'pipeline', 'agent', 'stages', 'stage', and 'steps' blocks to organize your process.
Upload the Jenkinsfile to Jenkins and run the pipeline to automate building, testing, and deploying.

Practice

(1/5)
1. Which block is mandatory to start a Jenkins declarative pipeline?
easy
A. stage
B. agent
C. steps
D. pipeline

Solution

  1. Step 1: Understand the pipeline structure

    The pipeline block is the root block that wraps the entire declarative pipeline script.
  2. Step 2: Identify mandatory blocks

    While agent, stages, and steps are important, the pipeline block is always required to define the pipeline.
  3. Final Answer:

    <code>pipeline</code> -> Option D
  4. Quick Check:

    Root block = pipeline [OK]
Hint: Look for the root block that wraps everything [OK]
Common Mistakes:
  • Confusing stage as root block
  • Thinking steps is the top block
  • Assuming agent is mandatory alone
2. Which of the following is the correct syntax to define an agent that runs on any available node?
easy
A. agent any
B. agent: any
C. agent { any }
D. agent = any

Solution

  1. Step 1: Recall agent syntax in declarative pipeline

    The correct syntax to specify an agent that runs on any available node is agent any without punctuation.
  2. Step 2: Check other options for syntax errors

    Options with colons, braces, or equals signs are invalid in declarative pipeline syntax for agent.
  3. Final Answer:

    agent any -> Option A
  4. Quick Check:

    Agent syntax is simple: agent any [OK]
Hint: Remember: no punctuation, just 'agent any' [OK]
Common Mistakes:
  • Adding colons or braces around 'any'
  • Using equals sign instead of space
  • Confusing scripted pipeline syntax
3. What will be the output of this Jenkins declarative pipeline snippet?
pipeline {
  agent any
  stages {
    stage('Hello') {
      steps {
        echo 'Hello World'
      }
    }
  }
}
medium
A. Fails because 'echo' is not allowed in declarative pipeline
B. Prints 'Hello World' in the build log
C. Pipeline runs but no output is shown
D. Syntax error due to missing agent label

Solution

  1. Step 1: Analyze the pipeline structure

    The pipeline has a valid agent any, one stage named 'Hello', and a step that echoes 'Hello World'.
  2. Step 2: Understand the echo step behavior

    The echo step prints the given message to the Jenkins build log during execution.
  3. Final Answer:

    Prints 'Hello World' in the build log -> Option B
  4. Quick Check:

    echo prints message to log [OK]
Hint: echo always prints text to Jenkins console [OK]
Common Mistakes:
  • Thinking echo is disallowed in declarative pipelines
  • Assuming missing agent label causes error here
  • Believing pipeline runs silently without output
4. Identify the error in this declarative pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'make build'
      }
    }
  }
  steps {
    echo 'Done'
  }
}
medium
A. The sh command is invalid in declarative pipeline
B. Missing agent block
C. The steps block is misplaced outside stage
D. The stage block must be outside stages

Solution

  1. Step 1: Review pipeline block structure

    In declarative pipelines, steps must be inside a stage block, which is inside stages.
  2. Step 2: Locate the misplaced steps block

    The steps block with echo 'Done' is incorrectly placed directly under pipeline, outside any stage.
  3. Final Answer:

    The <code>steps</code> block is misplaced outside <code>stage</code> -> Option C
  4. Quick Check:

    steps must be inside stage [OK]
Hint: steps always go inside a stage block [OK]
Common Mistakes:
  • Placing steps directly under pipeline
  • Confusing agent placement
  • Thinking sh is invalid in declarative pipelines
5. You want to run two stages in parallel in a declarative pipeline. Which snippet correctly defines parallel stages?
hard
A.
pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('Test') {
          steps { echo 'Testing' }
        }
        stage('Build') {
          steps { echo 'Building' }
        }
      }
    }
  }
}
B.
pipeline {
  agent any
  stages {
    parallel {
      stage('Test') {
        steps { echo 'Testing' }
      }
      stage('Build') {
        steps { echo 'Building' }
      }
    }
  }
}
C.
pipeline {
  agent any
  stages {
    stage('Test') {
      steps { echo 'Testing' }
    }
    stage('Build') {
      steps { echo 'Building' }
    }
  }
}
D.
pipeline {
  agent any
  parallel {
    stage('Test') {
      steps { echo 'Testing' }
    }
    stage('Build') {
      steps { echo 'Building' }
    }
  }
}

Solution

  1. Step 1: Understand parallel syntax in declarative pipeline

    Parallel stages must be nested inside a stage block using the parallel block.
  2. Step 2: Check each option for correct structure

    pipeline {
      agent any
      stages {
        stage('Parallel') {
          parallel {
            stage('Test') {
              steps { echo 'Testing' }
            }
            stage('Build') {
              steps { echo 'Building' }
            }
          }
        }
      }
    }
    correctly places parallel inside a stage named 'Parallel' with nested stages 'Test' and 'Build'. Other options place parallel incorrectly or omit it.
  3. Final Answer:

    Option A with parallel inside a stage block -> Option A
  4. Quick Check:

    parallel inside stage block = correct [OK]
Hint: Parallel block must be inside a stage block [OK]
Common Mistakes:
  • Placing parallel directly under stages
  • Omitting the parallel block
  • Placing parallel outside pipeline
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard