Setting environment variable from script command for later use in cache name

I’m using Cypress for some testing in Circle CI and I’d like to be able to cache the downloaded binary so speed up the builds a little bit, and ideally that cache would be based on the installed version number so it “just works” across upgrades.

I’m attempting to extract the version number in one job, and then use it in another.

      - run:
          name: Extract Cypress version for cache name
          command: |
            CYPRESS_VERSION=$(yarn info cypress version | grep -v "DeprecationWarning" | sed -n "2p")
            echo $CYPRESS_VERSION
            echo 'export CYPRESS_VERSION=$CYPRESS_VERSION' >> $BASH_ENV
      - restore_cache:
          name: Restoring Cypress cache
            - cy-{{ .Environment.CYPRESS_VERSION }}

I can see in the job output that it’s getting the correct value for the version, but on restoring the cache it says Found a cache from build 1476 at cy-<no value> which, oddly enough, isn’t the correct version number.

Current hypothesis is that the $BASH_ENV environment variables aren’t the same as the .Environment ones, which does make sense. Is it possible to use bash env vars in cache names?

In the docs, there’s this info:

The environment variable variableName (supports any environment variable exported by CircleCI or added to a specific Context—not any arbitrary environment variable).

However, all is not lost; you can use a file instead. In your version capture:

echo $CYPRESS_VERSION > /tmp/cypress-version

Then in your restore keys:

- cy-{{ checksum "/tmp/cypress-version" }}

This worked brilliantly, thanks!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.