<p>My company has a small pipeline library that we implicitly load for every build. Is there a way to overload the <code>node {</code> block of every build transparently?</p>

<p>My specific case is that I'm provisioning kubernetes slaves with the kubernetes plugin, and I want to provide a default YAML template, while allowing users to pick another template or override specific values. Eg:</p>

<pre><code>node {
  // Gets you a Pod with a DinD engine with a low CPU/Mem request/limit
}
</code></pre>

<p>Optionally overridden by name:</p>

<pre><code>node('2-core') {
  // Gets you a Pod with a DinD engine with 2 CPU/ more Mem request/limit
}
</code></pre>

<p>Or overridden with a template:</p>

<pre><code>import com.foo.utils.PodTemplates

slaveTemplates = new PodTemplates()

slaveTemplates.bigPod {
  node {
    // Big node
  }
}
</code></pre>

<p>Or:</p>

<pre><code>def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, yaml: """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:
  - name: redis
    image: redis
"""
) {
  node (label) {
    // Same small pod as before PLUS a redis container
  }
}
</code></pre>

<p>This seems trickiest, since you want the values of the parent to override the values of the child.</p>

<p>You can do this, but, in my opinion, it will lead to confusing behavior and possibly strange error cases.</p>

<p>For example:</p>

<p><code>echo.groovy</code></p>

<pre><code>def call(String string) {
  steps.echo "Calling step echo: $string"
}
</code></pre>

<p><code>Jenkinsfile</code></p>

<pre><code>echo 'hello'
</code></pre>

<p>Output:</p>

<pre><code>Calling step echo: hello
</code></pre>



<ul>
<li>There is a <a href="http://unethicalblogger.com/2017/08/03/overriding-builtin-steps-pipeline.html" rel="nofollow noreferrer">blog post here</a> that demonstrates this a little more in depth</li>
<li>Paid support for some pipeline restriction tools are offered by <a href="https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#pipeline-custom-factories" rel="nofollow noreferrer">CloudBees</a> that might solve your use case</li>
</ul>

<p>The heaviest way to accomplish this is to of course write a plugin.</p>
