jenkinsci / configuration-as-code-plugin

Jenkins Configuration as Code Plugin
https://plugins.jenkins.io/configuration-as-code
MIT License
2.69k stars 718 forks source link

readFile with default #2229

Open gaborbernat opened 1 year ago

gaborbernat commented 1 year ago

What feature do you want to see added?

Currenlty it's not possible to use readFile substition with a fallback value.

password: "${file:/data/secret/jenkins-password.txt:-fallback}"

Now this logs a warning.

Upstream changes

No response

jetersen commented 1 year ago

@gaborbernat can't find the docs but your basically using nested substitution wrong that's all. This is a supported feature.

We have some tests that uses nested substitution: https://github.com/jenkinsci/configuration-as-code-plugin/blob/099c739fb73781f31ca927e4c7701074c3808cfc/plugin/src/test/java/io/jenkins/plugins/casc/SecretSourceResolverTest.java#L315

gaborbernat commented 1 year ago

That's not the same. That means use an env-var to set the file name. It does not set a default value for readFile without an env-var. https://github.com/jenkinsci/configuration-as-code-plugin/blob/master/docs/features/secrets.adoc#passing-secrets-through-variables suggest the default syntax should work for all not just env-vars.

jetersen commented 1 year ago

I mean what you want is this: ${${file:/data/secret/jenkins-password.txt}:-fallback}

gaborbernat commented 1 year ago

But that would log a nasty warning https://github.com/jenkinsci/configuration-as-code-plugin/blob/099c739fb73781f31ca927e4c7701074c3808cfc/plugin/src/main/java/io/jenkins/plugins/casc/SecretSourceResolver.java#L175-L178 🤔 can I avoid that? As in this case the file missing is expected and handled.

jetersen commented 1 year ago

it gives a warning but it should work.

I think the warning here makes sense, please configure the file.

jetersen commented 1 year ago

What you're asking for will not be supported because we are using an Apache StrSubstitutor implementation.

Unless your willing to write a customized implementation.