Jenkins libraries configuration in Jenkinsfile Right now we have many projects with similar Jenkinsfile's. Instead of copypasting we're developing a shared library with some utilities. This library is loaded dynamically in each Jenkinsfile, and has some parameters that repeat among calls. For example, an utility has a custom Slack message posting function, and the channel, bot token, and some fixed text are always the same.

Instead of providing those parameters to every call to this Slack function we have thought of making a config file and loading it globally at the beginning with all these "constant" parameters.

We tried putting a config.json and using the readJSON step like this:

def load(path) {
    env.CONFIGURATION =  readJSON(file: path)
}


So the CONFIGURATION is an environment variable and so it's global. The problem is that it seems that environment variables can only be strings, so the JSON is broken (we cannot do env.CONFIGURATION.slack.channel for example)

We tried the following also in Jenkinsfile:

def CONFIG = readJSON(file: path)


But CONFIG then is not visible inside the library scripts.

So now, the questions:


Is our approach correct? Putting all common config in one file and
loading it for the library scripts to see.
If it's not correct, how would you deal with our problem? Simply
passing the config as parameter on every call and that's all?


Thanks in advance for your help.
You should look at Global Shared Libraries, which will allow you to reuse logic across different Jenkinsfiles.

Rather than depending on the environment, it's probably safer to explicitly pass parameters and returns values from the functions, which will make their behavior more explicit. Another side benefit is that you will be able to unit test them using