TeamCity server url from build script - teamcity

I heed to use TC API from build script (gradle). I can read user name and password, from project properties, but I have to read serverUrl as well. But I did not find property teamcity.serverUrl described in doc (right there)
May be this property is missed only on our build TC server?

This very parameter is a configuration parameter
Such parameters can be used in web UI, but are not implicitly passed to Gradle build
In your case, use Additional Gradle Command Line Parameters field in build step configuration and add following flag:
-PserverUrl=%teamcity.serverUrl%
This will pass the value explicitly. You can access server url in gradle like this:
println("Server url is $project.serverUrl")
UPD
If customising paramteres is not an option, you can use another way. There is a system property teamcity.configuration.properties.file that contains path to a file, that contains all the configuration parameteres in usual properties format. So, inside Gradle do something like:
def configFilePath = project["teamcity.configuration.properties.file"]
def props = new Properties();
props.load(new File(configFilePath).newDataInputStream())
def serverUrl = props["teamcity.serverUrl"]

Related

How to use different properties file or environment profiles to execute a maven project?

I am building an automation framework to test a web application in 2 separate environments namely stage and test.
I am storing environment variables like "user_names" and "application_URL" which are unique to the environments in a property file which is read by the test scripts.
Since I have two different environments, i want to execute the same tests in both of them by having two separate profiles of environment variables files.
I have first environment properties file as below:-
browser = Chrome
admin_Url = https://stage-some-website/login
Username = adminuser1
Password = adminuserpw1
DatabaseURl=""
DatabasePasswords=""
This file is read by below base case which will initialize the browsers and read this property file.
public static WebDriver driver;
public static Properties read_propertyFile;
String PropertyFilePath = "\\src\\test\\java\\com\\resources\\Environment.properties";
If i make another property file for second environment, how can i use it ?
Can i parameterize which property file is being read by the base case and pass the filename or environment name in the command that executes the maven project ?

Build Gradle getProperties before running already made task

