With the triggers from the previous lesson, orders-api-build runs several times a day without anyone asking it to. Every build generates files, leaves traces on disk and produces a .jar that, for now, is lost as soon as the next build starts. In this lesson you will gain a thorough understanding of where everything lives in Jenkins: the workspace, each build's temporary working directory; artifacts, the files you decide to keep permanently; and the history, the record of every build. You will learn to archive the orders-api .jar, track it with fingerprints, pass it reliably to orders-api-smoke and, above all, configure a retention policy so that the history does not end up filling the controller's disk.
Workspace vs. Job Directory
Jenkins stores a job's information in two different places, and mixing them up is a common source of errors:
| Workspace | Job directory | |
|---|---|---|
| Path (in our environment) | /var/jenkins_home/workspace/orders-api-build |
/var/jenkins_home/jobs/orders-api-build |
| Contents | Checked-out code, target/, temporary files |
config.xml, build history, consoles, archived artifacts |
| Where it exists | On every node where the job has run | Always on the controller |
| Lifetime | Temporary: it can be deleted at any time | Permanent, until retention removes it |
| What it is for | Running the build | Remembering what happened and keeping results |
Right now both are in the same container because we build on the built-in node. When builds move to the linux-agent-01 agent in module 5, the workspace will be on the agent's machine, while the history and artifacts will remain on the controller. That is why you should never rely on the workspace to keep anything.
Exploring the Workspace from the Interface and from Docker
From the Interface
On the orders-api-build page, click Workspace in the side menu. You will see a file browser with the contents of the workspace: .mvn, src, target, mvnw, pom.xml… You can:
- Open directories and view the contents of text files.
- Download a specific file by clicking it.
- Download an entire directory with the (all files in zip) link.
From Docker
For a closer look, use the terminal:
docker exec jenkins-controller ls -la /var/jenkins_home/workspace/
docker exec jenkins-controller ls -la /var/jenkins_home/workspace/orders-api-build
docker exec jenkins-controller ls -lh /var/jenkins_home/workspace/orders-api-build/target- The first command lists the workspaces of every job:
orders-api-build,orders-api-smokeand possibly directories with the@tmpsuffix, which Jenkins uses for internal temporary files. - The second shows the contents of the workspace, including the hidden
.gitdirectory: the workspace is a full clone of the repository. - The third shows
target/, withorders-api-1.0.0-SNAPSHOT.jarandorders-api-1.0.0-SNAPSHOT.jar.original(the jar before Spring Boot repackaged it).
If you need to explore at your leisure, open an interactive shell inside the container:
docker exec -it jenkins-controller bash
cd /var/jenkins_home/workspace/orders-api-build
git log --oneline -3
exit-itconnects your terminal to the container's terminal interactively.- Inside, you can use the usual commands.
exittakes you back to your machine.
Look, don't touch: do not modify workspace files by hand. The next build will start from that state, and manual changes cause failures that are impossible to reproduce.
What Happens to the Workspace Between Builds
By default, Jenkins does not clean the workspace between builds: the next build reuses the same directory. This has advantages and drawbacks:
- Advantage: Git only fetches the new commits instead of cloning everything, and build tools can take advantage of earlier results.
- Drawback: leftovers from one build can affect the next one (generated files, classes from code that has already been deleted, temporary test files).
In orders-api-build you enabled Clean before checkout in Creating and Running Jobs, which uses git clean -fdx to delete everything that is not under version control. It strikes a good balance: the cloned repository is kept, but target/ is regenerated on every build. Check it: the date of target/orders-api-1.0.0-SNAPSHOT.jar changes with every build.
Concurrent Builds and the @2 Suffix
A workspace can only be used by one build at a time. If a job has Execute concurrent builds if necessary enabled and two builds coincide on the same node, Jenkins gives the second one a workspace with a suffix:
/var/jenkins_home/workspace/orders-api-build ← build #20 /var/jenkins_home/workspace/orders-api-build@2 ← build #21, simultaneous
If three builds coincided, @3 would appear, and so on. These directories are not deleted when the builds finish: they stay on disk and are reused by future concurrent builds. Bear in mind that each one is a full clone of the repository, with its own target/. You will see this in practice in exercise 1.
Deleting the Workspace
There are several ways to delete a workspace, depending on when and how much you want to delete:
| Mechanism | Where | When it deletes |
|---|---|---|
| Wipe Out Current Workspace | The job's Workspace page, side menu | Now, once, by hand |
| Delete workspace before build starts | Configure → Environment (Workspace Cleanup plugin) | At the start of every build |
| Delete workspace when build is done | Post-build Actions (Workspace Cleanup plugin) | At the end of every build, with options depending on the result |
| Clean before checkout | Source Code Management → Additional Behaviours | At the start, untracked files only |
Wipe Out Current Workspace is useful when you suspect the workspace is in a bad state (for example, a corrupted Git repository). The next build will clone again from scratch.
The Workspace Cleanup plugin, which provides the two automatic options, also lets you specify patterns of files to delete or keep. You will look at it in more detail in Using Jenkins Plugins; in pipelines, its equivalent is the cleanWs() step.
Archiving Artifacts: Archive the Artifacts
An artifact is a file produced by a build that is kept permanently, associated with that specific build. Unlike the workspace, it survives subsequent builds. Let's archive the orders-api .jar.
- Open Configure in
orders-api-build. - Post-build Actions → Add post-build action → Archive the artifacts.
- In Files to archive, enter:
- Click Advanced and check:
- Archive artifacts only if build is successful.
- Fingerprint all archived artifacts.
- Save.
The target/*.jar pattern is relative to the workspace and uses Ant syntax: * matches any name within a directory and ** matches any number of directories (for example, **/*.jar would look for jars in any subdirectory). Notice that *.jar does not match orders-api-1.0.0-SNAPSHOT.jar.original, because that file ends in .original; exactly what we want.
The most important advanced options:
| Option | Effect |
|---|---|
| Excludes | Pattern of files to exclude from archiving |
| Do not fail build if archiving returns nothing | Without it, if the pattern finds no files, the build fails. Leave it unchecked: if there is no jar, something is wrong |
| Archive artifacts only if build is successful | Archives nothing if the build did not succeed, so defective jars do not take up disk space |
| Fingerprint all archived artifacts | Calculates the fingerprint of each archived file (see below) |
Do not worry about the order relative to Build other projects: Jenkins first runs the actions that record results (such as archiving) and then the ones that send notifications or trigger other jobs.
Start a build. At the end of the console you will see:
[INFO] BUILD SUCCESS ... Archiving artifacts Recording fingerprints Triggering a new build of orders-api-smoke Finished: SUCCESS
Where Artifacts Are Stored and How to Download Them
The build page now has a Build Artifacts section with the file and its size, and the job page has Last Successful Artifacts. On disk, the artifact is copied into the build directory, inside the job directory:
docker exec jenkins-controller ls -lh /var/jenkins_home/jobs/orders-api-build/builds/
docker exec jenkins-controller find /var/jenkins_home/jobs/orders-api-build/builds -name "*.jar"You will see paths like:
The structure is JENKINS_HOME/jobs/<job>/builds/<number>/archive/ followed by the relative path the file had in the workspace. Notice what this implies: every archived build keeps its own copy of the jar on the controller. With a jar of about 20 MB and several builds a day, disk usage grows fast. We will solve this with the retention policy at the end of the lesson.
Downloading Artifacts
- From the interface: click the file name under Build Artifacts.
- With a permanent link: combine the permalinks you learned about in Jenkins Dashboard Overview with the
artifact/path:
http://localhost:8080/job/orders-api-build/lastSuccessfulBuild/artifact/target/orders-api-1.0.0-SNAPSHOT.jar
This URL always returns the jar from the last successful build, whatever its number. It is very handy for scripts, or for Marta to download the latest valid version for her manual testing.
- All of a build's artifacts in a ZIP file: append
*zip*/archive.zipto theartifact/path:
- From the command line: with security enabled, you have to authenticate with a user and their API token (the one in the example is fake; the API is covered in 08-04):
curl -fSL -u "laura:11aa22bb33cc44dd55ee66ff77fake00" \
-o orders-api.jar \
"http://localhost:8080/job/orders-api-build/lastSuccessfulBuild/artifact/target/orders-api-1.0.0-SNAPSHOT.jar"-f: fails with an error code if the server returns an HTTP error, instead of saving the error page as if it were the jar.-S: shows the error, if any;-L: follows redirects.-o orders-api.jar: the name of the output file.
Jenkins artifacts vs. an artifact repository: archiving in Jenkins is perfect for short-term build results. For versions that are released, deployed or shared between teams, the right approach is a dedicated repository such as Nexus (
nexus.tecnoshop.local), which you will see in Artifact and Image Repositories.
Fingerprints: Tracking a File
A fingerprint is the MD5 hash of a file. For each fingerprint, Jenkins records which build of which job the file first appeared in and which other builds have used it since. This lets you answer questions such as:
- "This jar on the staging server: which build produced it?"
- "Has
orders-api-smokealready tested the jar from build#12?"
Since you checked Fingerprint all archived artifacts, every build page shows a See Fingerprints link. When you open the jar's fingerprint you will see something like:
orders-api-1.0.0-SNAPSHOT.jar MD5: 9f2c4e... Introduced 3 min ago orders-api-build #12 Usage orders-api-build #12 orders-api-smoke #7
The orders-api-smoke line will appear once that job copies the artifact, as you will do in the next section. Fingerprints are stored in /var/jenkins_home/fingerprints/.
To identify a stray jar, calculate its MD5 (md5sum orders-api.jar) and look it up on Jenkins' Check File Fingerprint page (http://localhost:8080/fingerprintCheck), which lets you upload the file and tells you which build it came from.
Copying Artifacts Between Jobs: Improving orders-api-smoke
In Creating and Running Jobs, orders-api-smoke read the jar directly from the orders-api-build workspace, with the problems you saw: coupling to another job's path and race conditions. Now that the jar is archived, it can copy the exact artifact from the build that triggered it. This is done with the Copy Artifact plugin.
Step 1: Install Copy Artifact
- Manage Jenkins → Plugins → Available plugins.
- Search for
Copy Artifact, select it and click Install. - Wait for the status to show that it was installed successfully.
That is enough for now; in Using Jenkins Plugins you will study plugin management in depth.
Step 2: Allow Copying in the Source Job
For security reasons, Copy Artifact requires the source job to explicitly authorize which jobs may copy its artifacts. In the Configure page of orders-api-build, General section:
- Check Permission to Copy Artifact.
- In Projects to allow copy artifacts, enter
orders-api-smoke. - Save.
Without this permission, the copy step will fail with a message saying that the project cannot be found or that there is no permission to copy its artifacts.
Step 3: Reconfigure orders-api-smoke
In the Configure page of orders-api-smoke:
- Environment: check Delete workspace before build starts. That way, a jar copied by an earlier build will never be checked.
- Build Steps → Add build step → Copy artifacts from another project, with these values:
| Field | Value |
|---|---|
| Project name | orders-api-build |
| Which build | Upstream build that triggered this job |
| Use "Last successful build" as fallback | Checked |
| Artifacts to copy | target/*.jar |
| Target directory | (empty) |
| Fingerprint Artifacts | Checked |
- Drag this step so that it comes before the Execute shell step.
- In the Execute shell step, change only the first line so that it uses a path relative to the job's own workspace:
The rest of the script stays the same. Save.
The most useful Which build options:
| Option | Copies from the build… |
|---|---|
| Upstream build that triggered this job | That triggered this build (the most precise option in a chain) |
| Latest successful build | The last successful build of the source job |
| Specific build | With a specific number |
| Specified by permalink | Given by a permalink (last stable, last completed…) |
| Specified by a build parameter | Given by a parameter of the job itself |
The fallback to the last successful build means you can start orders-api-smoke by hand, without an upstream build, and it will still work. Start orders-api-build; in the smoke job's console you will see:
Started by upstream project "orders-api-build" build number 13 ... [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Done Copied 1 artifact from "orders-api-build" build number 13 [orders-api-smoke] $ /bin/sh -xe /tmp/jenkins5567890123456789012.sh + JAR=target/orders-api-1.0.0-SNAPSHOT.jar ... Smoke OK: the jar exists and has a valid format Finished: SUCCESS
Copied 1 artifact from "orders-api-build" build number 13 confirms that exactly the jar from that build was checked. The chain no longer depends on another job's workspace or on the node each job runs on.
Build History on Disk
Take a look at the job directory:
docker exec jenkins-controller ls -la /var/jenkins_home/jobs/orders-api-build
docker exec jenkins-controller ls -la /var/jenkins_home/jobs/orders-api-build/builds/13The typical structure is:
/var/jenkins_home/jobs/orders-api-build/
├── config.xml ← job configuration (everything you see in Configure)
├── nextBuildNumber ← the number the next build will get
└── builds/
├── 12/
├── 13/
│ ├── build.xml ← metadata: result, duration, cause, parameters...
│ ├── changelog.xml ← commits included in the build
│ ├── log ← the console output
│ └── archive/ ← archived artifacts
│ └── target/orders-api-1.0.0-SNAPSHOT.jar
└── ... ← depending on the version, also links or a "permalinks" fileEach build is a numbered, self-contained directory. That is why deleting a build's directory removes all of its information, and why retention essentially boils down to deleting old directories from builds/. Backups, migrations and cleanups all revolve around this structure, as you will see in Backing Up and Restoring Jenkins.
Description, Display Name and Keep This Build Forever
On a build page, Edit Build Information lets you add:
- Display Name: replaces
#13in the interface. For example,demo-sprint-42. The number still exists and the URL does not change. - Description: text that appears on the build page and in Build History. For example,
Version shown to the business in the sprint 42 demo. The Build History filter also searches descriptions.
The Keep this build forever button (on the build page) marks the build as protected: it shows a padlock icon and no retention policy will delete it. To remove the protection, use Don't keep this build forever. Save it for builds with real historical value (a delivered release, the reproduction of an incident); if you use it often, disk usage will start growing out of control again.
Changes and Comparing Builds
A build's Changes page lists the commits included since the previous build, with author and message; details shows the modified files. The job's Changes page shows the changes across several consecutive builds, which lets you reconstruct the story: "build #15 failed and included these three commits".
Jenkins does not offer a direct comparison between two builds, but you have everything you need to do one yourself:
| What to compare | Where to look |
|---|---|
| Code | Each build's Revision; in your local copy, git log --oneline <revA>..<revB> lists the commits between the two |
| Parameters | Each build's Parameters page |
| Duration | Took… on each build, or the duration column in Build History |
| Output | The plain-text consoles |
To compare two consoles, download them as plain text (the View as plain text link) and use diff:
diffshows the lines that differ between two files;lesslets you scroll through the result with the arrow keys (exit withq).
Retention Policy: Discard Old Builds
Without a retention policy, Jenkins keeps every build forever. At TecnoShop, with polling every 15 minutes, the nightly build and manual runs, orders-api-build can pile up thousands of builds in a few months, each with its console and its 20 MB jar. The outcome is predictable:
- The controller's disk fills up and Jenkins stops working.
- The interface and startup become slower, because Jenkins loads build information.
- Backups grow and take longer and longer.
Configuring It
In Configure → General, check Discard old builds. With the Log Rotation strategy, four fields appear (the last two after clicking Advanced):
| Field | What it controls |
|---|---|
| Days to keep builds | Builds older than N days are deleted entirely |
| Max # of builds to keep | Only the N most recent builds are kept |
| Days to keep artifacts | After N days, the artifacts are deleted but the build is kept (console, result, changes) |
| Max # of builds to keep with artifacts | Only the N most recent builds keep their artifacts |
The separation between builds and artifacts is the key: the history (consoles, results) takes up little space and is valuable for diagnosis; artifacts take up a lot of space and usually only the recent ones are useful. Configure orders-api-build like this:
| Field | Value |
|---|---|
| Days to keep builds | 30 |
| Max # of builds to keep | 100 |
| Days to keep artifacts | (empty) |
| Max # of builds to keep with artifacts | 5 |
When there are several limits, whichever deletes first applies. And for orders-api-smoke, which archives nothing, Max # of builds to keep = 50 is enough.
How It Is Applied
- Retention is applied when each build of the job finishes (and periodically, in the background), not when you save the configuration. You will not see the old builds disappear until the next build.
- Never deleted: builds marked with Keep this build forever, builds that are running, and the last successful build and last stable build, so that the most important permalinks keep working.
Recommendations
| Job type | Builds | Artifacts | Comment |
|---|---|---|---|
| CI for the main branch | 30 days / 50-100 builds | Last 5-10 | The orders-api-build case |
| Branches and pull requests | 7-14 days / 10-20 builds | Last 1-3 or none | Many are generated and they expire quickly |
| Nightly builds | 30 days | Last 7 | To compare night by night |
| Checks and maintenance | 20-50 builds | None | Like orders-api-smoke |
| Released versions | As many as needed | In Nexus, not in Jenkins | See 06-05 |
To keep an eye on how much space each job takes up:
du -shshows the total size (-s) of each directory in human-readable format (-h). We usesh -c '...'so that the*wildcard is expanded inside the container and not on your machine.
Monitoring the controller's disk is covered in Monitoring Jenkins.
Common Mistakes and Tips
ERROR: No artifacts found that match the file pattern "target/*.jar": the pattern does not find anything. It is usually a mistake in the pattern or a build that never got as far as packaging. Check the path in Workspace.- The smoke job fails with a Copy Artifact permissions error: Permission to Copy Artifact is missing in
orders-api-build, or the job name is misspelled. - The smoke job copies an old jar: you started the smoke job by hand and the fallback to the last successful build kicked in. That is expected; to test a specific build, start it from the upstream job.
- Keeping artifacts from every build with no limit: this is the most common cause of full disks in Jenkins. Always configure Max # of builds to keep with artifacts.
- Expecting retention to delete builds when you save: it is applied when the next build finishes.
- Relying on workspace files between builds: if you need something from one build in another, archive it as an artifact.
- Using
**/*as the archiving pattern: you would archive the entire repository,.gitincluded. Be specific. - Tip: if a build is important, give it a Description as well as protecting it. Six months from now, nobody will remember why
#13has a padlock.
Exercises
Exercise 1. Create a temporary Freestyle job concurrent-test (with no source code) with an Execute shell step that runs pwd and sleep 30, and enable Execute concurrent builds if necessary. Start two builds back to back. Which path does pwd show in each one? Then disable the option and repeat. What changes? When you are done, delete the job and use docker exec to check whether its workspaces are still on disk.
Exercise 2. Download the jar from the last successful build using its permanent link and calculate its MD5 with md5sum. Compare it with the MD5 shown by See Fingerprints for that build. Do they match? Which jobs appear in the fingerprint's Usage section?
Exercise 3. A job has Max # of builds to keep = 5 and Max # of builds to keep with artifacts = 2. Its history, from oldest to newest, is: #1 SUCCESS (with Keep this build forever), #2 to #7 SUCCESS, #8 FAILURE. When #9 (SUCCESS) finishes, which builds are kept and which ones keep their artifacts?
Solutions
Solution 1. With concurrent builds enabled, both builds run at the same time: the first shows /var/jenkins_home/workspace/concurrent-test and the second /var/jenkins_home/workspace/concurrent-test@2. Without it, the second build waits in the queue until the first one finishes, and both use /var/jenkins_home/workspace/concurrent-test. When you delete the job, Jenkins deletes its directory in jobs/, but it may leave its workspaces on the node; check with docker exec jenkins-controller ls /var/jenkins_home/workspace/. If they are still there, Jenkins will clean them up over time, or you can delete them with docker exec jenkins-controller sh -c 'rm -rf /var/jenkins_home/workspace/concurrent-test*', after first making sure the pattern does not affect any other job.
Solution 2. Download the jar with your browser or with the curl command from the lesson and run md5sum orders-api.jar (on macOS, md5 orders-api.jar). The value matches the fingerprint, because it is exactly the same file. Usage lists orders-api-build (the build that introduced it) and orders-api-smoke (the build that copied and checked it), thanks to the Fingerprint Artifacts option in the copy step.
Solution 3. The 5 most recent builds, #5 to #9, are kept, plus #1, which is protected by Keep this build forever. #2, #3 and #4 are deleted. The last successful build is #9, which is already among those kept. The 2 most recent builds keep their artifacts (#9 and #8; although #8 failed, with Archive artifacts only if build is successful it never archived anything), along with the protected #1. Builds #5, #6 and #7 remain in the history, but without artifacts. The practical lesson: if you want to keep the artifacts of 2 successful builds, bear in mind that failed builds also count toward the limit.
Conclusion
You can now tell the workspace, which is temporary and specific to each node, apart from the job directory on the controller, where the configuration, history and artifacts live. You know how to explore the workspace from the interface and with docker exec, you understand why it is not cleaned between builds, where the @2 directories come from and what options exist for deleting it. orders-api-build now archives its jar with fingerprints, and orders-api-smoke uses Copy Artifact to copy exactly the jar from the build that triggered it. You know the structure of builds/ on disk, how to document and protect builds, how to compare two builds and, above all, how to configure Discard old builds to protect the controller's disk. However, look at what it took to get two jobs to work together: a plugin, a permission, a copy step, a conditional trigger and two configurations scattered across the interface. In the next lesson, Understanding Jenkins Pipelines, you will see how a pipeline solves all of this by describing the entire process in a single place.
