$BITRISE_CACHE_DIR seems to change between builds

Hi! I’m trying to use a script to cache a string to a file, and then read that string on a subsequent build. I’ve downloaded my cache and confirmed that I am successfully writing my value to a file by doing the following:

last_deployed_commit_path="$BITRISE_CACHE_DIR/last-deployed-commit"
echo "$GIT_CLONE_COMMIT_HASH" > "$last_deployed_commit_path"

But when I run my workflow again, and try to read my saved file, the path in $BITRISE_CACHE_DIR seems to change, and my script can’t find the file at “$BITRISE_CACHE_DIR/last-deployed-commit”.

For example, the $BITRISE_CACHE_DIR that I saved to initially contains the path
/var/folders/90/5stft2v13fb_m_gv3c8x9nwc0000gn/T/cache252802967

But when I try to read $BITRISE_CACHE_DIR on a subsequent build, the path is now
/var/folders/90/5stft2v13fb_m_gv3c8x9nwc0000gn/T/cache760449913

…and my “last-deployed-commit” file is nowhere to be found.

Am I misunderstanding something? These are scheduled builds (which I’ve been triggering manually to test), and this concerns a single workflow on a single branch. Any help would be much appreciated! Ultimately, I simply want to cache a string when my workflow is run, and have access to that string the next time that same workflow is run. Thanks!

Hello there, I have a script for custom build numbers, but this could be easily be rewritten to this purpose as well I think

make sure to add $BITRISE_SOURCE_DIR/magicnum.txt to the cache push step

#!/usr/bin/env bash
# fail if any commands fails
set -e
# debug log
set -x

if [ ! -f $BITRISE_SOURCE_DIR/magicnum.txt ]
then
  touch $BITRISE_SOURCE_DIR/magicnum.txt
fi

inc=$(<$BITRISE_SOURCE_DIR/magicnum.txt)
if [ -z "$inc" ]
then
  echo 1000 > $BITRISE_SOURCE_DIR/magicnum.txt
  inc=$(<$BITRISE_SOURCE_DIR/magicnum.txt)
fi


if [ $BITRISE_TRIGGERED_WORKFLOW_TITLE == "test" ]
then
  build_number_offset=1
  envman add --key MY_BUILD_NUMBER --value $((build_number_offset + inc))
  echo $(( inc + 1 )) > ./magicnum.txt
fi
1 Like

Thanks for the quick reply! Caching the file in $BITRISE_SOURCE_DIR instead of using $BITRISE_CACHE_DIR seems to have done the trick :+1:

1 Like

Great, I’m glad to hear it

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