Gradle ternary/elvis operator not setting 'else' value when external property not set - properties

The 'else' value of the elvis/ternary operator in my gradle build file is not setting the property value if I do not run gradle with the "-P" option.
Here's the root project's build.gradle
defaultTasks 'loadConfiguration'
task loadConfiguration << {
def profile = hasProperty('profile') ? profile : 'dev'
ext.profile = profile
def configFile = file('profile.properties')
def config = new ConfigSlurper(profile).parse(configFile.toURL())
ext.config = config
}
configure (subprojects) {
profile = profile //inject property into sub-project
println profile
task buildear << {
ear
}
}
The sub-project 'ear' is in the settings.gradle file.
Below are the results of a build attempt-
With external property set:
$ gradle -Pprofile=wwww
Parallel execution is an incubating feature.
wwww
wwww
wwww
:loadConfiguration
BUILD SUCCESSFUL
Total time: 5.834 secs
With empty external property set:
$ gradle -Pprofile
Parallel execution is an incubating feature.
:loadConfiguration
BUILD SUCCESSFUL
Total time: 5.389 secs
With no external property set:
$ gradle
Parallel execution is an incubating feature.
FAILURE: Build failed with an exception.
* Where:
Build file '/home/robert/codingk/kazi/trunk2/mazama2/build.gradle' line: 13
* What went wrong:
A problem occurred evaluating root project 'mazama2'.
> Could not find property 'profile' on project ':ear'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.104 secs
Line 13 is the line "profile = profile //inject property into sub-project"
I cannot figure out why the 'profile' property is not getting set to 'dev'. A few things I've tried without success:
profile= "${profile}
def profile = project.hasProperty('profile') ? profile : 'dev'
project.ext.profile = profile

some remarks about your initial build script.
In your initial build script you declared a task (loadConfiguration) to set the profile. This logic is executed AFTER the configure block below
is executed. That's one reason why "profile" is always null in the configuration block
The second problem is, that you need to be careful about scoping. In the snippet
ext.profile = profile
you add a dynamic property to the loadConfiguration task, not to the project itself. That's why you can't reference
The profile property if you havn't passed it via commandline.
Maybe instead of doing the configuration loading in a seperate task, do it on the top level of your build:
ext.profile = hasProperty('profile') ? profile : 'dev'
def configFile = file('profile.properties')
def config = new ConfigSlurper(profile).parse(configFile.toURL())
ext.config = config
configure (subprojects) {
profile = profile //inject property into sub-project
println profile
task buildear << {
ear
}
}

Got it working with the following modifications. I may be wrong but I believe it had to do with task ordering; declaring loadConfiguration() as a default task did not cause it to be executed first which is what I needed
def loadConfiguration() {
def profile = hasProperty('profile') ? profile : 'dev'
ext.profile = profile
def configFile = file('profile.properties')
def config = new ConfigSlurper(profile).parse(configFile.toURL())
ext.config = config
}
configure (subprojects) {
loadConfiguration()
profile = profile //inject property into sub-project
println profile
task buildear << {
ear
}
}

Related

How to display in a project that is called from another pipeline project in jenkins text in "Edit Build Information"

Hello: I have a pipeline project at Jenkins called "ProjectLaunch" that launches other projects automatically one day a week. I would like to show in "Edit Build Information" a text that says something like "ProjectCalled full launched", in the project that is called. With the pipeline I have in "ProjectLaunch" I can only get that text in its "Edit Execution Information" but not in the projects called.
My pipeline:
pipeline {
agent any
stages {
stage('Proyect called') {
steps{
build job: 'Proyect called',string(name: "DESCRIPTION", value: "ProjectCalled full launched") ]
}
}
}
}
In the project called in the configuration I have an Execute system Groovy script, it is this:
import hudson.model.*
def build = manager.build
def params = build.action(hudson.model.ParametersAction).getParameters()
def description = "${params.DESCRIPTION}"
echo "Setting build description to: ${description}"
build.createSummary("Build description").appendText(description, "html")
But when I launch it I get this error:
Caught: groovy.lang.MissingPropertyException: No such property: manager for class: hudson1130775177255181016
groovy.lang.MissingPropertyException: No such property: manager for class: hudson1130775177255181016
Any ideas?
Thank you.
As far as I know, you can't set the build description of a downstream job from an upstream Job. Hence in your Downstream Job, you can have some logic to set the description based on the trigger.
pipeline {
agent any
stages {
stage('Hello') {
steps {
script {
echo 'Your second Job'
if(currentBuild.getBuildCauses()[0]._class == "org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause") {
echo "This is triggered by upstream Job"
currentBuild.description = "ProjectCalled full launched."
}
}
}
}
}
}
I've done the following because with Groovy I couldn't do it. I installed "Build Name and Description Setter" plugin. In the "ProjectLaunch" pipeline project, the pipeline would be more or less like this:
Pipeline {
agent any
parameters{
string(name: "DESCRIPTION", defaultValue: "ProjectCalled full launched", description: "")
}
stages {
stage('ProjectCalled') {
steps{
build job: 'ProjectCalled', parameters: [string(name: "DESCRIPTION", value: params.DESCRIPTION)]
}
}
}
}
And in the ProjectCalled in settings, add a parameter, I select the plugin option Changes build description, and I write ${DESCRIPTION}.
Changes build description
Ah!, I have also created string parameter named DESCRIPTION and without value.
String parameter

