jenkins

How to use fileCopyOperation in jenkins groovy to copy 3 files with the same name from 3 different directories, to 1 directory with 3 different names


At the end of a Jenkins pipeline, I am trying to copy three files with the same name, in three different directories, into one directory, obviously renaming the files to avoid a name clash. The input structure is

repo/
     build-productOne/Release/program
     build-productTwo/Release/program
     build-productThree/Release/program

and the output structure should be

repo/
     output/
            productOne-program
            productTwo-program
            productThree-program

To achieve this, I created the following pipeline entry, after reading this page: https://www.jenkins.io/doc/pipeline/steps/file-operations/

stage('Build') {
    steps {
        sh """
        ...
        make -j -O -C ${WORKSPACE} Release
        """
        script {
            def version = readJSON file: "${WORKSPACE}/some-
            fileOperations(
                [
                    fileCopyOperations(
                        includes: 'build-*/Release/program',
                        flattenFiles: true,
                        renameFiles: true,
                        sourceCaptureExpression: 'build-(.*)/Release/program$',
                        targetNameExpression: '${env.OUTPUT}/blah_$1_program-${version}'
                    )
                ]
            )
        }
    }
}

I was expecting the above code to work, but it fails with the following error message:

08:53:43  File Copy Operation:
08:53:43  null
08:53:43  FATAL: null

Can any kind soul throw any light on this issue?


Solution

  • You need to set targetLocation to your output folder and use targetNameExpression only for the filename. Also make sure your variables like ${version} are defined. For example: groovy Copy Edit fileCopyOperations( includes: 'build-*/Release/program', flattenFiles: true, renameFiles: true, sourceCaptureExpression: 'build-(.*)/Release/program$', targetLocation: "${env.WORKSPACE}/output", targetNameExpression: "\$1-program-${version}" ) Make sure version is defined in your script before this step. The FATAL: null error often happens if these are missing or misconfigured.