Skip to main content
In this lesson we convert the caching portion of a Declarative Jenkins pipeline into a Scripted pipeline. The goals:
  • Run a single stage: Installing Dependencies
  • Enable timestamper output
  • Disable concurrent builds (abort previous runs)
  • Use the NodeJS tool configured in Jenkins
  • Reuse the existing cache logic
Repository and branch
  • Working repo: solar-system
  • Current branch: feature/advanced-demo
Create a working branch for this lesson:
A Git-backed organization folder build may have been triggered automatically; you can cancel the initial run if the Jenkinsfile hasn’t been pushed yet.
A dark-themed Jenkins web UI showing the "Gitea-Organization" organization folder page with a sidebar of actions. The main panel lists repositories (exploring-agents, parameterized-pipeline-job-init, solar-system) and a "Disable Organization Folder" button.
Why Scripted differs from Declarative
  • Declarative pipelines use a top-level pipeline { ... } block and many built-in directives.
  • Scripted pipelines run inside node { ... } and require some Declarative-only features (for example options { ... }) to be expressed with properties(...) or appropriate wrappers.
Scripted pipelines always run inside node { ... }. Declarative-only directives (like options) must be converted to properties(...) or equivalent build wrappers when migrating to Scripted pipelines.
Quick mapping: Declarative → Scripted Converting NodeJS tool usage
  • Declarative pipelines commonly declare tools via tools { nodejs 'name' }.
  • In Scripted pipelines, resolve the tool with tool(...), then export it to env and update PATH so node/npm become available.
Example:
Using pipeline properties in Scripted pipelines
  • Declarative options like disableConcurrentBuilds(abortPrevious: true) must be applied via properties(...) in Scripted pipelines.
  • Call properties(...) once inside your node block (typically near the start of the Jenkinsfile).
Example:
Timestamper wrapper
  • To enable timestamped console output in Scripted pipelines, use the wrap step with the Timestamper build wrapper:
Final Scripted Jenkinsfile Below is a compact, corrected Scripted Jenkinsfile that demonstrates the translated flow: resolve the NodeJS tool, apply properties to disable concurrent builds, perform SCM checkout, and run a timestamped Installing Dependencies stage that uses the same cache logic.
Key notes about the example
  • env.NODEJS_HOME = "${tool 'nodejs-22-6-0'}" resolves the NodeJS installation by name and updates PATH so node and npm are available.
  • properties([...]) is the Scripted equivalent for certain Declarative options (in this example, disableConcurrentBuilds(abortPrevious: true)).
  • wrap([$class: 'TimestamperBuildWrapper']) enables timestamps for the wrapped stage(s).
  • The cache { ... } block mirrors Declarative cache-plugin usage; adjust files/cacheName per your cache plugin configuration.
Selected console excerpts (cleaned and corrected)
  • Checkout phase (shortened):
  • Cache restore/create messages:
Behavior of disableConcurrentBuilds(abortPrevious: true)
  • When multiple builds for the same branch are triggered, earlier running builds will be interrupted and marked as superseded; the latest build proceeds. Console output may include:
Summary: converting Declarative to Scripted (cheat-sheet)
  • Wrap your pipeline steps in node { ... }.
  • Resolve global tools via tool(...) and export to env.
  • Convert Declarative options using properties([ ... ]).
  • Use wrap([$class: 'TimestamperBuildWrapper']) to get timestamped logs.
  • Keep existing cache and shell steps unchanged inside the Scripted flow.
Further reading and references That’s it for this lesson — you now have a compact Scripted pipeline that installs dependencies, enables timestamper output, disables concurrent builds, and leverages the NodeJS tool and your existing caching setup.

Watch Video