Sri
Sri

Reputation: 479

Jenkins Pipeline Utility Steps - zip zipFile

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

Upvotes: 25

Views: 114058

Answers (4)

VonC
VonC

Reputation: 1323045

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.

Upvotes: 39

MonoThreaded
MonoThreaded

Reputation: 12033

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

Upvotes: 4

geosmart
geosmart

Reputation: 656

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/
            '''
        }
    }
}

Upvotes: 2

Sri
Sri

Reputation: 479

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

Upvotes: 11

Related Questions