# Version Number Environment Variable

**URL:** <https://discuss.bitrise.io/t/version-number-environment-variable/89>\
**Category:** Question & Answer\
**Created:** [January 3, 2017, 6:10pm UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89 "2017-01-03T18:10:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahilbhansali](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rahilbhansali](https://discuss.bitrise.io/u/rahilbhansali)\
**Post date:** [January 3, 2017, 6:10pm UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/1 "2017-01-03T18:10:51Z")

</div>

For this step - is there a way to get the value which was set in the current info.plist in the git repo? Basically we want to use the version number set manually by us in the project. But want to avoid changing it everytime on bitrise as well. So would be handy if after a git clone, we could get an environment variable that exposes the version number in the project.

 ![](https://us1.discourse-cdn.com/flex016/uploads/bitrise/original/1X/d71ab28371475700a32cb441680804c7e6df882b.png)

Would be handy to add it in the git tag step, etc as well.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 3, 2017, 7:04pm UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/2 "2017-01-03T19:04:43Z")

</div>

There’s a community contributed step (not maintained by us) which can do exactly this: [Xcode Project Info](https://github.com/tadija/bitrise-step-xcode-project-info). It exports both the Version and the Build numbers from the `Info.plist`

> <https://github.com/tadija/bitrise-step-xcode-project-info/blob/master/step.yml#L34>

After this you can use these environment variables (`XPI_VERSION` and `XPI_BUILD`) in any other step.

Alternatively you can use a `Script` step and `PlistBuddy` to expose these infos, something like:

```auto
#!/bin/bash
set -ex

# get the values
INFO_PLIST_PATH=./path/to/Info.plist
version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFO_PLIST_PATH}")
build=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFO_PLIST_PATH}")

# expose the env vars for subsequent steps
envman add --key XPI_VERSION --value "${version}"
envman add --key XPI_BUILD --value "${build}"

```

Source (from the Step’s code): [https://github.com/tadija/bitrise-step-xcode-project-info/blob/master/step.sh](https://github.com/tadija/bitrise-step-xcode-project-info/blob/master/step.sh)

---

<div class="post-metadata">

**Author:** ![rahilbhansali](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rahilbhansali](https://discuss.bitrise.io/u/rahilbhansali)\
**Post date:** [January 4, 2017, 5:15am UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/3 "2017-01-04T05:15:02Z")

</div>

Thanks Viktor. In the Set Xcode Project Build Number Set (image attached) if we add an offset to the Bitrise Build Number, does that change the $BITRISE\_BUILD\_NUMBER for future use? Basically our last step is to add a git tag but we want the tag to include the correct build number. Ofcourse with the offset in the previous step, it may not be accurate.

For now, I have added the step Xcode Project Info again before the git\_tag step so it gets the updated value.

 ![](https://us1.discourse-cdn.com/flex016/uploads/bitrise/original/1X/0f99f4bf54f34dc8752ea7e9d1507c046afe97bb.png) ’

PS: I can post this as a separate question if you think its helpful.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 4, 2017, 10:24am UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/4 "2017-01-04T10:24:18Z")

</div>

> [@rahilbhansali](#):
>
> if we add an offset to the Bitrise Build Number, does that change the $BITRISE\_BUILD\_NUMBER for future use?

Unfortunately no, you can’t change the $BITRISE\_BUILD\_NUMBER for subsequent builds, it’s generated by [bitrise.io](http://bitrise.io) every time when a new build is started.

> [@rahilbhansali](#):
>
> Ofcourse with the offset in the previous step, it may not be accurate.

If you use these steps (the Set Xcode Build Number and Xcode Projec Info steps) it will work, but it won’t be in sync with the build number on [bitrise.io](http://bitrise.io) (as that’s a read only value, generated right when the build starts). During the build the number will be in sync, as the Xcode Project Info step reads the value from Info.plist, and the Set Xcode Build Number step writes the value into Info.plist

In general the best is to not to use the offset, as that way you can have a “perfect sync” between steps and the [bitrise.io](http://bitrise.io) web UI.

---

<div class="post-metadata">

**Author:** ![rahilbhansali](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rahilbhansali](https://discuss.bitrise.io/u/rahilbhansali)\
**Post date:** [January 4, 2017, 10:39am UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/5 "2017-01-04T10:39:10Z")

</div>

> [@viktorbenei](#):
>
> Unfortunately no, you can’t change the $BITRISE\_BUILD\_NUMBER for subsequent builds, it’s generated by [bitrise.io](http://bitrise.io) every time when a new build is started.
> 
> rahilbhansali:

We have removed the offset as per your suggestion. However, if someone wants to get the updated value, a hack would be the following:

Add the step **Xcode Project Info** again before the **git tag** step so it gets the updated value from info.plist again. Then use $XPI\_BUILD for the latest value.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 4, 2017, 3:05pm UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/6 "2017-01-04T15:05:58Z")

</div>

Indeed, if you want to get the build number which includes the offset, you have to have the steps in this order:

1. Set Xcode Build Number
2. Xcode Project Info

This way Xcode Project Info reads the value previously set by Set Xcode Build Number.

Thanks for the note / insight @rahilbhansali !

---

<div class="post-metadata">

**Author:** ![ariel](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@ariel](https://discuss.bitrise.io/u/ariel)\
**Post date:** [April 15, 2020, 4:42pm UTC](https://discuss.bitrise.io/t/version-number-environment-variable/89/7 "2020-04-15T16:42:33Z")

</div>

Adapted from [this](http://discuss.bitrise.io/t/marketing-version-wrongly-returned-by-xcode-project-info-step/10838/4)

```auto
set -x

PROJECT_FILE_PATH="path/to/project.pbxproj"
MARKETING_VERSION=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' $PROJECT_FILE_PATH`
BUILD_VERSION=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' $PROJECT_FILE_PATH`

if [-z "$MARKETING_VERSION"]; then
    echo "could not extract marketing version"
    exit 1
else
    echo "marketing version:"
    echo $MARKETING_VERSION
fi

if [-z "$BUILD_VERSION"]; then
    echo "could not extract build version"
    exit 1
else
    echo "build version:"
    echo $BUILD_VERSION
fi

NEW_VERSION="${MARKETING_VERSION}+${BUILD_VERSION}"
echo "new version:"
echo $NEW_VERSION

```
