Gift of Growth Sale-mobile

HomeBlogDevOpsJenkins Jobs: Configuring SCM, Build Jobs & Polling SCM

Jenkins Jobs: Configuring SCM, Build Jobs & Polling SCM

Published
25th Apr, 2024
Views
view count loader
Read it in
16 Mins
In this article
    Jenkins Jobs: Configuring SCM, Build Jobs & Polling SCM

    Jenkins is one of DevOps's most widely adopted CI solutions. Jenkins CI is a continuous open-source integration and delivery server. The evolution of cloud computing and DevOps has increased its popularity and made it a popular choice among software developers and DevOps engineers. 

    Jenkins CI allows developers to automate building, testing, and deploying code effortlessly. Also, it hones the capabilities to handle any build or continuous integration. Jenkins Jobs focuses on building and testing code continuously so that any changes made in the source code are easily integrated into the build. Best of all, Jenkins makes it easy to catch up on errors quickly, which is otherwise strenuous and time-consuming using manual processes. 

    Jenkins Jobs

    In this Jenkins tutorial, we will see Jenkins in action. Starting with Jenkins Jobs and its types, our discussion will continue up to triggering builds and scheduling jobs in Jenkins. With the help of an example, we will show you how to create a Jenkins Job. Additionally, we will configure, trigger, and schedule the Jenkins job. Finally, triggering another job in Jenkins post-build will also be discussed at the end of this guide.  

    Let's Start! 

    What are Jenkins Jobs?

    Jenkins Jobs are a given set of tasks that runs sequentially as defined by the user. Any automation implemented in Jenkins is a Jenkins Job. These jobs are a significant part of Jenkins's build process. We can create and build Jenkins jobs to test our application or project. 

    When working with Jenkins, the term “Jenkins Job” and “Jenkins Project” are synonymous. With a Jenkins Job, you can clone source code from version control like Git, compile the code, and run unit tests based on your requirements. Also, Jenkins allows you to merge code using other code management tools like Superversion, CVS, CVN, perforce, etc.  

    Enroll in the Jenkins Certification Course from KnowledgeHut to kick-start your journey in Jenkins from scratch. We have industry-level experts to help you learn most effectively.  

    Types Of Jenkins Jobs

    There are different Jenkins Job types available intended for different purposes. Based on the complexity and nature of your project, you can choose the one that best suits your needs. Let us look at the different types of jobs in Jenkins briefly: 

    Job TypeDescription
    Freestyle ProjectThis is the central and the most widely used feature in Jenkins. It is an available Jenkins build job offering multiple operations. Using this option, you can build and run pipelines or scripts seamlessly.
    Maven ProjectIf your work involves managing and building projects containing POM files, you prefer using Maven Project to build jobs in Jenkins. On choosing this option, Jenkins, by default, will pick the POM files, make configurations, and run builds.
    PipelineFreestyle Project is often not a good option to create Jenkins Jobs. Therefore, Pipeline is the best option. Use the option Pipeline for creating Jenkins Jobs, especially when working on long-running activities.
    Multi-configuration ProjectIf you are working on a project requiring multiple configurations, you prefer to use the Multi-configuration Project option. This option allows for making multiple configurations for testing in multiple environments.
    GitHub OrganizationIf you click on this option, it scans the User's GitHub account for all repositories. And then, it matches markers as defined.

    Are you new to DevOps and looking to jump-start your career in this industry? Register for the most demanding DevOps Training and Certification from KnowledgeHut. We have openings for all from the beginner’s course to expert-level training. Enroll today! 

    Setup Build Jobs

    It is essential to create a Jenkins build job before we initiate the build. In this section, we will create our first Jenkins Job and complete the initial setup. 

    1. Create a New Item 

    Initially, we will create a Jenkin Job using the steps discussed below: 

    • Step 1: Go to the Jenkins Home Page and verify your credentials. 
    • Step 2: Sign in to your Jenkins account. 
    • Step 3: In the Jenkins dashboard, click on New Item as shown: 

    Note: In our tutorial, we have created a New Item named Demo Jenkins Jobs using the Freestyle Project option for simplicity. Other options are available to choose the type of Jenkins Job type you prefer based on your project needs.  

    Upon clicking the OK button, you will have your first Jenkins Job ready. 

    Similarly, we can create as many Jenkins Jobs per our requirements. The procedure for creating jobs remains the same. In the next section, we will configure our newly created Jenkins Job. 

    Configuring Source Code Management

    Once we have successfully created our Jenkins Job, the next task is configuring. In this section, we will configure the source code of our Jenkins Jobs using the FreeStyle Job in Jenkins. Follow the below steps to complete the necessary Jenkins configuration: 

    1. Navigate to the Configure section as shown: 

    Clicking on the configure option will direct you to the Configuration page.

    2. Fill in the “Description” section using the following steps: 

    • Step-1: Click on General. 
    • Step-2: Specify the Jenkins Jobs detail on the “Description” page: 
    • Step-3: Save the changes.  

    Besides Description, there are other options available in the General section. Let's shortly these options in detail:  

    OptionsDescription
    Discard old buildsIf we prefer discarding the old builds while initiating a new build, use this option.
    GitHub ProjectIf you have source code in GitHub and want to use it in your Jenkins Job, use the GitHub Project option. When selecting this opinion, ensure that you specify the GitHub URL.
    This project is parameterizedThis option allows you to create builds with different parameters that would be passed during run time. Every parameter will have a specific name and value.
    Throttle buildsChoose the Throttle Builds option when working on a project with a minimum time required between builds based on the expected maximum rate.
    Disable this projectChoosing this option will disable the project you are currently working on. Simply put, no new build of this current project will get executed.
    Execute concurrent builds if necessaryThis is an additional option you can use to execute multiple builds of this project in parallel.

    Once you add the description, let's move to the next section, "Source Code Management". 

    Source Code Management

    This section allows you to specify the source code that Jenkins Jobs will use to run builds. Jenkins also allows you to use other source code like SVN, Perforce, and many more. However, you need to install additional plugins to use other options in the Jenkins job configuration. By default, you will find two options available in the Configuring Source Code Management section: 

    • None 
    • Git 

    If you choose None, you will not to asked to add any further detail, and we are good to proceed. 

    Alternatively, there is an option to use source code from Git. Jenkins allows source code to be uploaded in the Git version control tool.  When selecting Git as your preference, you will be asked for the repository URL. 

    Follow these steps if you prefer to use source code from your Git repo: 

    • Step 1: Select the Git option. 
    • Step 2: Add the repository URL you want the Jenkins Job to use. 
    • Step 3: Save the changes. 

    After completing the source code management, we will next check the Jenkins Build Triggers option. 

    Build Triggers 

    Before the Jenkins build step, triggering the job is essential. Building triggers in Jenkins allows you to execute a job on every occurrence. Simply put, any time there is a change in the source code, Jenkins automatically triggers a build with the most recent update.

    In the Jenkins Build Triggers option, there are multiple options available. Let's understand each in detail:

    OptionsDescription
    Trigger builds remotelyChoose the triggered builds remotely if you need to trigger new builds using a dedicated URL.
    Build after other projects are builtUsing this option, a new build gets triggered only after other builds are run successfully.
    Build periodicallyIf you choose to run builds periodically, choose this option. Ensure that you specify the time you want the build to start.  
    GitHub Pull requestsUsing this build trigger option allows for integration with GitHub Pull Requests and Issues activities. In addition, it launches runs as output.
    GitHub hook trigger for GITScm pollingUse this option if you need to execute your builds with the help of GitHub webhooks.
    Poll SCMSimilar to Jenkins Build Periodically build trigger option, we need to specify a timer for Poll SCM. However, the SCM in Jenkins option executes build only when there is any code change during that time.

    Jenkins Job Scheduler 

    Jenkins also provides an option to schedule our Jenkins Job as per our needs. When choosing the Jenkins schedule build option, you have to specify when the build starts. For instance, in our tutorial, we use the Build Periodically option that will trigger the build per the minute specified.  

    Polling the SCM

    Similar to the Build Periodically option, there is another commonly used build trigger option called Jenkins SCM or Poll SCM. Using this Jenkins schedule job option, you need to specify the time you want the build to trigger. Remember, this option will execute build only when any changes are made in the source code during that instance. 

    Builds

    In this section, we will walk through Jenkins Builds. As we are using the basic code using the Windows batch command, let's use the "Execute Windows batch command" option: 

    Once you click on the above option, a text area will appear where you will be asked to write the command. Specify the command you want the Jenkins Jobs to take as inputs. Once done, click on Apply and then Save the changes as shown: 

    You will see a confirmation from Jenkins that the changes you made are successful and Saved: 

    In addition, other Build Trigger options are available for use. Let’s understand each in detail:  

    OptionsDescription
    Execute Windows batch commandIf you want to run a Windows batch script to build your project, use the Execute Windows batch command. Here, the script runs with the workspace as the current directory.
    Execute shellUsing this option will run a shell script to build a project. Similar to Execute Windows batch command, the script runs with the workspace as the current directory here as well.
    Invoke AntWhen you use this option, it specifies a list of Ant targets to be invoked and is separated by space.
    Invoke Gradle ScriptConsider using this build if you are working on a project where Gradle is in use. Jenkins triggers Gradle with a specific set of switches and tasks.
    Invoke top-level Maven targetsIf you are working on a project where Maven is used as the build system, consider using Invoke top-level Maven targets. Jenkins triggers Maven with a specific set of goals and options.
    Run with timeoutIf a build fails to complete in a specified time, then that particular gets terminated and marked as aborted. If you are using the Run with timeout option, the default time is at least 3 minutes.

    Maven Build Steps

    Maven is another build tool that we can integrate with Jenkins. We have an option in Jenkins to connect Maven Build with our project. The integration goal is to quicken the build process. Follow these steps to start Jenkins Maven Build 

    Step-1: Navigate to the Jenkins dashboard and go to Manage Jenkins. 

    Step-2: Go to Global Tool Configuration.

    Step-3: Under Maven, click on add maven.  

     

    Step-4: Uncheck install Maven and specify the path.    

    Step 5: Apply and Save the changes. 

    Are you interested in learning Maven and Jenkins parallelly? Enroll yourself with KnowledgeHut’s Jenkins Certification Course and get trained by experts. Besides offering the best Jenkins course material, we also offer Maven training to help you strengthen your DevOps skills. Contact us to learn about our training and offerings. 

    How to Run Jenkins job?

    So far, we have discussed the necessary configuration. Next, we will be good to run Jenkins build jobs. Based on the configured code in the previous step, it's time to execute them and see Jenkins in action. Regardless of your choice, whether you have configured the code by uploading a Git repository URL or merely written codes manually, the job will run anyways. Once the job is executed, you can check the build's status and verify whether it was a success or a failure. 

    Use the following steps to run the job manually: 

    Step 1: Choose the job you want to run and click on the "Build Now"  section as shown below: 

    Step 2: On clicking on the Build Now option, the build will get initiated. You can also refer to the Build history to check the list of builds as shown below: 

    Step 3: Once the build is complete, you can check the build results and verify on the console output screen. 

     

    In our example, the build ran successfully. Therefore, the final status of the job is shown as SUCCESS. If there is any bug in the code, the build will fail. If you see a build has failed, investigate the issue and troubleshoot it to get the error fixed.  

    Congratulations and creating a Jenkins Job successfully. Next, we will learn to trigger another job in Jenkins post-build. 

    How to trigger another job in Jenkins post-build?

    Running a single job in Jenkins is great. However, there are times when we need to run multiple jobs one after another. This is common if you have a big or complex project. When working on such projects where there is a need to run multiple jobs, Jenkins outperforms automation servers. You can trigger Jenkins builds in a very sophisticated and tech-savvy way. 

    Whether the jobs you want to run are dependent or independent of each other, Jenkins build triggers take care of all. Suppose the jobs you want to run depend on each other, and you wish to continue to run the second job upon completion of the first. On the contrary, if the jobs are independent, you want to continue running the second only when the first is completed.  For handling such requests, Jenkins is highly effective and efficient.  

    Let’s understand how to trigger the Jenkins job in post-build. Follow the below steps to implement it: 

    Step 1: Go to the Jenkins dashboard and navigate to the previous job we have built. 

    Step 2: Next, go to the configuration section of Demo_Jenkins_Jobs. After that, go to the Post-build actions as shown:  

    Step 3: On clicking on the Add post-build action dropdown, you will see a list of available options. Select the option "Build other projects" and proceed: 

    Step 4: Name the project "Demo_Jenkins_Jobs"  inside the "Projects to Build"  textbox. Next, you will see the option “Trigger only if the build is stable.” Select the option and click on Save to continue. 

    You can go through other available options to explore your knowledge. For simplicity, let's stick with the default option, i.e., Trigger only if the build is stable.  

    Step 5: Once you have saved the changes, you will see a newly created Downstream project as shown below:

    Step 6: Next, click on the "Build Now"  option to start running the job. 

    Step 7: Upon completion of the first job, the second job will commence. You can differentiate the two jobs by their project location. i.e., upstream and downstream.   

    Step 8: Post execution, you can see the build results in the console output. The text "Started by upstream project"  validates that this job was automatically started after successfully executing the first job. 

     

    Conclusion

    Jenkins has become a key player in the DevOps industry. With the explanation to build Jenkins Jobs from scratch, you can create jobs comfortably. If you are a beginner in DevOps, we suggest you try creating Jenkins jobs following the steps discussed in this tutorial. Once you master the basics, the following strategy must apply to Jenkins in real-world projects. Moreover, you can explore online resources and deep dive into Jenkins. 

    Though we have reached the end of the tutorial, this must not be an end to our Jenkins learning journey. Use the concepts discussed to try building Jenkins jobs yourself and experiment with new things. As a one-step forward, we suggest you start building multiple jobs, or maybe creating a pipeline will be a great move. In addition, you can try integrating with Maven or any other comprehension tool to streamline your builds and level up your learning game. 

    Happy Computing!  

    Frequently Asked Questions (FAQs)

    1What is the purpose of the Jenkins job?

    Jenkins Jobs is aimed to build and test code continuously. The main purpose is to ensure that any changes made in the source code are easily integrated into the build.   

    2How are jobs created in Jenkins?

    To create a new Jenkins job, go to the Jenkins dashboard and click on New Item. After that, choose the Jenkins Job in the list of available options. Then, you are all set to start.  

    3What is a pipeline job in Jenkins?

    A pipeline is a collection of Jenkins jobs connected sequentially. Generally, pipelines support implementing and integrating continuous delivery pipelines into Jenkins.

    4What are the job types in Jenkins?

    Jenkins allows choosing different kinds of jobs based on project needs and requirements. The list of job types available in Jenkins are: 

    • Freestyle Project. 
    • Maven Project. 
    • Pipeline. 
    • Multi-configuration Project. 
    • GitHub Organization. 
    Profile

    Mayank Modi

    Blog Author

    Mayank Modi is a Red Hat Certified Architect with expertise in DevOps and Hybrid Cloud solutions. With a passion for technology and a keen interest in Linux/Unix systems, CISCO, and Network Security, Mayank has established himself as a skilled professional in the industry. As a DevOps and Corporate trainer, he has been instrumental in providing training and guidance to individuals and organizations. With over eight years of experience, Mayank is dedicated to achieving success both personally and professionally, making significant contributions to the field of technology.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming DevOps Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon