Create a Sample Job in Jenkins

    After having basic knowledge about Jenkins, let’s make a very simple example to see how it works in practice.


  • Python 3.7.x version

    Scenario -> Create a Jenkins job that you can create a folder or a text file with a given spesific name.

1.)    Go to main page of Jenkins and click New Item from left sidebar.

2.)    Give job name as CreateFolderFile and click Freestyle Project then click OK button. 

You will see a screen like below;

3.)    Click This Project is Parameterized  then click Add Parameter and choose Choice Parameter. Again click Add Parameter and this time choose String Parameter.

4.)    Fill the sections like below. This section will provide you a parameterized interface for your project.

5.) Then go to below of page and find  Add Build Step section and click it.Then click Execute Windows Batch Command

Note : If you are working with Linux , you can choose Execute Shell.


6.)    A code section section will open. Write here the code below. Pay attention that   create_type  and  name_of_type are passed as parameters to python file.

For Windows;

cd /d "C:\Users\THEUSER\Desktop\ahmetgungorcom\Jenkins\CreateSampleJenkinsJob\CreateFolderFile"

python "%create_type%" "%name_of_type%"

For Linux;

cd /path/of/python/file/

python "%create_type%" "%name_of_type%"

    In my case, I will be keep going with Windows;

    In this code block, we get parameters from Jenkins and send to python file. We will develop a simple python program and use these parameters to bring out our case.

    After writing the code above, click Save button to save our job.

    7.)    A page will come up like below. Click Build with Parameters option.

    You will see our Folder and File named parameters here. I choose Folder choice and named it as FunnyFolder

8.)    Now we should develop python file to send this parameters. We leave this page like above and we go to folder which we mentioned into above code. Create a txt file here and name it as like below;

9.)    Open file with a text editor. I use Notepad++. On linux you can use vi or vim to open it. Paste the code below into this python file and save it.

# Ahmet Gungor
# 08.07.2019
# Sample Code that create Folder or File with parameters

import os
import sys
if __name__ == "__main__":
    if not len(sys.argv) == 3:         #If parameter count is not 3, then give error.
        print("Wrong parameter count") #1.)   2.)type   3.)name
    type = sys.argv[1]
    name = sys.argv[2]
    current_path = os.getcwd() #get current path where python is.
    if type == "Folder":
        if os.path.exists(os.path.join(current_path, name)): #Is this folder exist next to python file?
            print("Folder is already exist")
            os.makedirs(os.path.join(current_path, name)) #If not exist, create the folder.
            print(f"Folder is created with name = {name}")
    elif type == "File":
        with open(f"{name}.txt", 'w') as the_file: #create a txt file with parameter name
            the_file.write("This python is triggered by Jenkins")
            print(f"File is created with name = {name}")
    else: # If a parameter comes except Folder or File, then give error.
        print("type parameter should be Folder or File")

10.)    Everything is done. Now we should go to Jenkins Job page to run it. Click Build button with Folder and FunnyFolder parameters.

    Tadaaam! Folder is created! Let’s check details on Jenkins Page about this run. When you run this job, it will be created a Job Number #1. This is your build number. Bring your mouse over it and you will see a small down arrow next to it. Click this arrow and then click Console Output to see the logs;

    As you can see on build logs above, the python is triggered successfuly.

    Now give the same parameters as Folder and FunnyFolder and try to run it again;

    Another build Job Number (#2) will be created. Check its Console Output again;

    As you see, this time it gave output like how we developed in python file as Folder is already exist and it didn’t try to create the folder again.

    Let’s try with new parameters such as File and FunnyFile parameters  to have FunnyFile.txt file. Click Build button and after it check Console Output.

    It created a FunnyFile.txt like how we developed on python file!

    You can try new cases and check Console Output to improve your understanding about this usage.

    In this article, I tried to explain you basically how Jenkins works. Like I mentioned my previously article, Jenkins is used as a trigger rather than a development in itself.

Peace ~

Ahmet Güngör.