23

I am trying to zip the folders which are created as output of my jenkins pipeline job using pipeline script. By googling i came to know the Jenkins

Pipeline Utility Steps - zip zipFile

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#code-zip-code-create-zip-file to zip folders/files but could not get exact pipeline syntax to zip.

In my job workspace, I have a folder by name 'Test' which has 2 sub folders as 'Test1', 'Test2'. Each sub folder will have .dll files. So, I would like to zip entire 'Test' folder with all subfolder.

node(Jenkinks_1)
{
    echo "ZIP"
    zip zipFile: 'Test.zip', dir:'C:\\workspace\\Build_Sample\\Test'
    echo "END - ZIP"
}

Below are the Console Output from Jenkins:

Started by user XXXXX
[Pipeline] node
Running on Jenkinks_1 in C:\workspace\Build_Sample
[Pipeline] {
[Pipeline] echo
ZIP
[Pipeline] echo
END - ZIP
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Looking for some guidance to zip the folders using pipeline syntax. Appreciate your inputs.

I wanted to zip some files as output of my jenkins pipeline job

mkobit
  • 43,979
  • 12
  • 156
  • 150
Sri
  • 459
  • 2
  • 9
  • 20

4 Answers4

36

First, try the same operation in stages and step, as in here:

pipeline {
    agent any
    stages {
        stage ('push artifact') {
            steps {
                sh 'mkdir archive'
                sh 'echo test > archive/test.txt'
                zip zipFile: 'test.zip', archive: false, dir: 'archive'
                archiveArtifacts artifacts: 'test.zip', fingerprint: true
            }
        }
        ...
    }

It uses archiveArtifacts to record the result.

If using an absolute path does now work, try a relative one ('..')

As seen by the OP Sri, zip zipFile is part of, and requires the JENKINS Pipeline Utility Steps Plugin.
See "Implemented Steps".


Regarding the syntax to be used for multi-criteria file selection, NicolasW notes in the comments that the documentation is vague: "use glob ant-style syntax"...
He got it to work though, with a basic coma separated syntax.
E.g.

zip zipFile: 'test.zip', archive: false, glob: 'config-/**/,scripts/**/*.*

But, as noted by Tanvir in the comments, issue 44078 means you need to replace zip by:

                 script{ zip zipFile: 'test.zip', archive: false, dir: 'archive' }

Meaning you need to use a script block.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the reply. I am looking to use Pipeline Utility Steps - zip zipFile using Pipeline Script instead of Declarative syntax. I did try with the above script.. but the build failed with following error messages: – Sri Jan 21 '18 at 05:26
  • 1
    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 8: Invalid parameter "zipFile", did you mean "label"? @ line 8, column 21. zip zipFile: 'test.zip', archive: false, dir: 'archive' ^ WorkflowScript: 8: Invalid parameter "archive", did you mean "url"? @ line 8, column 42. zip zipFile: 'test.zip', archive: f ^ WorkflowScript: 8: Invalid parameter "dir", did you mean "url"? @ line 8, column 58. : 'test.zip', archive: false, dir: 'arch – Sri Jan 21 '18 at 05:35
  • @Sri What version of Jenkins and Jenkins pipeline do you have? – VonC Jan 21 '18 at 05:39
  • I using Jenkins version - v2.73.2 and Jenkins pipeline - 2.5 – Sri Jan 21 '18 at 14:51
  • 3
    Why do you use `archiveArtifacts`? You can archive the created zipfile directly when using `archive: true` in the zip step. – IVleafclover Mar 19 '20 at 12:34
  • 1
    @IVleafclover I believe (2 years later, it is a bit fuzzy), this is to make the artifact available for download, as mentioned in https://issues.jenkins-ci.org/browse/JENKINS-50130. zipFile does just a zip, archiveArtifacts is the Jenkins’s built-in support for storing "artifacts", which is important to make them available as a result of a pipeline. – VonC Mar 19 '20 at 13:06
  • 1
    @VonC Thanks for the answer. `zip zipFile: 'test.zip', archive: true, dir: 'archive'` will zip and archive the zip file in Jenkins artifacts. That way it is not necessary to use the `archiveArtifacts` command. Nevermind your original answer is working fine and two years after your original answer my comment tend to be irrelevant. – IVleafclover Mar 20 '20 at 10:05
  • Currently in declarative pipeline, `zip` command does not work as documentation. It fails giving error message **Invalid parameter "zipFile"** . To make it work change the code to `script{ zip zipFile: 'test.zip', archive: false, dir: 'archive' } ` . This problem is addressed in [https://issues.jenkins.io/browse/JENKINS-44078](https://issues.jenkins.io/browse/JENKINS-44078) – Tanvir Apr 04 '21 at 19:21
  • @Tanvir Thank you. I have edited the answer to include your comment for more visibility. – VonC Apr 04 '21 at 20:09
11

Was able to Zip after installing the Pipeline Utility Steps plugin.

Sri
  • 459
  • 2
  • 9
  • 20
4

I came across this because zip was ... not installed on the host.
Reminder to self : If you need zip, install it first.

sudo yum install zip

MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
1

you can just use sh (jenkins server need install zip);

 sh '''
            zip -r  algo.zip algo
 '''

pipeline script like this

node {
    stage('Clean'){
        cleanWs()
    }
    stage('Checkout') {
       git branch: 'develop', url: 'ssh://user@ip:29418/prj.git'
    }
    stage('Zip') {
        dir('algo-python') {
            sh '''
            zip -r  algo.zip algo
            '''
       }
    }
    stage('Upload zip'){
        dir('algo-python') {
            sh '''
                source /etc/profile
                export HADOOP_USER_NAME=dev
                hdfs dfs -put -f algo.zip /user/dev/zipfile/
            '''
        }
    }
}
geosmart
  • 518
  • 4
  • 15