How to detect if a Gradle build is running on TeamCity? - gradle

Some log messages in my build scripts are only relevant when the Gradle build is running on TeamCity. How do detect programmatically if the Gradle build is running on TeamCity or not?

This can be done using environment variables corresponding to TeamCity's server build parameters, quote:
Environment variable name
Description
TEAMCITY_VERSION
The version of the TeamCity server. This property can be used to determine if the build is run within TeamCity.
To do it in a Gradle script, method java.lang.System#getenv(java.lang.String) can be used:
boolean isOnTeamCity = System.getenv("TEAMCITY_VERSION") != null

Related

Pass a file as a property while calling a maven goal in Jenkins v2.263.1

I am migrating all my jobs from Jenkins v1.651.3 to Jenkins v2.263.1.
Currently I am passing a file stored in my Linux server as a property under Invoke top-level maven targets build step in Jenkins.
e.g. property.a=/home/user1/props/a.properties and property.b=/home/user1/props/b.properties
In Jenkins v2.263.1, I am running all my jobs in a docker container which is created dynamically.
Where and how can I store this file so that I can pass this as a property to maven build like -Dproperty.a=/home/user1/props/a.properties and -Dproperty.b=/home/user1/props/b.properties
I tried adding these files in Managed Files section under Manage Jenkins option and passing these as -Dproperty.a=a.properties to maven build through Jenkinsfile, however, it did not work. Not sure if it's the right way.
Please let me now if there's a way to handle this.
Thanks in advance!

How to set name of the artifacts produced from Gradle to values passed from Jenkins pipeline

I am trying to setup a Jenkins pipeline to trigger builds using gradle for multiple environments.My requirement is that the artifacts produced when I run gradlew clean build should produce artifacts with name indicating the environment for which the pipeline was run. Example my-application-dev.jar
The value of the environment would be selected by the user when build will be triggered.
What is the optimal way to achieve this ? Does build allow to configure any such property via command line or do I need to define a task in my build.gradle and define properties within that task for which I will pass value from command line
There are basically two ways.
The first one is to pass these naming-relevant pieces of information to the gradlew process, e.g. via -D or -P properties.
Then you need the Gradle build to be aware of these parameters and craft the artifact names it produces accordingly.
The second one is arguably better and more contained. You simply rename the artifacts produced by the gradlew command after it completes, in the Jenkinsfile. This works well if the pipeline decides what to do with these artifacts (e.g. publish to a repository) as opposed to the gradle script doing it (in which case you would most likely be better off using the first method).

Configure Multiple SonarQube Instances in a Gradle Build

In our CI environment, we currently have one build server (based on Atlassian Bamboo) and two SonarQube instances (versions 6.0 and 6.5). Initially, our CI server was configured to communicate with the 6.0 SonarQube instance. This has been configured in the /home/bamboo/.gradle/gradle.properties file on our CI server like this:
systemProp.sonar.host.url=<http url of SonarQube 6.0 instance>
systemProp.sonar.login=<username here>
systemProp.sonar.password=<password here>
Now we have another Gradle-based project running on our CI server which shall talk to the new SonarQube 6.5 instance. I tried configuring this but failed all the time.
Things I have done so far:
Added commandline arguments to gradle wrapper command:
I have tried adding -Dsonar.host.url=, -Dsonar.login=, -Dsonar.password= to the Gradle command. As this didn't seem to work, I have also tried to set commandline arguments as SonarQube system properties using -DsystemProp.sonar.host.url=, -DsystemProp.sonar.login=, -DsystemProp.sonar.password=. This didn't work either.
Added properties to the build.gradle file
- Added properties to the build.gradle file like this:
sonarqube {
properties {
property "sonar.host.url", "<http url of SonarQube 6.0 instance>"
property "sonar.login", "<username here>"
property "sonar.password", "<password here>"
...<other SonarQube analysis settings here>...
}
}
In all cases, the CI server talked to the wrong SonarQube instance (6.0). My question is, whether it is possible to configure a single project to talk to another SonarQube instance. As you have seen, we use Gradle 3.2.1 as a build tool. And we are using the org.sonarqube Gradle plugin too.
Thank you for any help.
André
Your first try did not work, because you set the system properties from the commandline, but setting it from the project properties later on resets the system properties to the configured values.
Your second try did not work, because the systemProp.sonar.login syntax is only suppored in gradle.properties files, not via -P commandline project properties.
Your third try did not work because the SonarQube scanner prefers the system property values over the value configured via the DSL, so that one can change what is configured in the build script with the help of local configuration.
You need to set the system properties in your build script manually, this then overwrite what was automatically set from the project property. Using the project gradle.properties file does not work as the user file overwrite the project file. So you need something like System.properties.'sonar.login' = '...' in your build script. You can either hard-code it there, or then use project properties that you can set in your gradle.properties file or via -P parameters.
Besides that, I'd never depend on having any configuration in Gradle User dir on a build server. Most buildservers use build agents that might run on distributed machines, so you would always have to make sure that all build agents are configured the same and so on. I'd always configure in the build setup of the build server the according configuration, either by setting system properties, or environment properties or commandline arguments.
Just my 2ct.

Gradle Tooling : How to set environment variable

I have a custom plugin and I am writing tests to test it. For this I'm using gradle tooling api (I found that to be the recommended way to test).
One of the test requires me to run a task by setting some environment variable. How do I test this. I do not see ProjectConnection providing a way to set environment variable.
If I have to manually test I would have to do this :
setenv LRG_REPOS foo
gradle verify_lrg -PlrgName=abc
where verify_lrg is task added by my custom plugin.
Currently to solve this, I am running using ProcessBuilder, but would like to know if there is any gradle tooling way (because all other tests are using gradle tooling api)
It will be possible to configure environment variables via gradle tooling api since 3.5 version, see details at https://github.com/gradle/gradle/pull/1029
https://github.com/gradle/gradle/blob/446468213543e32a0ce1bce0bbedabcfe925c572/subprojects/tooling-api/src/main/java/org/gradle/tooling/LongRunningOperation.java#L190

Jenkins Build Pipeline plugin with empty ${PIPELINE_VERSION}

I'm using Build Pipeline plugin in Jenkins.
I've set a job and configured it to create the delivery pipeline version. The result will be something like: 3.0.0_r119723_b5
I can see the jobs are displayed with the correct build titles (the pipeline version).
After that, I need to pass this variable to Maven, but the ${PIPELINE_VERSION} always arrives empty.
clean install -Dapplication.version=${PIPELINE_VERSION}
I've already tried to remove the "{" and "}" but it didn't work.
Any thoughts?
Apparentely this is a bug in Maven Project plugin. I've updated both Maven Project and Delivery Pipeline plugins and now the ${PIPELINE_VERSION} works.

Resources