Environment variable is not saved acros builds

#1

Hi!

I am trying to create a script, that will increment and store value (similar to build number).

I added cache pull step, than after that there is a script step with code:

counter=${MY_COUNTER:=0}
counter=$((counter+1))
echo $counter
envman add --key MY_COUNTER --value “$counter”

Accessing MY_COUNTER (echo $MY_COUNTER) corresponds to a set value on a next flow steps.

At the end of the work flow flow I have cache push step.

After workflow started again MY_COUNTER is not restored. Please advice what am I missing?

1 Like

#2

Hi there @Mykol_Podolian!

Could you please send us a build URL of the related build and enable Support Access on the Settings tab of the app (https://devcenter.bitrise.io/troubleshooting/enabling-bitrise-support-user/), so that we may take a closer look? :slight_smile:

0 Likes

#3

Hi @Roland-Bak!
The build URL is https://app.bitrise.io/build/d0bba1d7663bd373#?tab=log
and the workflow ur is https://app.bitrise.io/app/8700dbe3ab34cd9f/workflow_editor#!/workflows?workflow_id=env_var_test
I have enabled support access
Thank you for a help !

0 Likes

#4

Hi, @Roland-Bak is there any update?

0 Likes

#5

Hello there @Mykol_Podolian!

Thanks for your patience.
Env Vars created by the script step are not directly cached, you would need need to use envman , which creates a .envstore file which then can be cached: https://github.com/bitrise-io/envman/blob/master/README.md#how-does-it-work

Hope this helps! :slight_smile:

0 Likes

#7

Hi Roland-Bak

Unfortunatelly, I did not get the point. I already use envman to store variable:

envman add --key MY_COUNTER --value "$counter"

Should I also configure a push step to cache .envstore file? (I could not locate this file)
Please specify what should I do, so variable is cached and can be reused on next builds?

(I would appreciate if you make modifications to the flow, so MY_COUNTER is saved across builds)

0 Likes

#8

Hey there @Mykol_Podolian,

You can only create permanent variables on the website, the values are not getting stored automatically because it’s a modification only happening in the build machine.
If you modify a variable the .envstore file gets created in the your current working directory, so that’s where it can be exported from (https://github.com/bitrise-io/envman#how-does-it-work).

Could you tell us a bit more about what you are trying to do with this counter? Perhaps there is a more convenient solution :slight_smile:

0 Likes

#9

I am trying to setup flow, so it finishes early if specific folders in project are not modified. In order to do that, I would need to check changes (we use git) for those folder between last build revision and current revision, that means I need to store git revisions for folders after last build and update those revisions after each build.

That is why I need some env variables that will be stored across builds. If you know a better way, how to achieve “no modification (for specific folders of a project) - no build” behaviour please share it.

0 Likes

#10

Thanks for the clarification! :slight_smile:

You could try using Selective Builds for that same effect, as this feature was designed with a similar idea in mind: https://devcenter.bitrise.io/builds/selective_builds/

Let me know if this helps! :wink:

0 Likes

#11

It looks like what I need. The only problem selective builds only work for gitHub repository. Unfortunately we do not use a github.
So it seems like I am tight to develop a manual solution.

Could you please provide a code snippet to save envman file across builds?

0 Likes

#12

Short answer -> it is better to use a cache files (along with cache pull and cache push steps) to store such variables and then load them to envman, so they are available at each build step.

Long answer -> It seamed to me that envman should not only carry variables between workflow steps, but also save and restore variables between builds. It seemed like a better way rather then store variables in cache files.

I could not get a clear answer from @Roland-Bak (after giving full access to a workflow & waisting 2 weeks to discus the problem), but I have found a thread Save state between build where similar problem was described and offered solution was to keep variable in a cache file.

I still spent some time trying to save and restore envman variables file, but it turns out to be not a trivial task.

First I tried to init envman (envman init) to a working directory but that implied a restriction to add --path paramet to every envman command.
Then I tried to update ENVMAN_ENVSTORE_PATH variable and set it to a cached file and that worked, I did not have to add --path to every envman command, but I had to set this variable to every script step.
My last attempt was to push cache file to ENVMAN_ENVSTORE_PATH and copy it back to cache after workflow is completed. But it turned out that ENVMAN_ENVSTORE_PATH file was empty on the very last script step (probably envman clears it`s cache file on beginning or on the end of each step).

So I gave up and applied solution with keeping value in a file

0 Likes