griddynamics / mpl

[IT-36925] Jenkins Shared Modular Pipeline Library
https://blog.griddynamics.com/developing-a-modular-pipeline-library-to-improve-devops-collaboration/
Apache License 2.0
158 stars 96 forks source link

Passing pipeline CFG as env variable in build environment #70

Closed adhikasp closed 4 years ago

adhikasp commented 4 years ago

Is there any way to export the pipeline config defined by MPL to environment variable?

I have use case to enable/disable some auxilary service (like database, redis, kafka, etc) if we want to build apps that need them in testing phase. I want the apps to be able to parse the setting from env var and connect to the right port/db/whatever.

For now, I manually define the env var for each of the config. Something like this:

void injectIntegrationSettingToEnv(Map config) {
    env.'test_integration_postgresql_enabled' = config.test.integration.postgresql.enabled
    env.'test_integration_postgresql_username' = config.test.integration.postgresql.username
    env.'test_integration_postgresql_password' = config.test.integration.postgresql.password
    env.'test_integration_postgresql_database' = config.test.integration.postgresql.database
    env.'test_integration_postgresql_port' = config.test.integration.postgresql.port
    env.'test_integration_postgresql_host' = config.test.integration.postgresql.host
}
sparshev commented 4 years ago

Hmmm, I think it's not a good idea to export the whole CFG, but you can get the map, stored in some variable inside CFG, for example:

So you can get the map and iterate over it - just give the good name instead of config, combine the good env var and write it using writeFile to workspace.

Hopefully this will help)

sparshev commented 4 years ago

Or you can define env from the for loop, just use env."prefix_${it.key}" = it.value - something like that.

adhikasp commented 4 years ago

Hmmm, I think it's not a good idea to export the whole CFG

Agree, I only needed subset of the CFG anyway.

Or you can define env from the for loop, just use env."prefix_${it.key}" = it.value - something like that.

Ended up this way. Thanks for the suggestion 😄