Jenkins Build Artifacts

A build produces files worth keeping: a JAR, a ZIP, a test report, a Docker image reference, or a log bundle. Jenkins calls these files artifacts. Without an artifact strategy, results vanish when Jenkins cleans the workspace. With one, every build leaves a downloadable record.

The Layman's View

Imagine a bakery. The kitchen counter is the workspace, where messy work happens and gets wiped clean each night. The display shelf is the artifact store, where finished loaves stay for customers. Jenkins needs you to move the loaves from counter to shelf before closing time.

Workspace Versus Artifact Storage

+----------------------+          +-------------------------+
|  WORKSPACE           |  archive |  ARTIFACT STORAGE       |
|  temporary files     | -------> |  kept with the build    |
|  source, objects,    |          |  download from job page |
|  logs                |          |                         |
+----------------------+          +-------------------------+
   wiped or reused                  survives until the build
   by the next build                is discarded
FeatureWorkspaceArchived Artifacts
LifetimeUntil cleaned or overwrittenUntil the build is deleted
AccessInside the running jobDownload from the build page
PurposeWork areaFinal output

Archiving in a Freestyle Job

  1. Open the job and select Configure.
  2. Scroll to Post-build Actions and click Add post-build action.
  3. Choose Archive the artifacts.
  4. Enter a file pattern such as target/*.jar.
  5. Save the job and run a build.

The build page now shows a list of files that visitors can download.

Archiving in a Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B clean package'
            }
        }
    }
    post {
        success {
            archiveArtifacts artifacts: 'target/*.jar',
                             fingerprint: true,
                             onlyIfSuccessful: true
        }
    }
}

Option Meanings

  • artifacts sets the file pattern. Use * for names and ** for any folder depth.
  • fingerprint records an MD5 checksum that links the file to the build that created it.
  • onlyIfSuccessful skips archiving when the build fails.

Fingerprints in Plain Words

A fingerprint works like a serial number stamped on a product. If a bug appears in production, you check the fingerprint of the deployed file and Jenkins reveals which build produced it and which commit it came from. This link saves hours during an investigation.

Sharing Files Between Stages

Different stages may run on different agents, and each agent has its own workspace. The stash and unstash steps move files across agents within a single run.

stage('Build') {
    agent { label 'linux' }
    steps {
        sh 'mvn -B package'
        stash name: 'app', includes: 'target/*.jar'
    }
}
stage('Test') {
    agent { label 'test-box' }
    steps {
        unstash 'app'
        sh 'java -jar target/app.jar --selftest'
    }
}

Stash holds files only for the current run. Archive keeps files after the run ends.

Reusing Artifacts From Another Job

The Copy Artifact plugin lets one job fetch artifacts from another. A deployment job can copy the exact JAR that a build job produced, which guarantees that the tested file and the deployed file match.

 Build Job ---archive---> app.jar
                             |
                             | copyArtifacts
                             v
                      Deploy Job ---> Server

Controlling Storage Growth

Artifacts consume disk space on the controller. A busy project with large files fills a disk quickly. Apply these controls:

  • Enable Discard old builds and keep only the last 10 or 20 builds.
  • Set a maximum number of builds that keep artifacts.
  • Store large release files in an artifact repository such as Nexus or Artifactory instead of Jenkins.
  • Avoid archiving whole folders when one file is enough.

Good Practices

Name Files With Versions

A name like app-1.4.2.jar tells readers what they hold. A name like final.jar tells them nothing.

Archive Reports Too

Test reports, coverage summaries, and log files help teammates diagnose failures without rerunning the build.

Verify Before Publishing

Check file sizes and run a quick smoke test on the artifact before pushing it to an external repository.

Troubleshooting

ProblemFix
No artifacts found matching patternPrint the workspace with ls and correct the path
Artifact missing after a failed buildRemove onlyIfSuccessful or archive inside an always block
Disk full on controllerLower the build retention count and move large files to a repository

Good artifact habits turn Jenkins from a simple runner into a reliable record of everything your team has built.