Jenkins


# Jenkins: High-level process description

Upstream job, which is configured as the first one, includes all tests to be run. After a build is executed by this job, a test report with results is generated and a downstream job is triggered to execute its build (upstream build URL is also being sent to the downstream job).

Downstream job takes the test report with results and according to the set parameters it writes the report’s results into the database.



# Jenkins: Detailed configuration

  1. Log in to Jenkins.
    01_Login

  2. Hit the arrow down of the Jenkins button at the top left corner to open a menu, hover over Manage Jenkins and in the submenu select Manage Plugins.
    02_Manage_plugins

  3. In the tab Available use the Filter field to find Parameterized Trigger plugin and install it.
    03_Install_plugin

  4. Hit the top left corner Jenkins button to get back to the list of builds and click on the People menu option. After the page loads, copy your User ID and save it aside, you will need it later.
    04_UserID

  5. Click on your User ID link and hit the Configure menu option.
    05_User_Configure

  6. Hit Add new Token button, then Generate buttonCopy the token number and save it aside, you will need it later. Hit the Save button.
    06_Generate_token

  7. It’s time to create a new project – this project will later write test results down from the upstream job (the job must generate the results from a plugin, where the selenium tests are configured = jUnit plugin is for tests to work properly, Bootstraped-multi-test-results-report plugin serves for displaying the test results).
    Hit the New Item menu option, add a name of your project, click on Freestyle project, click on OK and at the new page hit directly Save button.
    07_Create_new_project

    07_Save

  8. Hit the top left corner Jenkins button to get back to the list of builds.
    Click on your created project and hit the Configure menu option.
    08_Go_to_your_project

  9. At tab General select checkbox This project is parameterized. Into a newly created section add the following details:
    • Name – always enter “proj_ID
    • Default Value – project ID from JunoOne – portal menu section Projects
      09_Project_ID_Juno_EN
    • Description – it’s preferable to fill in the details in this field to describe the parameters from above fields
      09_General_tab

  10. Go to tab Build Triggers and select checkbox Trigger builds remotely (e.g. from scripts)
    This is a build trigger from console. 
    Enter the authentication token – create your own token which will be used later in JunoOne once you’ll be creating the “Job to schedule” in Projects > Automat section.
    17_Build_triggers

    17_Juno_Automat_1_EN

    17_Juno_Automat_2_EN

  11. Go to tab Post Steps, hit the Add post_build step button and add a new section Trigger parameterized build on other projects.
    10_Add_post_build_step

  12. The newly created section will appear under tab Build Settings. 
    Projects to build – fill in with the name of your created project
    Trigger when build is – select option Complete (always trigger)

    11_Fill_in_Trigger_section

  13. Hit Add Parameters button, select option Predefined parameters.
    Enter PRE_BUILD=${BUILD_URL} in the Parameters text field and then hit Save button.
    12_Predefined_parameters

  14. The project overview page appears and it now contains a new section Downstream Projects where your downstream project needs to be configured. Click on its link.
    13_Downstream_project

  15. Click on the Configure menu option. 
    14_String_parameters

    In tab General select checkbox This project is parameterized and use the button Add Parameter to add two String Parameter sections (description fields can stay blank in them):
    • the first section:
      • Name – PRE_BUILD (this is the build we have configured previously in the upstream job)
    • the second section:
      • Name – proj_ID
      • Default Value – project ID which must be the same as in the upstream job previously
  16. Go to tab Build, use the button Add Build Step and select option Execute Shell in order to add it. For each client there will be specific variables created. Once provided, insert the below script into the newly created Execute Shell section. 

    You can see an example of the script below. These items will always be unique for each client:
    Jenkins_USER – User ID previously saved aside
    Jenkins_TOKEN
     – generated token number previously saved
    WSO2_TOKEN – access token into API (every user has a different access token which will be provided by Denevy)
    API address - replace the current API address at the last line of the script: "Authorization: Bearer $AUTH_TOKEN" "https://api-test.denevy.eu/test_juno2/v1/test_results"
     
    #!/bin/bash
    #This job is being triggered by upstream job. For a successful run it only needs
    #'$BUILD_URL' from upstream. Any number of projects can connect to this job and
    #data from all projects will get transfered to database (according to API). Test report should be the same as in Juno_automatizovane_testovanie
    echo $proj_ID
    echo $PRE_BUILD
    ################################################################################################
    #automated_jobs sheet
    #these vars are required for report download
    Jenkins_USER=jcervenak
    Jenkins_TOKEN=113597f0229f8434bb1a309d5230e25aeb
    WSO2_TOKEN=UDhBX2tLeERKMFpreGM1SnNBeDFUUDVhV1VvYTplSWtDYVZNZVAzd2lpV2E5a2Z5S2ljaUZIYVlh

    JOB_ID=$(echo $PRE_BUILD | cut -d'/' -f 6) #it pulls out the job number from build url
    JOB_NAME=$(echo $PRE_BUILD | cut -d'/' -f 5) #it pulls out project name from build url

    promenna=$(curl -k --user $Jenkins_USER:$Jenkins_TOKEN --silent ${PRE_BUILD}api/xml) #an overview of upstream job will get downloaded into this variable
    JOB_START=$(echo $promenna | xmlstarlet sel -t -m '//timestamp' -v . -n) #from the upstream job overview we get the tag <timestamp> - the beginning of upstream project
    JOB_DURATION=$(echo $promenna | xmlstarlet sel -t -m '//duration' -v . -n) #from the upstream job overview we get the tag <duration> - duration of the whole job

    let time_en=$JOB_START+$JOB_DURATION #calculation of the upstream job end
    time_end=$(printf "%.3f\n" $(($time_en/1000))) #time conversion from miliseconds to seconds
    JOB_END=$(date +%FT%T -d @$time_end) #upstream job end time

    #from upstream job overview we get the result
    BUILD_STATUS=$(curl -k --user $Jenkins_USER:$Jenkins_TOKEN --silent ${PRE_BUILD}api/json | jq -r '.result')

    #api token
    AUTH_TOKEN=`curl -k -d "grant_type=client_credentials" -H "Authorization: Basic $WSO2_TOKEN" https://api-test.denevy.eu/token | awk -F'"' '{print $4}'`
    #insert to database via API and json
    curl -v -k -H "Content-Type: application/json" -H "Authorization: Bearer $AUTH_TOKEN" "https://api-test.denevy.eu/test_juno2/v1/automated_jobs" -d '{ "job_status": "'$BUILD_STATUS'", "project_ID": "'$proj_ID'", "job_name": "'$JOB_NAME'", "job_id": '$JOB_ID', "job_time_end": "'$JOB_END'", "job_build_responsible": "'$EXECUTOR_NUMBER'", "job_url": "'$PRE_BUILD'", "job_last_duration": '$JOB_DURATION' }'
    ################################################################################################
    #tabulka test_results

    #
    promenna1=$(curl -k --user $Jenkins_USER:$Jenkins_TOKEN --silent ${PRE_BUILD}testReport/api/xml) #upstream job test report will get donwloaded into this variable

    #pulling the data out of the report
    skipped=$(echo $promenna1 | xmlstarlet sel -t -m './/case/skipped' -v . -n) #from report pulling <skipped> - has the test been skipped?
    status=$(echo $promenna1 | xmlstarlet sel -t -m './/case/status' -v . -n) #from report pulling <status> - what status has the test ended with?
    duration=$(echo $promenna1 | xmlstarlet sel -t -m './/case/duration' -v . -n) #from report pulling <duration> - how long did the test take?
    name=$(echo $promenna1 | xmlstarlet sel -t -m './/case/className' -v . -n) #from report pulling <className> - test name
    test_count=$(echo $promenna1 | xmlstarlet sel -t -m '//totalCount' -v . -n) #from report pulling <totalCount> - how many test have been there in the upstream job?

    #data into array
    arr_status=($status)
    arr_name=($name)
    arr_skipped=($skipped)
    arr_duration=($duration)

    #api token
    AUTH_TOKEN=`curl -k -d "grant_type=client_credentials" -H "Authorization: Basic $WSO2_TOKEN" https://api-test.denevy.eu/token | awk -F'"' '{print $4}'`

    #insert to database via api and json
    for (( i=0; i<$test_count; i++ )); do array_name_rep=${arr_name[$i]}; array_status_rep=${arr_status[$i]}; array_duration_rep=${arr_duration[$i]}; array_skipped_rep=${arr_skipped[$i]}; curl -v -k -H "Content-Type: application/json" -H "Authorization: Bearer $AUTH_TOKEN" "https://api-test.denevy.eu/test_juno2/v1/test_results" -d '{ "status": "'$array_status_rep'", "project_ID": "'$proj_ID'", "build_ID": '$JOB_ID', "test_name": "'$array_name_rep'", "duration": "'$array_duration_rep'", "skipped": "'$array_skipped_rep'", "job_name": "'$JOB_NAME'" }'; done
  17. After pasting the code, the field should look like this.
    16_Entered_source_code

  18. Hit Save button in Jenkins.


COMPANY

DENEVY s.r.o
Business Centrum Ocelářská
Ocelářská 35/1354
190 00, Prague 9

phone: +420 774 494 633
ID: 02995107 VAT: CZ02995107

SUPPORT & SALES

SUPPORT
e-mail: support@denevy.eu
phone: +420 774 494 633

SALES
e-mail: sales@denevy.eu
phone: +420 774 494 633

2020 © Copyright

page by DENEVY