externalizing configurations for test.datasource - grails

I have been able externalize DataSource configurations for development environment by updating grails.config.locations in Config.groovy and then setting specific configurations in .grails/${appName}-config.properties as following:
dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url = jdbc:mysql://localhost/db-dev
dataSource.username = root
dataSource.password = pass
For the 'test' environment, i have different Database so i would like to updated test.dataSource.url accordingly but this doesn't work. It still takes the 'dev' dataSource.url when running 'grails test' mode(i.e. grails test dgm-update). How to externalize test environment DataSource configuration?
Thank You for helping

grails.config.locations = ["file:${userHome}/.grails/${appName}-config-${grails.util.Environment.current.name}.properties"]
and then put nested files for each env:
/home/me/.grails/myapp-config-dev.properties
/home/me/.grails/myapp-config-test.properties
/home/me/.grails/myapp-config-production.properties

Try to use Groovy external configuration, i.e. ".grails/${appName}-config.groovy" with environment specific DSL:
environments {
test {
dataSource {
...
}
}
}

Related

Get jenkinsfile environment vars from a file located in a repo

I need to take env vars for different apis from a repo that contains all the .env files(one per api). For example:
Inside /env_file directory:
foo.env
bar.env
Is it possible to use a .env file located in a repo to get the env vars instead of having them set manually in environment {}? In case it is, how could be referenced each file in each api stage?
Thanks in advance! Never worked in Jenkinsfile like this before. Sorry if this is a weird question.
You can inject environment into Jenkins pipeline's global variable: env which is a Map and the wirepuller of environment {}, but it has a limitation:
Allow insert new environment, NOT allow override existing environment
If your those .env files has same environment and you need to load them together, you will run into 'override existing environment' issue.
If you're not in that case, you can do as following:
pipeline {
stages {
stage('Test endpoint A') {
script {
def props = readProperties file: 'A.env'
for (p in props) {
env[p.key] = p.value
}
}
// other steps
}
stage('Test endpoint B') {
script {
def props = readProperties file: 'B.env'
for (p in props) {
env[p.key] = p.value
}
}
// other steps
}
}
}

Grails 3.0.9 gradle new task with custom environment

I'm trying to create a new task in build.gradle for running "check" with a custom environment called "integration".
I've tried several things and I've been able to run the check task from my custom task but I can't find the way to set the environment in int.
This is the task that I have right now in build.gradle. The comments are for things that I've tried but all fail because the property doesn't exist.
I've also tried setting the task as type:GrailsTask, but with no luck
task integrationCheck(){
dependsOn "check"
//env = "integration"
//grailsEnv = "integration"
//tasks.check.env = "integration"
//check.env = "integration"
//project.property("grailsEnv")
}
Edit: per #vinay-prajapati suggestion
Have tried with this:
task integrationCheck << {
systemProperty 'spring.profiles.active', 'integration'
}
And it gives this error:
Execution failed for task ':integrationCheck'.
> Could not find method systemProperty() for arguments [spring.profiles.active, integration] on root project 'my-project'

Setting environment variables from Gradle

I need to execute from Gradle an Ant script which relies on environment variables.
Ant uses <property environment="env"/> for it.
I tried to do env.foo="bar" in Gradle, but it throws a Groovy exception.
What is the proper way to pass environment variables from Gradle to Ant?
From the gradle 2.0 docs, i see something like this is possible
test {
environment "LD_LIBRARY_PATH", "lib"
}
Or in this case could use this
systemProperty "java.library.path", "lib"
It is impossible to set environment variables from Gradle or JVM in general, but it is possible to trick Ant like this:
ant.project.properties['env.foo'] = 'bar'
Accepted solution from #Sergey:
ant.project.properties['env.foo'] = 'bar'
Does not work for me on gradle 2.9 and ant 1.9.7.
That did not thrown any error, but do nothing. Indeed if you are look at code it implemented as:
public Hashtable<String, Object> getProperties() {
return PropertyHelper.getPropertyHelper(this).getProperties();
}
where org.apache.tools.ant.PropertyHelper#getProperties is:
public Hashtable<String, Object> getProperties() {
//avoid concurrent modification:
synchronized (properties) {
return new Hashtable<String, Object>(properties);
}
}
So it make explicit copy and it can't work.
The way do it correctly in gradle file:
ant.project.setProperty('env.foo', 'bar')
Documentation mention few other ways (note, without project):
ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name: 'buildDir', location: buildDir)

Grails: Is there a debug flag I can check?

I have a few log.debugs() that I don't want to process (since they are heavy) unless the app is currently in debug mode (not production).
Is there a way to check if the grails app is currently in debug mode/development mode?
You can test if the current environment is dev (for example) using the following:
import grails.util.Environment
if (Environment.current == Environment.DEVELOPMENT ) {
// Do your dev logging here
}
IMO, a better solution than hard-coding the env where this logging happens, is to configure it. For example, to enable debug logging for this class only in the dev environment. add the following to Config.groovy
log4j = {
appenders {
// config for stdout and logfile appenders omitted
}
// log everything at error level to stdout and logfile appenders
root {
error 'stdout', 'logfile'
}
environments {
development {
// log this class at debug level in dev env only
debug 'com.example.MyClass'
}
}
}
in the config.groovy the environments are defined.
you can specify what you want the log configuration to be based on the environment the application in running in
environments {
development {
log4j = {
// determine what appenders are logging in development...
}
}
production {
log4j = {
// determine what appenders are logging in production...
}
}
}

How to extend per environment configuration in grails

It seems that only grails.serverURL and grails.path are recognized as per environment configrautions. bla and foo are ignored and could not be used in application
Anyone could solves this and provide a way to get bla and foo configured per environment?
environments {
production {
grails.serverURL = "http://alpha.foo.de"
grails.path = ""
bla = "text"
foo= "word"
}
test {
grails.serverURL = "http://test.foo.de"
grails.path = ""
bla = "othertext"
foo= "otherword"
}
}
Since Grails 3.0, both Groovy or YAML syntax can be used. The new main application configuration file is /conf/application.yml but you can continue to use your existing groovy configuration defining a /conf/application.groovy file.
This is an example of datasource definition per environment (in YAML):
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb
production:
dataSource:
dbCreate: update
url: jdbc:h2:prodDb
myenv:
dataSource:
dbCreate: update
url: jdbc:h2:myenvDb
To run a grails command in a specific environment you can use:
grails [environment] [command name]
To target an environment different from dev, test and prod you can use:
grails -Dgrails.env=myenv run-app
See the grails environment documentation or this example application for more information.
All the ConfigSlurper variables are scoped by environment. What you've shown above should work fine.
When you use grails run-app, you are running in the development environment by default. Could that be your issue?
Try...
> grails prod run-app
This should give you bla (text) and foo (word)
> grails test run-app
This should give you bla (othertext) and foo (otherword)
Config.groovy setting
environments {
development {
grails.serverURL = "http://alpha.foo.de"
grails.path = "/bar"
staticServerURL = "http://static.foo.de"
staticPath = "/static"
}
}
source code index.gsp
${grailsApplication.config.staticServerURL}a
${grailsApplication.config.staticPath}b
${grailsApplication.config.grails.serverURL}c
${grailsApplication.config.grails.path}d
what is printed out when started with grails run-app
a b http://alpha.foo.dec /bard
Check in your Config.groovy that you have the import for Environment
import grails.util.Environment
Otherwise the Environment.current is empty.

Resources