Read text file in Kotlin DSL from repo root
Hi,
I want to read a version text file into my Kotlin DSL configuration and use it for some other step.
Here is my project structure:
repo_root
| - version.txt
| - .teamcity
| - settings.kts
The following Kotlin code
import java.io.File
...
steps {
script {
val version = File("../version.txt").readlines().joinToString(".")
...
}
}
gives me the an access error:
java.security.AccessControlException: access denied ("java.io.FilePermission" "../version.txt" "read")
while file permission is
-rw-rw-r--
So how can I read a text file that is located outside the .teamcity directory?
Thanks
Please sign in to leave a comment.
Unfortunately, anything outside of the `.teamcity` directory is inaccessible by the compiler for security reasons. Is it feasible to move the file into `.teamcity` directory?
Ok, got it.
if moving the version file to .teamcity directory is not possible, I can show some workaround via bash script in a script-step:
Take care of correct escaping of reserved bash characters $ and % since both are key characters in Kotlin and Teamcity, respectively:
- Escaping single $ becomes ${'$'}
- Escaping single % becomes %%%%
Say "123.456" is assigned to `version` in the bash script (retrieved from version file) and TeamCity's `build.number` is "1000", this last line of the bash script assigns "123.456_1000" to TeamCity's `build.number` environment variable that may then be used in another step as %build.number% for archiving, e.g.
Yes, this would work just fine - thanks for sharing the example!
@...,
I have a similar scenario where I am trying to populate options for select parameters based on deployment configurations in my source code. The goal was to avoid having to maintain and synchronize the list in two places or force a user to type the value in manually.
Thanks,
Michael
Hello Michael,
Thank you for sharing your use case! I have just realized there is one more option present; if you have a teamcity.default.properties file in your VCS root, TeamCity will read the parameters from there and pass them into any build that is using this VCS root. This way, you could keep the version in this file and reference it in your build version parameter. Please refer to this article for more details on the usage of this logic.