Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way of loading config file from jenkins pipeline groovy script

I want to load a config value (something like json, yaml, xml or ini) from a jenkins pipeline script. When I try to use org.yaml.snakeyaml.Yaml I get

Scripts not permitted to use new org.yaml.snakeyaml.Yaml

I know I can unlock org.yaml.snakeyaml.Yam, but the message tells me that this does not seem to be the standard way of loading config files.

Is there a way of loading config files that is already unlocked?

like image 873
Nathan Avatar asked Oct 25 '16 07:10

Nathan


People also ask

How do I load Groovy script in Jenkins pipeline?

In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.

How do I access Jenkins config file?

In the example below, we are using Ubuntu 18.04. Note: The default location for the Jenkins configuration file on Ubuntu and Debian is /etc/default/jenkins. If you are working with RedHat, CentOS, or Fedora, the Jenkins configuration file is located at /etc/sysconfig/jenkins.

Which pipeline approach is used in Jenkins as a best practice?

Using a shell script within Jenkins Pipeline can help simplify builds by combining multiple steps into a single stage. The shell script also allows users to add or update commands without having to modify each step or stage separately.


2 Answers

If anyone looking for yaml parser in jenkinsfile, I recommend the following

 def yamlData = readYaml file: 'cae.yaml'

Reference : https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#code-readyaml-code-read-yaml-from-files-in-the-workspace-or-text

like image 132
f-society Avatar answered Nov 12 '22 07:11

f-society


Try using the JsonSlurper:

def config = new JsonSlurper().parse(new File("config.json"))
like image 28
Raniz Avatar answered Nov 12 '22 06:11

Raniz