Jenkins Configuration and Run Jenkins Jobs using Batch file command

tudip-logo

Tudip

05 June 2019

Jenkins is an automation tool written which is open source. This tool is written in Java with plugins built for Continuous Integration purpose. Plugins allow integration of various DevOps stages. Jenkins is the most widely accepted tool for Continuous integration because of its flexibility and the amount of plugins that used. Jenkins supports plugins, which allow Jenkins to be extended to meet the specific needs of individual projects. Plugins can be categorized like JUnit, HTML Publishers, Jenkins Build Notification Plugin, Deploy Plugin, Maven Plugin and many more. Jenkins can be installed using a native system package, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed.

Requirements

  • Java Version 8 or 9
  • Jenkins 2.168
  • Jenkins Plugins.
  • Selenium/WebDriver
  • Web Test Script

Jenkins Installations/Setup

  • Download the Jenkins 2.168 war file from https://updates.jenkins-ci.org/download/war/
  • Open the command prompt and go to the folder where Jenkins is downloaded.
  • Run Jenkins.war using the following command.
    java -jar #File_Path/jenkins.war

    Ex. java -jar F:\jenkins.war
    run-command  server-running-1024x473

  • Hit localhost:8080 in the browser. It redirects you to Jenkins login page.
  • Select ‘Install Suggested Jenkins Plugins’, this will automatically add all the suggested plugins.
  • Create an account in Jenkins and you will be redirected to Jenkins Dashboard page.
    welcome-jenkins-1024x452

Jenkins Configuration

  • Click on Manage Jenkins from the Dashboard menu.
  • Go to Configure System, you have to give only the Java path so that Jenkins can identify where the java is installed.
    manage-jenkins-1024x496
  • Copy the Java Path from the system.
  • Click on JDK Installation button.
    jdk-installation-1024x533
  • Type the following details:
    • Name: JAVA_HOME
    • Java Home: Java_Path i.e. C:\Program Files\Java\jdk1.8.0_181
  • Apply and Save the JDK Configuration in Jenkins.

Create a sample test script

  • You need to create a sample test script with TestNG annotation as shown below.
    jenkinsRunJava-1024x591

Run testng.xml file using Command Prompt

  • Go to the project workspace, create a lib folder inside the project and paste all the selenium executable jar library files.
  • Open the command prompt and go to the project workspace folder.
  • Now, to set the classpath, we need to use the following command given below.
    set classpath=#project_path\bin;#project_path\lib\*;

    Ex. set classpath=C:\Workspace\JenkinsDemo\bin;C:\Workspace\JenkinsDemo\lib\*;

  • Then, run the testng.xml file through the following command:
    java org.testng.TestNG testng.xml
  • Now, the console output or result will display in the command prompt itself as shown below:
    jenkinsdemo

Create a batch file and run the Jenkins job

  • Create a new text document and add the following command in it:
    java -cp bin;lib\* org.testng.TestNG testng.xml
  • Save it with “run.bat” file name then, it will create a batch file in the project workspace folder.
  • Now, go to the Jenkins dashboard page and create a new freestyle project.
    jenkins-new-item-1024x444
  • After clicking the Ok button, you will redirect to the project configuration page.
  • Click the Advanced button in the Advanced Project Options.
  • Select the Use Custom Workspace option and add the workspace directory in the Directory field.
    create-jenkins-project-1024x599
  • Scroll down to Build and select the Execute windows batch command in the Add Build Step drop-down.
    build-triggers-1024x686
  • Add the batch command as run.bat in the command field.
    build-environment-1024x595
  • Click on Apply and save the configuration then, you will redirect to the Project Workspace.
  • Click on the Build Now, it will execute the given batch command that executes the run.bat file.
  • You can see the console results on the Console Output.
    console-output-1024x422
  • Whenever you will change the script then, it will build automatically and run the script through batch command.
  • We can see the new changes in the build, status and also, we can run the previous build from Jenkins.

References

Request a quote