Set variables based on branch
Good afternoon/evening!
Due to how we have set up some of our TeamCity projects, we consume build configuration licenses at a rapid pace. We need the ability to set environment variables based on the branch that was built - what are our options?
Specifically this is a Node.JS build that publishes to an S3 bucket (dev/test/staging/prod). When the Node build runs, it looks for environment variables so it runs the correct build config.
I looked at the following:
https://youtrack.jetbrains.com/issue/TW-24275#comment=27-403264
It seems like our only option is to have a dedicated step as mentioned in that YouTrack feature request. Could someone shed some light on this for me please?
Thanks!
-Chris
Please sign in to leave a comment.
Hi Chris,
Using a dedicated build step is definitely the easiest option, and doesn't consume any extra configuration towards your license (a step are the different steps within a build configuration, configurations can have any number of steps), but it will somehow depend on your exact build set up, whether you have it split through several configurations or in a single one.
The built branch is available through the parameter %teamcity.build.branch%, so it should be rather simple to access it from any of your build configurations in a chain, or any step. If you have a "command line" step, you could simply use <your_env_variable>=%teamcity.build.branch% and it would be good to go. If you only have one build configuration for this specific scenario, then you shouldn't even need environment variables (unless your publishing scripts expect them), you could simply reference that parameter for the branch.
If you have multiple build configurations, then it might be a bit more problematic, but is still doable. First, environment variables obviously are set up for the computer that is running the build. Different build configurations can be spawned in different computers and thus do not share automatically environment variables. This said, you can use the "env." variable format in TeamCity for this, and they are "published" to the machine as environment variables. If you then need to access this parameter from future build configurations, you can access it like %dep.<original_build_id>.env.<var_name>%. If it's from within a script that you can't modify or adapt for teamcity, you should probably also create a first build step that exports the variable
In summary, to get it to have an environment variable:
-Modify your initial build configuration to add an initial build step that exports the %teamcity.build.branch% value as the env. variable you are expecting.
-If further configurations down the chain also require it, reference them as %dep.<original_build_id>.env.<var_name>%
-If further configurations require it but can't reference it in teamcity's specific terms, add a first build step to all configurations that need it and export the dependency value.
Hope this helps. If it's not clear or you have any other question, feel free to ask.
I think it's clear enough for us to take a crack at it. Thanks!
The %teamcity.build.branch% variable was JUST was I looking for. I was building a PowerShell step to have TeamCity push a deployment announcement to MS Teams when we were building a package. I wanted to include (among other things) the branch. This variable was just what I needed. Thanks!
I personally think having parameters that flex based off the branch should be baked into the project. (Just like you can see builds based off of branch.) Let me choose which setting inherit and which ones are different per branch. (Usually just version number for me.)