Mixing Scripted Blocks

Mixing Scripted Blocks into a Declarative Pipeline

Declarative's script { } step opens a Scripted block inside steps, for the odd bit of logic the directives cannot express. The scr-mixed job checks BookNest's seed data after its checkout stage:

scr-mixed (excerpt): a script block inside a Declarative stageGroovy
    stage('Check seed data') {
      steps {
        script {
          def count = sh(script: 'grep -c \'"title"\' db/seed.json', returnStdout: true).trim()
          if (count.toInteger() != 6) {
            error "Expected 6 books, found ${count}"
          }
          env.BOOK_COUNT = count
        }
        sh 'echo "seed data OK: $BOOK_COUNT books"'
      }
    }
Output
+ echo seed data OK: 6 books
seed data OK: 6 books

Setting env.BOOK_COUNT hands the value to later steps and stages. Keep script blocks to a few lines; when they grow, move the logic into a shared-library step (Writing a Global Pipeline Step) and call it by name.