Feature with tag still being run when configured not to

I have a main feature file where I have included a "setup" feature file that should add some test data. This setup feature file has an annotation that I have called #ignore. However, following the instructions in this Can't be enable to #ignore annotation for the features SO answer, but I am still seeing the setup feature file being run outside of the main test feature.
Main feature file, unsubscribe_user.feature:
Feature: Unsubscribe User
Background:
* def props = read('properties/user-properties.json')
* url urlBase
* configure headers = props.headers
* def authoriZation = call read('classpath:basic-auth.js') { username: 'admin', password: 'admin' }
* def testDataSetup = call read('classpath:com/meanwhileinhell/app/karate/feature/mockserver/testDataSetup.feature') { data1: #(props.data1), data2: #(props.data2) }
Scenario: Unsubscribe user
...
...
Scenario: Remove test data
* def testDataTearDown = call read('classpath:com/meanwhileinhell/app/karate/feature/mockserver/testDataTearDown.feature') { data1: #(props.data1), data2: #(props.data2) }
...
testDataSetup.feature file
#ignore
Feature: Add data to REST Mock Server
Background:
* url mockServerUrlBase
Scenario: Add data
* print 'Adding test data'
Given path 'mapping'
And request { data1: '#(data1)', data2: '#(data2)' }
When method post
Then status 201
Now from my Java runner class, I have added #KarateOptions(tags = "~#ignore").
import org.junit.runner.RunWith;
import com.intuit.karate.KarateOptions;
import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
#RunWith(Karate.class)
#CucumberOptions(features = "classpath:com/meanwhileinhell/app/karate/feature/unsubscribe_user.feature")
#KarateOptions(tags = "~#ignore")
public class KarateTestUnSubscribeUserRunner {
}
However, I can still see my print statement in my setup class being called, and two POSTs being performed. I have also tried running my suite with the following cmd options, but again, still see the feature file run twice.
./gradlew clean test -Dkarate.env=local -Dkarate.options="--tags ~#ignore" --debug
I am following this wrong somewhere? Is there something I can add to my karate-config.js file? I am using Karate version 0.9.0.
Annotations only work on the "top level" feature. Not on "called" features.
If your problem is that the features are being run even when not expected, you must be missing something, or some Java class is running without knowing it. So please follow this process and we can fix it: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
EDIT: I think I got it - please don't mix CucumberOptions, in fact we deprecated it, use only KarateOptions. Even that is not recommended in 0.9.5 onwards and you should move to JUnit 5.
Read the docs: https://github.com/intuit/karate#karate-options

Groovy URL getText() returns a PasswordAuthentication instance

I am trying to download the content of a password-protected Gerrit URL in a Jenkins pipeline Groovy script. HTTPBuilder is not accessible so I am using the URL class with Authenticator:
// To avoid pipline bailing out since data PasswordAuthentication is non-serializable
#NonCPS
def getToString(data) {
data.toString()
}
def fetchCommit(host, project, version) {
withCredentials([usernamePassword(credentialsId: 'my-credentials',
usernameVariable: 'user',
passwordVariable: 'PASSWORD')]) {
proj = java.net.URLEncoder.encode(project, 'UTF-8')
echo "Setting default authentication"
Authenticator.default = {
new PasswordAuthentication(env.user, env.PASSWORD as char[])
} as Authenticator
echo "https://${host}/a/projects/${proj}/commits/${version}"
url = "https://${host}/a/projects/${proj}/commits/${version}".toURL()
result = getToString(url.getText())
echo "${result}"
}
}
The result is a PasswordAuthentication instance, and not the expected data:
[Pipeline] echo
java.net.PasswordAuthentication#3938b0f1
I have been wrestling with this for a while. I have tried different ways to setup the authentication and reading the data, but those mostly end up with an exception. Using eachLine() on the url does not enter the closure at all. The job also exits far to quickly, giving the impression it not even tries to make a connection.
Refs:
https://kousenit.org/2012/06/07/password-authentication-using-groovy/

How do you include multiple activities in the Gradle Liquibase plugin's runList field?

I’m using Gradle 2.7 on Mac Yosemite with Java 8. I’m using the Liquibase 1.1.1 plugin and would like to use it to do a couple of activities (build a test database and build my normal database). So I have
liquibase {
activities {
main {
File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
Properties properties = new Properties()
properties.load(new FileInputStream(propsFile))
changeLogFile 'src/main/resources/db.changelog-master.xml'
url properties['url']
username properties['username']
password properties['password']
}
test {
url 'jdbc:h2:file:target/testdb'
username 'sa'
}
runList = (
"test"
"main"
)
}
}
But I can’t figure out the proper syntax for runList. I get the error when running the above …
* Where:
Build file '/Users/myuser/Dropbox/cb_workspace/cbmyproject/build.gradle' line: 163
* What went wrong:
Could not compile build file '/Users/myuser/Dropbox/cb_workspace/cbmyproject/build.gradle'.
> startup failed:
build file '/Users/myuser/Dropbox/cb_workspace/cbmyproject/build.gradle': 163: expecting ')', found 'main' # line 163, column 2.
"main"
^
1 error
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
According to one of there example, the runList must be placed after the activity block:
liquibase {
activities {
main {
File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
Properties properties = new Properties()
properties.load(new FileInputStream(propsFile))
changeLogFile 'src/main/resources/db.changelog-master.xml'
url properties['url']
username properties['username']
password properties['password']
}
test {
url 'jdbc:h2:file:target/testdb'
username 'sa'
}
}
runList = 'test, main'
}
See the example here.
Hope this helps.

How can I specify a different config file for testing in Play 2.1

I would like to define different database connections for multiple test environments(Production, Staging, Development). After reading the post "How do I specify a config file with sbt 0.12.2 for sbt test?" it seems that it was possible in earlier versions of Play, by using the follwing SBT setting:
val main = play.Project(appName, appVersion, appDependencies).settings(
javaOptions in Test += "-Dconfig.file=conf/test.conf"
)
But if I use this setting in my Build.scala, I get the following error:
not found: value javaOptions
So my question is, how can I define different connections for different test environments?
Edit:
A possible workaround would be to override the default setting during testing. This can be done with a environment variable.
object Config {
var defaultConfig = Map(
"db.default.user" -> "user",
"db.default.pass" -> "************"
)
def additionalConfiguration(): Map[String, _] = sys.env.getOrElse("PLAY_TEST_SCOPE", "") match {
case "development" => {
defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/development"
defaultConfig
}
case "staging" => {
defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/staging"
defaultConfig
}
case "production" => {
defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/production"
defaultConfig
}
case _ => {
throw new Exception("Environment variable `PLAY_TEST_SCOPE` isn't defined")
}
}
}
And then running a fake application with this configuration.
FakeApplication(additionalConfiguration = Config.additionalConfiguration())
javaOptions is contained within the Keys object.
Make sure that you use the proper import in your Build.scala file:
import Keys._
we can mix the above solutions, to pass the config file as a parameter to sbt.
This will be useful to integrate the test in CI pipeline
First, in the Build.scala file
val testOptions = "-Dconfig.file=conf/" + Option(System.getProperty("test.config")).getOrElse("application") + ".conf"
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
javaOptions in Test += testOptions
)
Then, in the command line to run the test with integ.conf
sbt -Dtest.config=integ test
to use the default application.conf
sbt test
Update for Play 2.5.x
Explicit import for import Keys._ is no longer required, and the vm param for config resource location has changed.
javaOptions in Test += "-Dconfig.resource=<conf_name>.conf"
You can run your application from console with alternative config file, anyway you need to use -Dconfig.file with full path as there are some problems ... that I can't realize with other option. For an example in unix env:
play -Dconfig.file=/home/akkie/play/some-project/conf/local_akkie_dev.conf "~run 9123"
Of course for easier launching you can create bash script for calling this line.
Edit: Note that you don't need to write whole config in each additional config file, as you can just include your main config at beginning and then overwrite only required properties :
include "application.conf"
key.to.override=blah
Take a look to the official doc