Empty BITBUCKET_PIPELINES_BUILD_STARTED
Asked by thedutchy:

Hi!  

As we are trying to improve our build times, we need to get the start time and/or duration of a build from the environment. Apparently this should be stored in BITBUCKET_PIPELINES_BUILD_STARTED, but I cannot find any documentation on it. Also, if I print the BITBUCKET_PIPELINES_BUILD_STARTED in the "after" of a step, it is empty.  

Am I doing something wrong? Anyone know how to get the build start time from the environment, or have an alternative solution?  

Many thanks in advance!


Answers:
author: Patrik S
body: Hello [@thedutchy](/t5/user/viewprofilepage/user-id/2509749) ,

thank you for reaching out to Community!

I'm afraid the variable BITBUCKET_PIPELINES_BUILD_STARTED is not one of the [default variables](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Default-variables) available in Pipelines.

As you mentioned you want to track the amount of time used in a step, since pipelines actually run your script in a bash session, you can make use the the default bash variable $SECONDS, which tracks the number of seconds that have passed since the shell was started.

Since you mentioned you want to fetch the time taken for the step to complete as part of the after-script section of the step, it's important to note that after script starts a different shell script so the variable $SECONDS will be reset. To workaround that, you can export that variable to a file, and then import it again in the after-script using the source command. Following is an example YML file using that approach :

```
pipelines:
  default:
    - step:
        script: 
          - sleep 10 # wait 10 seconds
          - echo $SECONDS # this would print 10 seconds
          - sleep 5 # wait 5 seconds
          - echo $SECONDS # this would print 15 seconds
          - echo TIME_TAKEN_IN_SEC="$SECONDS" > build.env #save the current value of $SECONDS in a file 
        after-script: # the $SECONDS variables will be reset in the after script as its a different shell session
          - source build.env #get the env variable from the file
          - echo $TIME_TAKEN_IN_SEC # this would print 15 seconds
          - echo "$(($TIME_TAKEN_IN_SEC / 60)) minutes and $(($TIME_TAKEN_IN_SEC % 60)) seconds elapsed."
```

The $SECONDS variable also supports assigning a value to it while retaining its properties. This means that the value returned after the assignment is the number of seconds since the assignment plus the assigned value.

It's also important to highlight that the measured time will only consider the time to run the commands in your step's script, but will not include the time included in the **Build Setup**, where Bitbucket Pipelines will create the container, fetch the docker image and clone your repository inside the container, so you may see a different between the time logged in the UI and the time resulted from the $SECONDS variable.

Hope that helps!

Thank you, [@thedutchy](/t5/user/viewprofilepage/user-id/2509749) !

Patrik S

comments: author: thedutchy
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)!  

First of all, thank you very much for this elaborate answer / solution. We have multiple steps in our build and we would need to share the build.env file between the steps.  

Do you know whether the build.env from your example would be readable in a next build step as well? Or is this information lost?  

- Vincent


author: thedutchy
body: For those wanting to do something similar, I found the solution to share files between steps:  

<https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/>  

I then struggled a bit because my variable would still not show up in the script that I run in after-script. I had to change the \`source build.env\` to \`set -o allexport \&\& source build_stats.env\` for it work.  

[@Patrik S](/t5/user/viewprofilepage/user-id/4603309) Many thanks for your support!  


author: Patrik S
body: Artifacts are indeed the way to go to share files between steps in your pipeline!

Glad to hear you were able to find the solution and thank you very much for sharing it with the community here :)

If you ever need help, feel free to reach out to Community!

Thank you, [@thedutchy](/t5/user/viewprofilepage/user-id/2509749) !

Patrik S


Tags: bitbucket-cloud

cloud