Reusing Configuration Files

3 minute read

A typical scenario when building projects is having configuration files used across multiple pipelines or projects. Examples of configuration files that you can use are the settings.xml file for Maven projects, the npmrc file for NojeJS projects, plain xml files, Groovy files, JSON files, and so on.

The Configuration File Provider plugin allows CloudBees CI users to define configuration files using the UI and reuse them across different pipelines or projects. Those configuration files are injected as files in the pipeline workspace using the configFileProvider step.

Define your configuration files

To define a configuration file, follow these steps:

  1. Navigate to Manage Jenkins  Managed files (This menu is only available if the Configuration File Provider plugin is installed).

  2. Select Add a new Config on the left navigation pane.

  3. Select the type of configuration file you want to define.

  4. Select Next.

    Select the configuration file type
    Figure 1. Select the configuration file type
  5. In the Edit Configuration File screen, fill in the values for the configuration file.

    Configuration file values
    Figure 2. Configuration file values
    1 The ID field contains the unique identifier for the configuration file. This ID is used later in the pipeline to reference the configuration file.
    2 The Name field contains the name for the configuration file.
    3 The Comment field describes the configuration file.
    4 As the image above is for a settings.xml configuration file for a Maven project, you may need to add Server Credentials to the file. For other configuration file types, this section does not appear.
    5 The Content field contains the configuration file’s content. The content for the settings.xml file.
  6. Select Submit to save and create the configuration file.

Use your configuration files

When a configuration file is defined in the Manage Jenkins  Managed files screen, it can be injected in a pipeline workspace using the configFileProvider step. The configFileProvider step allows you to read the content of the configuration file using the fileId parameter and store the content in one of the following files:

  • A temporary file in the workspace, if the targetLocation parameter is not set.

  • A file in the workspace that is set with the targetLocation parameter.

If the parameter variable is set, you can use the value of this parameter as an environment variable to reference the file in the workspace where the configuration file content is stored.

The following example describes a declarative pipeline that reads a JSON configuration file defined in the controller UI and displays the file using a temporary file (no targetLocation) or a file in the workspace set by the user (targetLocation).

Temporary file
File in the workspace defined by the user
pipeline { agent { label 'linux' } stages { stage('build') { steps { configFileProvider([configFile(fileId: 'my-json-config', variable: 'JSON_CONFIG')]) { echo " =========== ^^^^^^^^^^^^ Reading config from pipeline script " sh 'cat $JSON_CONFIG' (1) echo " =========== ~~~~~~~~~~~~ ============ " } } } } }
1 The JsonConfig variable is defined and contains, as targetLocation is not defined, the path to a temporary file in the workspace with the configuration file’s content.
pipeline { agent { label 'linux' } stages { stage('build') { steps { configFileProvider([configFile(fileId: 'my-json-config', variable: 'JSON_CONFIG',targetLocation: 'tmp/some-file.json')]) { echo " =========== ^^^^^^^^^^^^ Reading config from pipeline script " sh 'cat "$JSON_CONFIG"' (1) echo " =========== ~~~~~~~~~~~~ ============ " } } } } }
1 The JsonConfig variable is defined and contains the path to the file defined in the targetLocation parameter. This file contains the configuration file’s content.

For more information on the configFileProvider step and the different fields and options, use the following steps:

  1. Access the Pipeline Syntax screen for any pipeline.

  2. Select the Snippet Generator tool.

  3. Select configFileProvider from the Sample Step dropdown list or refer to the Configuration File Provider plugin homepage.

Pipeline Syntax screen for the configFileProvider step
Figure 3. Pipeline Syntax screen for the configFileProvider step