0
0
Jenkinsdevops~5 mins

Declarative pipeline syntax in Jenkins - Commands & Configuration

Choose your learning style9 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.