0
0
Jenkinsdevops~5 mins

CI/CD pipeline mental model in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
A CI/CD pipeline automates the steps to build, test, and deliver software. It helps teams release code faster and with fewer errors by running tasks automatically when code changes.
When you want to automatically check if new code works before adding it to the main project
When you want to build your software and run tests every time a developer saves changes
When you want to deploy your app to a server automatically after tests pass
When you want to catch errors early by running checks on every code update
When you want to save time by automating repetitive tasks like building and testing
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                // Add build commands here
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
                // Add test commands here
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
                // Add deploy commands here
            }
        }
    }
}

This Jenkinsfile defines a simple CI/CD pipeline with three stages: Build, Test, and Deploy.

agent any means the pipeline can run on any available Jenkins agent.

Each stage groups related steps: building the app, running tests, and deploying the app.

The steps section contains commands that Jenkins runs for each stage.

Commands
This command creates a new Jenkins job named 'my-pipeline' using the Jenkinsfile to define the pipeline steps.
Terminal
jenkins-cli create-job my-pipeline < Jenkinsfile
Expected OutputExpected
Job 'my-pipeline' created successfully.
This command starts the pipeline job to run the build, test, and deploy stages automatically.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 for job 'my-pipeline'.
This command shows the output logs of the first build of the 'my-pipeline' job so you can see what happened during each stage.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the application... [Pipeline] echo Running tests... [Pipeline] echo Deploying application... [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: a CI/CD pipeline automates building, testing, and deploying your code to catch errors early and deliver faster.

Common Mistakes
Not defining stages clearly in the Jenkinsfile
Without clear stages, Jenkins cannot organize tasks properly, making the pipeline hard to understand and maintain.
Define each major step (build, test, deploy) as separate stages in the Jenkinsfile.
Running deployment before tests complete
This can cause broken or buggy code to be deployed, leading to failures in production.
Always run tests stage before deployment stage to ensure code quality.
Not checking pipeline logs after running
You miss important information about errors or warnings that happened during the pipeline run.
Always review the console output logs to verify each stage ran successfully.
Summary
Create a Jenkinsfile to define build, test, and deploy stages for your pipeline.
Use Jenkins CLI commands to create and start the pipeline job.
Check the pipeline logs to confirm each stage completed successfully.