enkins pipeline example hello world

Jenkins pipeline example hello world | Complete tutorial [2022]

This is the first part of the Jenkins pipeline example series. This blog will understand how to write a basic Jenkins pipeline example hello world using Jenkins declarative and scripted syntax.

What is Jenkins pipeline?

A Jenkins pipeline is nothing but a sequence of steps that are linked together. Jenkins provides many readily available plugins using which we can customize the Jenkins pipeline.

In Jenkins, there are two ways by which a user can define the pipeline.

  • Scripted syntax
  • Declarative syntax

Each syntax comes with its own advantage and disadvantages. More information about the Jenkins pipelines can be found here.

Jenkins pipeline example hello world

In this session, we will write a Jenkins hello world pipeline using Declarative pipeline syntax.

To create aDeclarative pipeline in Jenkins, go to Jenkins UI and click on New item.

provide the pipeline name and select Pipeline, and then click on ok

Jenkins pipeline example hello world
Now goto the pipeline session and paste the below code
pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}
jenkins pipeline example hello world

Code explanation

  1. Pipeline
    • The Declarative pipeline should start with the pipeline block and this is the mandatory block.
  2. Agent
    • Agent signifies where the Jenkins build job should run. In this case, we have selected agents as any.
    • Jenkins supports a wide variety of agents. The entire list of supported agents in Jenkins can be found here
  3. Stages/stage
    • stages block consists of different executable stage blocks.
    • At least one stage block is mandatory inside the stages block.
    • Here we have names stage as “Hello”
  4. Steps
    • Steps blocks consist of the actual operation which needs to be performed inside Jenkins.
    • In the above example, we are printing “Hello World“.

Click on the save button to save the Pipeline.

Now click on Build now button(1) to run the Pipeline.

Once the job runs successfully, you can view the logs as highlighted by number 2

Click on #1 to view logs and then goto the console output

Jenkins pipeline example hello world logs

As you can see from the above output, the Pipeline ran successfully and printed Hello World

Jenkins scripted pipeline example hello world

This session will help understand how to create a Jenkins hello-world pipeline using the Jenkins scripted pipeline syntax.

The scripted pipeline syntax is the old format to define the Jenkins job as a pipeline. Type the below command in the script session of the Jenkins job

node {
    stage('Hello') {
        echo 'hello'
    }

The Jenkins scripted pipeline always starts with a node block. Follow my tutorial on Jenkins scripted pipeline to get a complete understanding of Jenkins scripted pipeline.

Jenkins pipeline syntax generator

Jenkins provides a very fantastic tool using which we can generate the syntax for our Jenkins pipeline. The tool can be found in the URL

http://<jenkins_url>/directive-generator/

The tool comes with the Jenkins deployment, and you don’t have to install it separately to use it. More information about the syntax generator tool can be found here.

Jenkins pipeline github

Most of the time, our code is present in the github and needs a CICD tool like Jenkins to pull the code from the version control tool and perform the cicd task.

Follow my blog about Jenkins pipeline Github to understand how to integrate Jenkins with github. The Pipeline to pull the code from the github looks like below.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git url: 'https://github.com/naiveskill/devops.git', branch: 'main'
            }
        }
    }
}

Jenkinsfile example

So far in the above examples, we have copy-pasted the code into the script session of the Jenkins pipeline and Built the Jenkins job. But we can also define the pipeline in the Jenkins file and upload the file into the source code management tool (github).

In this session, we will understand how to write a Jenkinsfile and run the Jenkins job by specifying the jenkinsfile path. To follow along, I assume that we have created an account in github and created a repository under it.

For this demo, I will be using the naiveskill github repo to demonstrate the process. Now connect to your github repo, create a hello_world.jenkinsfile, and paste the below content.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}
Jenkinsfile example

Now go to Jenkins UI and create a Jenkins pipeline. Goto pipeline session and select definition as Pipeline script from SCM.under SCM, select git and pass the github repository URL and branch name

Jenkinsfile example

Now go to the script path and provide the path where the script is present in the github.

In our case, the script is present under the path

./jenkins/jenkinsfile/hello_world.jenkinsfile

Now save the pipeline and click on the build now button. You will get the below output on the successful run.

Jenkinsfile example output

Conclusion

In this blog, we have learned about the Jenkins pipeline and how to create a hello-world pipeline in Jenkins using declarative and scripted syntax. We have also learned briefly about the Jenkins git hub integration and Jenkins syntax generator tool.

Finally, we understood how to write the Jenkins pipeline in a jenkinsfile and build the Jenkins pipeline from the jenkinsfile. Please do let me know if you are facing any issues while following along.

More to Explore

Deploy Jenkins on Kubernetes using Jenkins operator

Jenkins scripted pipeline

Jenkins workflow

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top