'Zip Command not found in Jenkins

I am trying to zip a directory in a Jenkins pipeline, my code is similar to this

stages {
            stage('ZIP') {
                steps {
                    script {
                        currentBuild.displayName = "DISPLAY_NAME"
                    }



                    // Zip DIRECTORY
                    sh '''
                        cd ${WORKSPACE}
                        zip -r zip_file_name src_dir
                        '''
            }
          }
         }

I get the following error

@tmp/durable-4423e1f6/script.sh: line 3: zip: command not found

However, when I create another job with execute as a shell option for the build, zip is working fine.

I have tried using zip pipeline utillity plugin, but when I try to access the zip file it is not found.

script {
                        currentBuild.displayName = "${VERSION}"
                         zip zipFile: '${zip_file_name}',   dir: 'src_dir', overwrite: true
                    }


Solution 1:[1]

@raviTeja, I guess zip utility is missing in your jenkins agent machine. What is the OS flavour of your Jenkins agent? Lets say if you are using Linux flavours like Redhat, ubuntu.. first you need to install the zip utility in the agent machine. Then alone you can use the zip command in your script

If you are using RedHat flavour in the agent machine

  1. First install zip utility
sudo dnf install zip
  1. Then execute zip command in your pipeline script
zip -r zip_file_name src_dir

If you are using ubuntu/debian flavour for jenkins agent

  1. Install zip utility using apt
sudo apt install zip
  1. Execute zip command in your pipeline script
zip -r zip_file_name src_dir

Update:

If you are using Jenkins in the docker container, you can do something similar to the below.

I am guessing you are running ubuntu base image (identify the respective base image Linux flavour and execute the below commands)

  1. Get into docker container using exec command
docker exec -it <container> /bin/bash
  1. Update packages
apt-get -y update
  1. Install zip
apt-get install zip -y

But remember if you delete this container, you are going to loose this set-up. You might have repeat all these steps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1