I’m trying to use a Java, Serenity-BDD project with gradle version 4.8+, but the application is not pulling the CLI arguments of -Denvironment and -Dservicebranches. I have these properties as blank values in my local.properties file, and they’re not getting assigned when my app runs.
./gradlew --build-cache build -Dwebdriver.remote.url=${SELENIUM_REMOTE_URL} -Denvironment=${ENVIRONMENT} -Dservicebranches=${SERVICE_BRANCHES} -Dtags=${TAGS}
I have a local.properties file with properties that are being successfully dependency injected into the project (through Serenity-Spring). I'm hoping that these CLI arguments will override these values:
servicebranches=
environment=local
But right now, anything specified in the CLI arguments are not being passed into the project. Either through DI, or through explicitly grabbing the environment variables in the build.gradle, which what I've tried hasn't been working.
Here's a few things which I have tried in the build.gradle:
//task integrationTests() {
// doFirst
// {
// def environment = System.getProperty('environment')
// def servicebranches = System.getProperty('servicebranches')
// }
// tasks.build.execute()
//}
//integrationTests.dependsOn(build)
//build.doFirst{
// systemProperties System.properties
// def environment = System.properties['environment']
// environment = environment //This actually flags with 'Silly assignment'
//}
build.doFirst{
def environment = System.getProperty('environment')
def servicebranches = System.getProperty('servicebranches')
}
The latest one seems to still be missing a step, because the program is still working, but the args are still not getting through. I've even tried -Denvironment=potato, and no errors have come up because I do not have a property or properties file named that.
I've also tried using the -P tag instead of -D tag, but that doesn't seem to be working either.
All I’m trying to do is use build.gradle to use System.getProperty(‘environment’) and System.getProperty(‘servicebranches’) before I use the already created ‘build’ task that comes with Serenity. How would I do this? Do I build a whole new task, where I use these getProperties, and then call the build task? Do I have to specify the assignment of these same named variables in the local.properties file?
-D is for system properties in Gradle. Try with -P instead (https://docs.gradle.org/current/userguide/build_environment.html#sec:project_properties)
I know this is a very old question but here's what I did to solve my problem, I got the idea from here: https://github.com/serenity-bdd/serenity-documentation/pull/120/files
Serenity was not pulling the environment from gradle to use EnvironmentSpecificProperties, it kept saying "undefined property for environment 'null" when I removed the default environment. I had to add this to my Gradle file:
test {
systemProperty 'environment', System.properties['environment']
}

How to set multiple properties for Maven on Teamcity

The configuration passes a property to Maven using the "Additional Maven command line parameters" setting for the Maven runner.
This is done with -Darguments='' so the maven-release-plugin can use the arguments on each run as it forks new processes.
For one property the configuration is:
-DsomeProp=%teamcity.agent.name% -Darguments='-DimportantProp=true'
The problem is when passing multiple properties like so:
-DsomeProp=%teamcity.agent.name% -Darguments='-DimportantProp=true -DsecondProp=file_on_disk.name'
For the multiple properties configuration the Build log shows that importantProp gets resolved as true -Dsecondprop=file_on_disk.name which is expectedly an invalid value.
The second property secondProp is then not applied as the string gets absorbed into the value of importantProp.
The reason to do this is to simplify test runs on TeamCity and not to change the poms for each test.
I see hardly any examples for this configuration on TeamCity.
your props differ by -DsecondProp=true. So, you should create only one prop for a pass to build. Let's name mainProp
Also, we need to add new prop which contains empty if not checked or -DsecondProp=true if checked. Create checkbox parameter additionalParam with
checked value - -DsecondProp=true
unchecked value - `` (nothing)
Now we need to add this cb parameter our mainProp.
mainProp = -DsomeProp=%teamcity.agent.name% -Darguments='-DimportantProp=true %additionalParam%'
When you will triggered the build you can check the checkbox and pass -DsomeProp=%teamcity.agent.name% -Darguments='-DimportantProp=true -DsecondProp=true
Applying a configuration parameter to the configuration twice has worked. Thanks for the configuration parmeter suggestion Senior Pomidor.
Create configuration parameter in build parameters or build template %mavenArguments%:
-DpropCheck=true -DpropPath=file_on_disk-1.path
Then apply the supplied configuration parameter in the Additional Maven command line parameters on the Maven build step (works directly on the build step or through build template) like this:
%mavenArguments%
-Darguments='%mavenArguments%'
I still have no idea why it's not applied correctly by writing directly into the Additional Maven command line parameters but it finally works.

Setting gradle system property in .gradle file

I have a "general.gradle" file that sets the common properties for all of my projects.
This file is committed to git repository and shared among many users.
I would like to add a system property to is so it will be common to all the users
such options like systemProp.http.nonProxyHosts
is there a way?
You could make another file, like general.properties, add your system properties there prefixed by systemProp and then in general.gradle load the properties from that file, like so:
FileInputStream fileInputStream = new FileInputStream(new File('{YOUR_PATH}/general.properties'))
Properties properties = new Properties()
properties.load(fileInputStream)
fileInputStream.close()
properties.stringPropertyNames().forEach({key -> ext.set(key, properties.getProperty(key))})
and then load it to your root build.gradle file in projects, like so:
apply from: '{YOUR_PATH}/general.gradle'
You can retrieve it from the ext property. Following this example, if you put general.properties in your project and add there,for example: spring=dev. Then you put the property loading code in general.gradle and apply it in your build.gradle, if you add a task like this in your build.gradle:
task testProp << {
String profile = getProperty('spring')
System.setProperty('Spring.profiles.active', profile)
String prop = System.getProperty('Spring.profiles.active');
println prop
}
then the task execution should print out dev.

Passing properties to a gradle build

I admit I am quite new to gradle but I did not expect to be unable to understand something as simple as the example below. I can read the gradle documentation about checking whether a project property have been set or not using a hasProperty(String propertyName) call and I am sitting here and have no idea why something so basic does not work.
I believe my mind must be so much "ant like" oriented that for sure I am missing something ordinary basic
task printSystem() << {
println system
println "has property: " + hasProperty("system")
}
and invoking that task with the command below:
$gradle printSystem -Psystem=mySystem
mySystem
has property: null
So my questions would be:
Why system is printed out but hasProperty returns null?
How should I check for the existence of the project property called "system"?
Is there a different way for testing for a project property as opposed to a system property?
How would you pass a system property from the command line?
This is from, the gradle documentation and I believe I am reading it right
19.2.1. Checking for project properties
You can access a project property in your build script simply by using its name as you would use a variable. If this property does not exist, an exception will be thrown and the build will fail. If your build script relies on optional properties the user might set, perhaps in a gradle.properties file, you need to check for existence before you access them. You can do this by using the method hasProperty('propertyName') which returns true or false.
You need to explicitly invoke hasProperty on the project instance - without it, hasProperty is invoked on some local context. The following example works:
task printSystem() << {
println system
println "has property: " + project.hasProperty("system")
}
Because non-existing properties (system is not defined in the script) are taken from the project instance. If you won't pass the system property, an exception will be thrown on println.
project.hasProperty('propName')
Not sure if I understood right, but you can access project properties via the project instance and system properties via the System class.
Using -D switch - gradle -Dprop=value

Resources