Jenkins

Jenkinsfile ArchiveArtifacts

Jenkins build artifacts refer to the files generated by a specific Jenkins build and saved in one location so that they can be archived and shared with other team members or deployed to testing and production environments.

Some examples of build artifacts include compiled executables, libraries, and package files, as well as documentation, test results, and other files produced as part of the build process. Build artifacts are mainly stored in a repository or artifact manager, such as Nexus or Artifactory, where they can be accessed and used by other build and deployment processes.

Artifact archiving in Jenkins can be defined as archiving build artifacts in a repository or artifact manager, facilitating access and use by other build and deployment processes.

You can specify the artifact files you wish to archive during the job configuration process. You also define where you wish to store the archived artifacts.

Once the Jenkins job is complete, the server will save the saved artifacts in the specified location where they can be accessed and used as required.

There are several benefits to artifact archiving in Jenkins. For example,  by storing build artifacts in a central repository, you can ensure that they are available to all team members who need them and track the history of artifacts over time.

Additionally, artifact archiving can help manage and deploy artifacts to testing and production environments, as you can use the repository to store different versions of artifacts and quickly retrieve a specific version.

This tutorial will cover how we can work and use the archiveArtifacts block in the Jenkins job.

Jenkins archiveArtifacts

The following shows the syntax of the Jenkins archiveArtifacts block:

steps {
      archiveArtifacts artifacts: options
}

The block accepts various options as shown:

  1. Artifacts – this allows you to specify the artifacts you wish to archive. This is a string type; you define it as a literal or wildcard. Keep in mind that this follows the Ant fileset wildcard format. Hence, special characters such as commas as treated as a separator. You can check the documentation to learn more.
  2. allowEmptyArchive – This Boolean option determines whether Jenkins will create the Archive even if no artifacts are available. By default, this value is set to false, meaning the build will fail if no artifacts are available.
  3. caseSensitive – A Boolean option specifies whether the artifact archiver is case-sensitive. By default, this value is set to true. Hence, the file file.tar and FILE.tar are different file names.
  4. defaultExcludes – This is another Boolean parameter that allows you to exclude default artifacts.
  5. Excludes – This parameter is a string type that enables you to define the files you wish to exclude from the Archive. You can also use wildcard characters as specified by the Ant fileset.
  6. Fingerprint – If set to True, Jenkins will create a fingerprint for each artifact which can be used to distinguish the artifacts over time.
  7. followSymlinks – This allows the archiver to follow symbolic links if available.
  8. onlyIfSuccessful – This allows you to specify that artifacts should only be archived if the build is successful.

Now that we have broken down the options of the archiveArtifacts block in Jenkins, let us explore how we can use it.

Example Usage

The following example shows how to use the archiveArtifacts block to create an archive of executable files from a Rust project build.

pipeline {
    agent any
   
    stages {
        stage('Build') {
            steps {
                sh 'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y'
                sh 'echo "export PATH=$HOME/.cargo/bin:$PATH" >> $BASH_ENV'
                // Build the program
                sh 'cargo build --release'
            }
        }
        stage('Archive Artifacts') {
            steps {
                // Archive the executable
                archiveArtifacts artifacts: 'target/release/hello_world', fingerprint: true
            }
        }
    }

}

The pipeline above has two stages: a “Build” stage and an “Archive Artifacts” stage.

The “Build” stage installs Rust, adds it to the PATH, and then builds the program using the cargo build command.

The “Archive Artifacts” stage then archives the executable file produced by the build process (target/release/hello_world) as a build artifact.

We also set the fingerprint option to true, allowing Jenkins to create a fingerprint for the artifact.

This pipeline assumes the Rust project and the Cargo.toml files are located in the root of the Jenkins workspace.

Conclusion

This tutorial taught us about the Jenkins archiveArtifact block, the various options we can use, and more. We also provided a practical example demonstrating how we can use the archiveArtifact block to create an archive of binary artifacts from the build.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list