# Day 26 Task: Jenkins Declarative Pipeline

One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins

## Some terms for your Knowledge

**What is Pipeline -** A pipeline is a collection of steps or jobs interlinked in a sequence.

**Declarative:** Declarative is a more recent and advanced implementation of a pipeline as a code.

**Scripted:** Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.

# Why you should have a Pipeline

The definition of a Jenkins Pipeline is written into a text file (called a [`Jenkinsfile`](https://www.jenkins.io/doc/book/pipeline/jenkinsfile)) which in turn can be committed to a project’s source control repository.  
This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

**Creating a** `Jenkinsfile` and committing it to source control provides a number of immediate benefits:

* Automatically creates a Pipeline build process for all branches and pull requests.
    
* Code review/iteration on the Pipeline (along with the remaining source code).
    
* # Pipeline syntax
    
* ```plaintext
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    //
                }
            }
            stage('Test') {
                steps {
                    //
                }
            }
            stage('Deploy') {
                steps {
                    //
                }
            }
        }
    }
    ```
    
    ***Discuss with the help of a Task:***
    
    ### **Task-01**
    
    1. ***Create a New Item, this time select the pipeline option not a Freestyle Project.***
        
    
    ***A) Create a Declarative pipeline in Jenkins, go to jenkins UI and click on New item.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQEx2ElrXHbS0g/article-inline_image-shrink_400_744/0/1683217899130?e=2147483647&v=beta&t=2M4KLE-pldfzLXEH8b6LuSsuVYyrslwjvSMl3v8zQQQ align="left")
    
    Jenkins Dashboard Diagram.
    
    ***B) Enter the pipeline name & select Pipeline,and the click on ok.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGfs_BBpaMwIA/article-inline_image-shrink_400_744/0/1683217972048?e=2147483647&v=beta&t=8oC4XgD8vG87oO1Sd7_8BydMKE3fz7RTw7JXq6AAKHs align="left")
    
    Enter a name select pipeline Diagram.
    
    ***C) After create the pipeline, Now go to project configuration page, In general give a description of your project (I will add This is a Jenkins Pipeline Hello world).***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQEc4rrfWdTBYw/article-inline_image-shrink_400_744/0/1683218114123?e=2147483647&v=beta&t=knSw_tYyZoOlEHb-A3v1wEUMZ1sWYfBIYzdl_gLeunc align="left")
    
    In General option give a Description of the Pipeline Diagram.
    
    ***D) Noe to to the 3rd option Pipeline (Definition option) scroll down select the pipeline script.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQHquKPINjxYpQ/article-inline_image-shrink_400_744/0/1683218402333?e=2147483647&v=beta&t=3cmO6VijgyGRiOPKRT_L0Vxo-B_lSGdamItWGgzvFzA align="left")
    
    3Rd option Pipeline and add a Pipeline script Diagram.
    
    ***E) Write a Pipeline Script of "Hello world"***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQHeoJaQ5m4Xog/article-inline_image-shrink_400_744/0/1683218443134?e=2147483647&v=beta&t=n-gcF-rc0tJn8EN6Ktgy-4wpUF187byZELQLAm1JUBk align="left")
    
    Now Write a Script of "Hello World" Diagram.
    
    ***Before i will move for the Build the Pipeline, Let's discuss a small Description of every points which are used for the Script.***
    
    ***1) Pipeline:- Declarative pipeline should start with the pipeline and this is the mandatory block.***
    
    ***2) Agent:- It specifies the jenkins build job should run. we have used the agent as any.***
    
    ***3) Stages:- It consists of the different executable stage block.***
    
    ### **Steps:-**
    
    ***Steps block consist of the actual operation which needs to be performed inside Jenkins. in the Diagram i will use the "Hello World"***
    
    ***F) Build the project, you can manually build the project by clicking on the "Build now". link in the project's main page dashboard.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQE1ZNS2sJG07A/article-inline_image-shrink_400_744/0/1683218950811?e=2147483647&v=beta&t=xWyIXRQ5GkXuvzFr_tEh8lT0PxsySlsz6Yv2C1If6n4 align="left")
    
    Click on the Build now Diagram.
    
    ***G) After a build a completed, you can view the console output by clicking on the "Console output" in the build page.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGLoChqpOxWnA/article-inline_image-shrink_400_744/0/1683219087384?e=2147483647&v=beta&t=_VcU55qj5g_zRaWMGG4gqt7LkIHqC9lqnj8HZcYYQVo align="left")
    
    Console Output of the Declaritive Jenkins Pipeline Diagram.
    
    ***H) Now your pipeline are Successfully and Printed the "hello World"***
    
    ***I) you can see the each stage view by Clicking on the "Full stage View" in the project main page.***
    
    ![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGDRj5p1kzHOA/article-inline_image-shrink_400_744/0/1683219256145?e=2147483647&v=beta&t=aQRzo4dOHvJgb_b0ri8zy1KbAZBLgMmYc3V_VyinEOU align="left")
    
    stage view by Clicking on the "Full stage View Diagram.
    
    ***Now in the today, I have Successfully run the Declarative Pipeline inJenkins.***
    
    ***Thank you for reading this Article !! Stay Connected!!***
    
    ***Happy Learning!!***
