Global variable incremented in every build

Hi,
I need to create global Bitrise variable like $BITRISE_BUILD_NUMBER that will increment in custom step every build. Is it possible?

1 Like

Hi @Nodon,

Thanks for asking this here! :wink:

What do you mean by this? E.g. if there are two builds running, one on the master branch and another one on a feature/A branch, should both bump the value? Should feature/A’s run affect master’s?

In example, I have flow called release-market and in this flow is custom script that set version in format X.X.$BITRISE_BUILD_NUMBER and in need to do to store this build number in variable to use next when this flow will run. For example I need to do something like this:

$BITRISE_LAST_RELEASE_BUILD_NUMBER = $BITRISE_BUILD_NUMBER
1 Like

If you don’t need cross-branch storage then you can write these infos into the Build Cache (docs: http://devcenter.bitrise.io/caching/about-caching/).

Just add the Cache:Pull and Cache:Push steps to your workflow (Pull before you’d use the file/info/cache, and Push after you update it. E.g. Pull right after Git Clone, and Push at the very end of the workflow.), and then write your info into a file.

E.g. to write it into a file inside the cache dir, using a Script step, you can write something like this:

#!/bin/bash
set -ex

file_pth="$BITRISE_CACHE_DIR/last_release_build_number"
echo "$BITRISE_BUILD_NUMBER" > "$file_pth"

To read it from the file and expose it as an env var (BITRISE_LAST_RELEASE_BUILD_NUMBER) using envman (related docs: http://devcenter.bitrise.io/tips-and-tricks/expose-environment-variable/) in a Script step:

#!/bin/bash
set -ex

file_pth="$BITRISE_CACHE_DIR/last_release_build_number"

if [ ! -f "$file_pth" ] ; then
  echo " (!) File does not exist"
  exit 0
  # or if you want to fail in this case:
  # exit 1
fi

# read the value from the file
value_from_file="$(cat $file_pth)"

# expose the value as an env var for other steps
envman add --key BITRISE_LAST_RELEASE_BUILD_NUMBER --value "$value_from_file"

This will work if you don’t need cross-branch sharing. As described in the cache docs (http://devcenter.bitrise.io/caching/about-caching/) a build running on feature/A can’t write into the cache of master, or any other branch’s cache, only into its own.

But if you only read & write this “last release” info on a specific branch this will work.

For a truly global (cross branch) solution, where e.g. feature/A can affect the variable for master, you have to use a third party service, like an FTP server or Amazon S3, we don’t have a feature like that right now.

If you’d like to request that feature, to be built into bitrise.io, please create a #feature-request!

Thank you, i will try to implement this :slight_smile:

1 Like

I created a quick example, demoing what I described above:

format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
trigger_map:
- push_branch: '*'
  workflow: primary
- pull_request_source_branch: '*'
  workflow: primary
workflows:
  primary:
    envs:
    - CACHED_DEPLOY_INFO_FILE_PATH: $BITRISE_CACHE_DIR/last_release_build_number
    steps:
    - activate-ssh-key@3.1.1:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - git-clone@3.4.1: {}
    - cache-pull@0.9.2: {}
    - script@1.1.3:
        title: Read from Cache
        inputs:
        - content: |-
            #!/bin/bash
            set -ex

            file_pth="$BITRISE_CACHE_INFO_PATH"

            if [ ! -f "$file_pth" ] ; then
              echo " (!) File does not exist"
              exit 0
              # or if you want to fail in this case:
              # exit 1
            fi

            # read the value from the file
            value_from_file="$(cat $file_pth)"

            # expose the value as an env var for other steps
            envman add --key BITRISE_LAST_RELEASE_BUILD_NUMBER --value "$value_from_file"
    - deploy-to-bitrise-io@1.2.9: {}
    - script@1.1.3:
        title: Write into Cache
        inputs:
        - content: |-
            #!/bin/bash
            set -ex

            echo "$BITRISE_BUILD_NUMBER" > "$CACHED_DEPLOY_INFO_FILE_PATH"
    - cache-push@0.9.4: {}

Of course, you can add any step between the “Read from Cache” and the “Write into Cache” script steps - in this example I just added a “Deploy to Bitrise.io” step to mark the place of a deploy step.

I also changed one more thing, that I specified the cached info file path as a workflow specific env var (CACHED_DEPLOY_INFO_FILE_PATH) instead of specifying the value in both Script steps manually/separately.

I hope this helps, if you’d have any questions just let us know! :wink:

Hi, Is there any update on how to share global env variable in real-time between branches/workflows?
Thanks

Hey there @hidaya!

Currently there are no updates, but we have an ongoing feature request about this which you can track here: Share global environment variables and files / Organization level environment (and secret) variables :slight_smile: