0
0
Jenkinsdevops~5 mins

Archiving artifacts in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run a build in Jenkins, you often create files like reports or compiled programs. Archiving artifacts means saving these files so you can look at them later or use them in other steps.
When you want to keep test reports after a build finishes to check results.
When you need to save compiled programs so they can be downloaded or used in deployment.
When you want to keep logs or output files from a build for troubleshooting.
When you want to share build outputs with your team without rerunning the build.
When you want to keep a history of important files from each build for auditing.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project...'
        sh 'echo Hello World > output.txt'
      }
    }
    stage('Archive') {
      steps {
        archiveArtifacts artifacts: 'output.txt', fingerprint: true
      }
    }
  }
}

This Jenkinsfile defines a simple pipeline with two stages.

Build stage: Creates a file named output.txt with some content.

Archive stage: Uses archiveArtifacts to save output.txt as a build artifact. The fingerprint: true option tracks the file for changes.

Commands
This command updates or creates the Jenkins job configuration from a YAML file. It prepares Jenkins to run the pipeline that archives artifacts.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline-job.yaml
Expected OutputExpected
Job 'my-pipeline-job' updated successfully
This command triggers the Jenkins job named 'my-pipeline-job' and waits for it to finish. It runs the pipeline that creates and archives the artifact.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-pipeline-job -s
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build) [Pipeline] sh + echo Hello World > output.txt [Pipeline] } [Pipeline] stage [Pipeline] { (Archive) [Pipeline] archiveArtifacts Archiving artifacts [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
-s - Waits for the build to complete before returning output
This command downloads the archived artifact file from the last successful build to verify it was saved correctly.
Terminal
curl -s http://localhost:8080/job/my-pipeline-job/lastSuccessfulBuild/artifact/output.txt
Expected OutputExpected
Hello World
Key Concept

Archiving artifacts saves important build files so you can access them later without rerunning the build.

Common Mistakes
Not specifying the correct file path in the archiveArtifacts step
Jenkins will not find the files to archive and the artifact will be missing.
Use the exact relative path or pattern matching the files created during the build.
Forgetting to add the archiveArtifacts step in the pipeline
Build files will be created but not saved, so they disappear after the build finishes.
Always add an archiveArtifacts step after files are generated to save them.
Trying to archive files before they are created
Jenkins will archive nothing because the files do not exist yet.
Place the archiveArtifacts step after the build or test steps that create the files.
Summary
Use the archiveArtifacts step in Jenkins pipelines to save build files like reports or binaries.
Run the pipeline to create files and then archive them for later access.
Verify archived files by downloading them from the Jenkins build page or using curl.