Dynamic build agent properties

Completed

By default build agents create one environment variable per java version installed letting you do something like this:

requirements {
exists("env.JDK_11")
}

I would like to do the same thing for Xcode versions since we require multiple Xcode versions in the system. I believe the only way to do this is to check this during the agent startup like with Java. Is there any way to hook up a script that defines these variables on startup?

4 comments
Comment actions Permalink

Hello Nicklas!

Xcode Project runner enables the Xcode-specific properties for agents which are automatically selected during startup (a quick example of build requirements):

As you can see, you could use the tools.xcode.version.major to further limit the agents by Xcode version registered on the agent. You could find more on this runner at https://www.jetbrains.com/help/teamcity/2019.2/xcode-project.html#top

On a side note, you can also add your own custom properties to the agent. For instance, I have added below agent property by editing the buildAgent.properties file:


The details on how this works could be found here: https://www.jetbrains.com/help/teamcity/project-and-agent-level-build-parameters.html#ProjectandAgentLevelBuildParameters-AgentLevelBuildParameters

Please let me know if you have any further questions or if the above options could not suit you.

0
Comment actions Permalink

Yes, those properties exists but will only contain the value of the selected Xcode version which doesn't help if there are multiple versions of Xcode installed.

Adding custom properties in the .properties file would work but it would be very tedious as I would have to remember to update it every time I add a new version of Xcode, I'd much rather have a script that is run during startup that dynamically generates the properties.

0
Comment actions Permalink

Hello Nicklas,

Let`s say that you have a script which can fetch all installed versions on the machine. As I see it, you could extend the script to do 2 more things:
1) open corresponding agent .properties, remove your custom Xcode agent property lines and insert new lines with the current versions;
2) start the agent process.

This way, you could use the script as a wrapper for agent startup; this would ensure that the Xcode agent properties are up to date each time the agent is restarted. Unfortunately, I don`t believe you can make agent to run certain script on startup otherwise.

Above is the simplest approach to having the properties populated on a startup; however, you could also extend this approach to write a custom agent-side plugin which would do the same for you, but without the need to handle outside scripts. If you would like to consider this approach, I suppose these articles could come handy: 
https://plugins.jetbrains.com/docs/teamcity/developing-plugins-using-maven.html
https://plugins.jetbrains.com/docs/teamcity/agent-side-object-model.html

0
Comment actions Permalink

Haha, I came up with the exact same idea at the same time.

Here is the script I wrote if it helps anyone else:

#!/usr/bin/env bash

set -e

CONFIG_FILE="conf/buildAgent.properties"

XCODE_PATHS=$(find "/Applications" -maxdepth 2 -name '*.app' -type d -exec sh -c 'if [ "$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "{}/Contents/Info.plist" 2>/dev/null)" == "com.apple.dt.Xcode" ]; then echo "{}"; fi' \;)

HEADER="########## XCode Versions ##########"
FOOTER="######## End XCode Versions ########"
NEWLINE=$'\n'

CONTENTS=$(sed "/$HEADER/,/$FOOTER/d" "$CONFIG_FILE")
CONTENTS="$CONTENTS${NEWLINE}${NEWLINE}$HEADER${NEWLINE}# THIS BLOCK IS AUTOMATICALLY ADDED${NEWLINE}# DO NOT PLACE ANYTHING BETWEEN THEM"


for XCODE_PATH in $XCODE_PATHS; do
VERSION=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$XCODE_PATH/Contents/Info.plist")
CONTENTS=$"$CONTENTS${NEWLINE}tools.xcode.version.$VERSION=$XCODE_PATH"
done
CONTENTS=$"$CONTENTS$NEWLINE$FOOTER"

mv "$CONFIG_FILE" "$CONFIG_FILE.bak"
echo -n "$CONTENTS" > "$CONFIG_FILE"

"bin/agent.sh" run

Then you can use %tools.xcode.version.11.4.1% for example.

0

Please sign in to leave a comment.