Jenkins pipelines often fail to archive build artifacts because the archiveArtifacts step is either misconfigured or not present, leading to lost build outputs and difficulty in debugging or redeploying.
Here’s a breakdown of common causes and their solutions:
1. Missing archiveArtifacts Step
-
Diagnosis: Review your
Jenkinsfile. ThearchiveArtifactsstep is a declarative directive within yourpostsection or a specificstage. If it’s absent, Jenkins has no instruction to save artifacts. -
Fix: Add the
archiveArtifactsstep to yourJenkinsfile. For example, to archive all files in thetarget/directory:pipeline { agent any stages { stage('Build') { steps { // ... your build commands ... } } } post { success { archiveArtifacts artifacts: 'target/**' } } } -
Why it works: This explicitly tells Jenkins to collect files matching the specified pattern after the pipeline run completes successfully.
2. Incorrect artifacts Pattern
-
Diagnosis: The
artifactsparameter uses Ant-style glob patterns. If the pattern is too broad, too narrow, or contains typos, it won’t match the files you intend to archive. Check the Jenkins console output for messages indicating no files were found for archiving. -
Fix: Adjust the
artifactspattern to precisely match your build outputs. For instance, if your build produces JAR files in abuild/libs/directory:post { success { archiveArtifacts artifacts: 'build/libs/*.jar' } }If you need to archive multiple types of files, use a comma-separated list or a more general pattern:
post { success { archiveArtifacts artifacts: 'build/libs/*.jar, build/reports/**/*.html' } } -
Why it works: The
artifactsparameter is a filter; a correct pattern ensures that only the intended files are selected for archiving, preventing unnecessary disk usage and ensuring essential outputs are saved.
3. Archiving in the Wrong Pipeline Section
-
Diagnosis: The
archiveArtifactsstep is typically placed in thepostsection, specifically withinsuccess,failure, oralwaysblocks. If it’s placed directly in astagethat doesn’t always run or if the stage fails before the archiving step, artifacts won’t be saved. -
Fix: Ensure
archiveArtifactsis within thepostsection of yourJenkinsfilefor reliable execution.pipeline { agent any stages { stage('Build') { steps { sh './gradlew assemble' } } stage('Test') { steps { sh './gradlew test' } } } post { success { archiveArtifacts artifacts: 'build/libs/*.jar' } failure { archiveArtifacts artifacts: 'build/reports/tests/test/**' // Archive test reports on failure } } } -
Why it works: The
postsection is designed for actions that occur after the stages have executed, guaranteeing that archiving happens regardless of individual stage outcomes (depending on thepostcondition used).
4. Insufficient Disk Space on Jenkins Controller/Agent
-
Diagnosis: If the Jenkins controller or the agent where the build runs runs out of disk space, it cannot write the archived artifacts. Check the disk usage on your Jenkins nodes using
df -hon Linux/macOS or Disk Management on Windows. -
Fix: Free up disk space on the relevant Jenkins node or increase its storage capacity. For example, on Linux, you might remove old build artifacts or logs:
# Find and remove old log files larger than 1GB find /var/log/jenkins/ -type f -size +1G -deleteThen, re-run the pipeline.
-
Why it works: Archiving requires physical storage. Ensuring sufficient free space allows Jenkins to persist the build artifacts.
5. Incorrect allowEmpty Setting
-
Diagnosis: By default, if the
archiveArtifactsstep finds no files matching the pattern, the build might be marked as unstable or fail, depending on Jenkins configuration. If you intend for artifacts to be optional, this setting is crucial. -
Fix: Set
allowEmpty: trueif it’s acceptable for no artifacts to be archived for a particular build.post { success { archiveArtifacts artifacts: 'nonexistent/path/*.log', allowEmpty: true } } -
Why it works: This flag tells Jenkins that it’s okay if the artifact pattern matches zero files, preventing the build from being incorrectly flagged as failed or unstable due to the absence of artifacts.
6. Network Issues or Agent Disconnection
- Diagnosis: If the Jenkins agent is disconnected from the controller during or after the build, or if there are network interruptions, the archived artifacts might not be transferred to the controller’s artifact storage. Check Jenkins build logs for connection errors or agent status.
- Fix: Ensure stable network connectivity between your Jenkins agents and the controller. If using distributed builds, verify the agent’s connection status in "Manage Jenkins" -> "Nodes". For transient network issues, a simple retry of the build might suffice.
- Why it works: Artifacts are typically stored on the Jenkins controller after being collected from the agent. A broken communication channel prevents this transfer.
After ensuring all artifacts are correctly archived, the next common challenge is managing the sheer volume of stored artifacts, which can lead to disk space exhaustion on the Jenkins controller.