Skip to content
Latchkey

How to Read a YAML or JSON Config in a Jenkins Pipeline

readYaml and readJSON (Pipeline Utility Steps plugin) parse a file or string into a Groovy map you can drive the build from.

Install Pipeline Utility Steps, then call readYaml(file: '...') or readJSON(file: '...'). The result is a normal Groovy map/list you can index to configure stages.

Steps

  • Install the Pipeline Utility Steps plugin.
  • Call readYaml(file: 'config.yml') (or readJSON).
  • Index the returned map to read values into the build.

Jenkinsfile

Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Configure') {
      steps {
        script {
          def cfg = readYaml(file: 'ci/config.yml')
          def services = cfg.services
          echo "Deploying ${services.join(', ')} to ${cfg.environment}"
        }
      }
    }
  }
}

Gotchas

  • Maps and lists from readYaml are not serializable across a CPS step; read inside one script block.
  • Use text: instead of file: to parse a string you already have in a variable.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →