Skip to main content

Managing Build Artifacts

1. Archiving Build Artifacts

Property Description Default Value
archiveArtifacts Archives build artifacts from the workspace to the build directory None
artifacts Pattern to match files for archiving (e.g., **/*.jar) None
allowEmptyArchive Whether to allow archiving of empty artifact sets false
onlyIfSuccessful Whether to archive artifacts only if the build is successful false

Example: Archiving JAR files

pipeline {
    stages {
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }
        stage('Archive Artifacts') {
            steps {
                archiveArtifacts artifacts: '**/*.jar', allowEmptyArchive: true
            }
        }
    }
}

Example: Archiving with specific pattern

pipeline {
    stages {
        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
        stage('Archive Artifacts') {
            steps {
                archiveArtifacts artifacts: 'dist/**/*', onlyIfSuccessful: true
            }
        }
    }
}
Note: Artifacts are stored in the Jenkins build directory and can be accessed via the UI or API.

2. Using Artifact Patterns

Pattern Description Usage
**artifact** Defines the artifact to be archived archiveArtifacts artifacts: 'build/libs/*.jar'
**includes** Includes files matching the pattern archiveArtifacts artifacts: '**/*.jar'
**excludes** Excludes files matching the pattern archiveArtifacts artifacts: '**/*.jar', excludes: 'test-*.jar'

Example: Archiving JAR files

pipeline {
    stages {
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }
        stage('Archive Artifacts') {
            steps {
                archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
            }
        }
    }
}
Note: Artifact patterns support Ant-style wildcards such as * and **.

3. Configuring Artifact Retention

Property Values Description
artifactArchiver.artifacts **/*.jar Pattern to match artifacts for archiving
artifactArchiver.latestOnly false Whether to keep only the latest build's artifacts
artifactArchiver.allowEmpty false Whether to archive even if no artifacts match
artifactArchiver.compress false Whether to compress artifacts in the archive
artifactArchiver.fingerprint false Whether to fingerprint artifacts for tracking

Example: Archiving JAR files with retention policy

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: '**/*.jar', allowEmptyArchive: true
        }
    }
}
Note: Artifact retention is managed by Jenkins' built-in ArtifactArchiver plugin. You can configure how many builds to retain using the Build Discarder in project settings.
Warning: Large artifacts can consume significant disk space. Consider setting up a retention policy to prevent indefinite storage growth.

4. Publishing Artifacts to Artifactory

Property Value Description
artifactoryUrl http://localhost:8081/artifactory URL of the Artifactory server
repositoryKey my-repo Repository key to publish artifacts to
artifactPattern org/example/[artifact]-[revision].[ext] Pattern to define artifact paths

Example: Publishing to Artifactory

artifactory {
    contextUrl = 'http://localhost:8081/artifactory'
    repository {
        repoKey = 'my-repo'
        username = 'admin'
        password = 'password'
    }
}

publishArtifacts {
    pattern = 'org/example/[artifact]-[revision].[ext]'
}
Note: Ensure the Jenkins Artifactory plugin is installed and configured correctly.
Warning: Artifactory credentials should be stored securely using Jenkins credentials store.

5. Publishing to Nexus Repository

Property Value Description
nexusUrl http://localhost:8081 URL of the Nexus repository manager
repositoryId releases ID of the repository to publish to
artifactId my-app Artifact identifier for the published artifact
groupId com.example Group identifier for the published artifact
version 1.0.0 Version of the artifact to publish

Example: Publishing to Nexus

publishToNexus(
    nexusUrl: 'http://localhost:8081',
    repositoryId: 'releases',
    artifactId: 'my-app',
    groupId: 'com.example',
    version: '1.0.0'
)
Note: Ensure the Jenkins user has appropriate permissions in Nexus to upload artifacts.
Warning: Make sure the artifact version does not already exist in the repository to avoid overwriting.

6. Using Artifact Fingerprinting

Artifact fingerprinting in Jenkins allows you to track and manage build artifacts by their content hash, ensuring that artifacts are uniquely identified based on their actual content rather than just their filename or build number.

Property Description Default Value
fingerprint Enables fingerprinting for the artifact false
fileName Name of the artifact to fingerprint None
target Path to the artifact file or directory None

Example: Fingerprinting an Artifact

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
            }
        }
    }
}
Note: Fingerprinting is particularly useful in multi-job pipelines where artifacts from one job are used as inputs in another. It ensures that the same artifact content is reused, avoiding unnecessary rebuilds.

Key Benefits of Artifact Fingerprinting

  • Ensures artifact uniqueness based on content
  • Enables reuse of artifacts across jobs
  • Reduces redundant builds by identifying identical content

7. Copying Artifacts Between Jobs

Property Values Description
artifactArchiver artifacts: 'build/libs/*.jar' Defines which files to archive from the build
allowEmpty false Whether to archive even if no files match the pattern
onlyIfSuccessful true Only archive artifacts if the build is successful
fingerprint false Whether to fingerprint the archived artifacts

Example: Archiving Artifacts

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

Example: Copying Artifacts to Another Job

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }
    }
    post {
        success {
            archiveArtifacts artifacts: 'target/*.jar', onlyIfSuccessful: true
        }
    }
}
Note: Artifacts are copied to the artifacts directory of the job's workspace in the Jenkins master.
Warning: If the artifact path is incorrect, no artifacts will be archived and no error will be thrown.

8. Using Artifact Manager

Property Values Description
jenkins.artifactManager.class hudson.util.io.ArtifactManager Class used to manage artifacts in Jenkins
artifactManager.storage local, s3, gcs Storage backend for artifacts (local, S3, GCS)
artifactManager.s3.bucket my-jenkins-bucket S3 bucket name for artifact storage
artifactManager.gcs.bucket my-jenkins-gcs-bucket GCS bucket name for artifact storage

Example: Configure Artifact Manager for S3

import jenkins.model.Jenkins
import hudson.util.io.ArtifactManager

def jenkins = Jenkins.instance
jenkins.setArtifactManager(new ArtifactManager(
    "s3",
    "my-jenkins-bucket",
    "us-east-1"
))
Note: Artifact Manager supports local, S3, and GCS storage backends. Ensure proper credentials are configured for cloud storage.

9. Cleaning Old Artifacts

Jenkins provides several mechanisms to clean up old build artifacts to manage disk space and maintain a clean workspace.

Method Description Configuration
Log Rotation Automatically deletes old builds based on build number or time. buildsToKeep and daysToKeep in logRotator
Workspace Cleanup Deletes the workspace directory after a build completes. Enabled via cleanWs in pipeline or Post-build Actions
Artifact Cleanup Removes old artifacts from the build directory. Configurable in Post-build Actions or via script

Example: Log Rotation Configuration

pipeline {
    agent any
    post {
        always {
            logRotator {
                numToKeep = 5
                daysToKeep = 7
            }
        }
    }
}

Example: Clean Workspace in Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                cleanWs()
                sh 'make'
            }
        }
    }
}
Note: Cleaning old artifacts is crucial for maintaining performance and avoiding disk space issues in long-running Jenkins instances.

10. Accessing Artifacts via API

Endpoint Description Parameters Notes
/job/<jobName>/lastBuild/artifact/ Access artifacts from the last build of a job jobName Requires READ permission for the job
/job/<jobName>/<buildNumber>/artifact/ Access artifacts from a specific build of a job jobName, buildNumber Requires READ permission for the job and build
/job/<jobName>/lastBuild/artifact/* Access all artifacts from the last build of a job jobName Allows wildcard matching for artifact paths

Example: Fetching Artifact via API

curl -u "username:token" \
  "http://jenkins.example.com/job/MyJob/lastBuild/artifact/target/myapp.jar"
Note: The artifact endpoint returns the actual file content. If the artifact is not found, Jenkins returns a 404 error.

Was this article helpful?