jenkinsci / file-operations-plugin

File Operations as Build Step in Jenkins
https://plugins.jenkins.io/file-operations
33 stars 31 forks source link

fileOperations - fileCopyOperation: targetLocation variable ignored when renameFiles: true #101

Closed DigiDuckArnold closed 4 months ago

DigiDuckArnold commented 5 months ago

Jenkins and plugins versions report

Environment Jenkins 2.440.2 File Operations Plugin 214.v2e7dc7f25757

What Operating System are you using (both controller, and any agents involved in the problem)?

RHEL 8, Java 17

Reproduction steps

Jenkins pipeline step with: steps{ fileOperations([fileCopyOperation( includes: "*/target/app-.war", targetLocation: "app-artifacts/", flattenFiles: true, renameFiles: true, sourceCaptureExpression: "((?app-)(?\d+\.\d+\.\d-[a-zA-Z])(?.war\Z)", targetNameExpression: "${fileName}" + "${env.releaseVersion}" + "${fileExtension}") ]) }

run in pipeline.

Expected Results

app-*.war is copied to ${WORKSPACE}/app-artifacts/app-${env.releaseVersion}.war

Actual Results

app-${env.releaseVersion}.war is copied to ${WORKSPACE}/target/app-${env.releaseVersion}.war variable targetLocation: "app-artifacts/" is ignored

Anything else?

In code: https://github.com/jenkinsci/file-operations-plugin/blob/main/src/main/java/sp/sd/fileoperations/FileCopyOperation.java


if (renameFiles) { String targetFileName = item.getRemote().replaceAll(sourceCaptureExpression, targetNameExpression); FilePath fpTF = new FilePath(fpTL, targetFileName); listener.getLogger().println("Copy from " + item.getRemote() + " to " + fpTF); item.copyTo(fpTF);

you can see that variable targetLocation is not used, which is confusing.

Either update de fix the class as mentioned above or update the documentation at https://www.jenkins.io/doc/pipeline/steps/file-operations/

targetLocation : String Destination folder location to copy the files. Base directory is workspace. Will be ignored when setting renameFiles: true.

targetNameExpression : String An expression that provides the desired target file name. This can reference variables captured in the source capture expression by using $1, $2 etc. When using renameFiles: true set the full target file path including target file name. i.a. ${WORKSPACE}/app-config/app-dev-0.0.2.yml

Are you interested in contributing a fix?

No response

DigiDuckArnold commented 5 months ago

Seems to work fine obviously now, when I strip down the file path of my includes file down via sourceCaptureExpression to only the filename. Then the targetLocation is used as expected.

env.ReleaseVersion = "3.1.2"

fileOperations([fileCopyOperation( includes: "*/target/app-.war", targetLocation: "ArtifactsFolder", flattenFiles: true, renameFiles: true, sourceCaptureExpression: "(?.+?/target/)(?app-)(?\d+\.\d+\.\d-[a-zA-Z])(?.war\Z)", targetNameExpression: '${appName}' + "${env.ReleaseVersion}" + '${fileExtension}' )])

Maybe good to add in the documentation.

MCMicS commented 4 months ago

sourceCaptureExpression says following;

Java-style regular expression that is run against the full path of each matching source file. This should be used to capture parts of the path that will be used in the target name expression to make each file name unique across all subdirectories.

So the full path is used and replaced within this. This means if your sourceCaptureExpression is simply for parts of full path it will only repalce this parts and still use the original path before the file to copy. Either use the path to replace it or the plugin should work with relative paths

jonesbusy commented 4 months ago

Fixed by https://github.com/jenkinsci/file-operations-plugin/pull/106