How do you create a pipeline in Jenkins?

Creating a Jenkins pipeline means defining the build, test, and deploy stages as code in a Jenkinsfile, which Jenkins executes automatically whenever changes are detected in the source repository.

Key Points: • Create a new Jenkins job and choose the "Pipeline" project type rather than a legacy freestyle job. • Define the pipeline using declarative or scripted syntax in a Jenkinsfile, checked into the project's repository as pipeline-as-code. • Stages typically include checkout, build, test, and deploy, each containing one or more steps such as shell commands. • Jenkins can trigger the pipeline via SCM polling or, more commonly, a webhook fired on every push or pull request. • Pipeline runs are visualized stage-by-stage in the Jenkins UI (or Blue Ocean), making it easy to spot where a failure occurred.

Example: A team adds a Jenkinsfile to the repo root with build, test, and deploy stages; after connecting a GitHub webhook, every push to main automatically triggers Jenkins to run mvn verify and then deploy the resulting artifact.

Code Example:

pipeline {
    agent any
    stages {
        stage('Checkout') { steps { checkout scm } }
        stage('Build')    { steps { sh 'mvn clean package' } }
        stage('Test')     { steps { sh 'mvn test' } }
        stage('Deploy')   { steps { sh './deploy.sh' } }
    }
}

Interview Tip: A concise interview answer is:

"I create a Pipeline job in Jenkins and define the stages in a Jenkinsfile checked into the repo, using declarative syntax for checkout, build, test, and deploy steps. Jenkins then triggers that pipeline automatically via a webhook whenever code is pushed."