jfrog / jenkins-jfrog-plugin

Easy integration between Jenkins and the JFrog Platform.
https://github.com/jfrog/jenkins-jfrog-plugin
Apache License 2.0
39 stars 17 forks source link

Plugin Usage In Scripted Pipelines #14

Closed thomas-galla closed 1 year ago

thomas-galla commented 1 year ago

How can we help?

How would we use the jenkins-jfrog-plugin in a Scripted Pipeline?

The tool name: 'jfrog-cli', type: 'jfrog' was created using Snippet Generator.

See this pipeline:

node {

    // FYI: The full path of jf
    //    /var/jenkins_home/tools/io.jenkins.plugins.jfrog.JfrogInstallation/jfrog-cli/jf

    tool name: 'jfrog-cli', type: 'jfrog'

    stage('Use jf step in scripted pipeline') {

            jf 'rt ping'
    }

}

Output is

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/project_main
[Pipeline] {
[Pipeline] tool
[Pipeline] stage
[Pipeline] { (Use jf step in scripted pipeline)
[Pipeline] jf
[project_main] $ null/jf c add artifactory --user= ******** --url=https://artifactory:8081 --artifactory-url=http://artifactory:8081/artifactory --distribution-url=http://artifactory:8081/distribution --xray-url=http://xray:8082/xray --interactive=false --overwrite=true
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.IOException: error=2, No such file or directory
    at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
    at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
    at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
Caused: java.io.IOException: Cannot run program "null/jf" (in directory "/var/jenkins_home/workspace/project_main"): error=2, No such file or directory
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
    at hudson.Proc$LocalProc.<init>(Proc.java:254)
    at hudson.Proc$LocalProc.<init>(Proc.java:223)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:997)
    at hudson.Launcher$ProcStarter.start(Launcher.java:509)
    at hudson.Launcher$ProcStarter.join(Launcher.java:520)
    at io.jenkins.plugins.jfrog.JfStep.configAllServers(JfStep.java:164)
    at io.jenkins.plugins.jfrog.JfStep.setupJFrogEnvironment(JfStep.java:128)
    at io.jenkins.plugins.jfrog.JfStep.perform(JfStep.java:87)
Caused: java.lang.RuntimeException: Couldn't execute 'jf' command. IOException: error=2, No such file or directory
    at io.jenkins.plugins.jfrog.JfStep.perform(JfStep.java:95)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:101)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:71)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

So the path is not being set in the tool step. It appears to be a problem reported also for other tools when using Scripted Pipelines but is there any other solution than to wrap the jf call into some sh() call?

chrismathis commented 1 year ago

For anyone else having this problem - setting the JFROG_BINARY_PATH seems to be a workaround:

node {
    withEnv(["JFROG_BINARY_PATH=${tool 'jfrog-cli'}"]){
        jf 'rt bpr mybuild my-staging'
    }
}

If no JFROG_BINARY_PATH ist set the binary path should not be null/jf or null\jf.exe though.

thomas-galla commented 1 year ago

@chrismathis Thank you for providing this workaround. I can confirm that this approach does the trick.

yahavi commented 1 year ago

Thank you @chrismathis! We'll add it to the documentation.