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:
-
Navigate to
(This menu is only available if the Configuration File Provider plugin is installed). -
Select
Add a new Config
on the left navigation pane. -
Select the type of configuration file you want to define.
-
Select Next.
Figure 1. Select the configuration file type -
In the Edit Configuration File screen, fill in the values for the configuration file.
Figure 2. Configuration file values1 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. -
Select Submit to save and create the configuration file.
Use your configuration files
When a configuration file is defined in 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 |
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
).
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:
-
Access the Pipeline Syntax screen for any pipeline.
-
Select the Snippet Generator tool.
-
Select
configFileProvider
from the Sample Step dropdown list or refer to the Configuration File Provider plugin homepage.