Certified Jenkins Engineer

Pipeline Enhancement and Caching

Demo Sharing a file between containers

In Kubernetes, all containers in the same Pod share the same filesystem volume. This demo shows how one Jenkins pipeline stage writes a file in an Ubuntu container, and a subsequent stage reads it in a Node.js container—both running side by side in the same Pod.

Prerequisites

  • A Jenkins server with the [Kubernetes Plugin][] and [Blue Ocean][] installed.
  • A connected Kubernetes cluster configured as a cloud agent.

Jenkinsfile Overview

The following Jenkinsfile defines two stages: one running on Ubuntu, the other in a Node.js sidecar. They share the workspace via an emptyDir volume.

pipeline {
  agent any
  stages {
    stage('Write File in Ubuntu') {
      steps {
        // On the default Ubuntu container
        sh 'hostname'
        sh 'echo important_UBUNTU_data > ubuntu-${BUILD_ID}.txt'
        sh 'ls -ltr ubuntu-${BUILD_ID}.txt'
        sh 'sleep 60s' // keep the Pod alive briefly
      }
    }
    stage('Read File in Node.js') {
      steps {
        container('node-container') {
          sh 'node -v'
          sh 'npm -v'
          // List and display the same file
          sh 'ls -ltr ubuntu-${BUILD_ID}.txt'
          sh 'cat ubuntu-${BUILD_ID}.txt'
        }
      }
    }
  }
}

Executing the Pipeline

When this pipeline runs, Jenkins dynamically provisions a Pod with two containers—ubuntu-container (default) and node-container (sidecar)—sharing an emptyDir workspace volume.

The image shows a Jenkins dashboard displaying the status of a pipeline named "k8s-cloud-agent-demo," with several stages and their execution results. The interface includes options like "Delete Pipeline" and "Open Blue Ocean" on the left sidebar.

Under the hood, the Pod specification looks like this:

apiVersion: v1
kind: Pod
metadata:
  name: k8s-cloud-agent-demo
spec:
  restartPolicy: Never
  volumes:
    - name: workspace-volume
      emptyDir: {}
  containers:
    - name: ubuntu-container
      image: ubuntu:latest
      volumeMounts:
        - name: workspace-volume
          mountPath: /workspace
    - name: node-container
      image: node:18
      volumeMounts:
        - name: workspace-volume
          mountPath: /workspace

The image is a screenshot from the Kubernetes documentation website, showing a diagram of a multi-container Pod with a file puller sidecar, a web server, and a shared volume.

Pipeline Log Example

The log below confirms the file created in the Ubuntu container is accessible in the Node.js container:

[Pipeline] stage
[Pipeline] { (Write File in Ubuntu)
+ hostname
k8s-cloud-agent-demo-5-rv7b8-8frt0-n41pw
+ echo important_UBUNTU_data > ubuntu-5.txt
+ ls -ltr ubuntu-5.txt
-rw-r--r-- 1 root root 22 Nov 10 09:47 ubuntu-5.txt
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Read File in Node.js)
+ node -v
v18.20.4
+ npm -v
6.14.17
+ ls -ltr ubuntu-5.txt
-rw-r--r-- 1 root root 22 Nov 10 09:47 ubuntu-5.txt
+ cat ubuntu-5.txt
important_UBUNTU_data
[Pipeline] // stage

Static Nodes vs. Dynamic Agents

You can run Jenkins jobs on:

Agent TypeDescriptionProsCons
Static Node (VM)A long-running machine with preinstalled tools.Always available; ideal for specialized buildsIdle resource costs; manual maintenance required.
Docker AgentContainers spun per job and removed when done.Custom images; resource-efficientRequires Docker environment setup.
Kubernetes Cloud AgentPods created on demand, destroyed after completion.Auto-scaling; shareable volumes per PodKubernetes cluster required; plugin configuration.

Tip

Dynamic agents help you package toolchains into container images, cutting down on idle resources and improving build scalability.

Further Reading

Watch Video

Watch video content

Previous
Demo Utilize Kubernetes Pod as Agent