1

I want to add 2 different time in shell script but i am not sure how to do it The scenario is to get the build start time , build end time and build execution time of a jenkins build. i can get the build start time and build execution time so to get the build end time i will have to add build start time and execution time. since this is not a normal addition i am not sure how to add these two any help on this

build_start=11:17 AM
build_duration=07m:52sec
build_end= $build_start + $build_duration
noob_coder
  • 749
  • 4
  • 15
  • 35

2 Answers2

1

An alternative approach is to touch marker files for whatever parts you want to calculate duration.

(Update: assuming you're on linux)

rm "${WORKSPACE}/${BUILD_NUMBER}-start" || true
touch "${WORKSPACE}/${BUILD_NUMBER}-start"

...
# duration is current time - start
# see https://stackoverflow.com/questions/19463334/how-to-get-time-since-file-was-last-modified-in-seconds-with-bash
echo $(($(date +%s) - $(date +%s -r "${WORKSPACE}/${BUILD_NUMBER}-start")))
mghicks
  • 1,144
  • 6
  • 16
0

you can use two build-steps. But note that the result time will be still inaccurate. Since every shell step uses its own environment you need to tell your final shell step what environment to use. This can be done by "Inject environment variables" step.

Solution for your question:

1) execute shell step and get your $build_start, then

build_start=$(date +%s)
echo "build_start=$build_start" > time.env

2) your job build steps here

3) add a build step "Inject environment variables" and point to location of previously created "time.env"

3) execute shell step to count build duration and echo it

    #you now can access injected environment variable ${variable}
    end_time=${build_start}
    build_end=$(date +%s)
    runtime=$(($end_time - $start_time))
    echo Build duration: $runtime seconds.
hopetds
  • 435
  • 2
  • 6
  • i donot have EnvInject Plugin installed in my jenkins instance..can i do this without using the plugin? – noob_coder Jan 24 '18 at 11:26
  • If you want to use shell steps - then no. Without installing plugins you can take a look at this topic: https://stackoverflow.com/questions/13799652/jenkins-get-build-time-trend-values-using-remote-access-api – hopetds Jan 24 '18 at 14:33