1
votes

Following on from my question How to trigger parameterized build on successful build in Jenkins?

I would like the invoke a downstream project but only if a boolean parameter is set to true. Is this possible? My pipeline looks like this:

node {
    try {
        echo "ConfigFilePath: ${ConfigFilePath}"
        echo "Delete VM on Successful Build: ${DeleteOnSuccess}"

        stage('checkout') {
            deleteDir()
            git 'http://my.git.lab/repo.git'
        }   

        stage('deploy') {
                bat 'powershell -nologo -file BuildMyVM.ps1 -ConfigFilePath "%ConfigFilePath%" -Verbose'                
            }
        }
        stage('test') {
            // functional tests go here
        }    
    } 
    catch (e) {
    // exception code

  } finally {
    // finally code

  }
} //node

stage('delete') {
    if(DeleteOnSuccess)
    {
        bat 'SET /p VM_NAME=<DeleteVM.txt'
        echo "Deleting VM_NAME: %VM_NAME%"
        def job = build job: 'remove-vm', parameters: [[$class: 'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
    }
}   

I get this error on the delete stage

Required context class hudson.FilePath is missing. Perhaps you forgot to surround the code with a step that provides this, such as: node

If I wrap the above in a node, then the parameter values are lost. If I put the delete stage in the main node, then I take up two executors, which I'm trying to avoid because it will result in some deadlock conditions.

1
The error you get is about the bat you are trying to run. This requires a node (or actually a environment) as well. So depending on how you invoke your build in the first place, you need to surround that with node {...} as wel, or just put it inside the node block as well. If you only want to trigger it, and not wait for the build, you can add wait: false to the build command. - Rik
OK, if I remove the bat and the echo lines, the job in the delete stage executes whether the project parameter DeleteOnSuccess is set to true or false. Any ideas why? - Mark Allison
Is it perhaps a string. Not a boolean? - Rik
@Rik Definitely boolean i.imgur.com/ubsAvke.png - Mark Allison
I think it is a string. Try echoing DeleteONSuccess.class in your pipeline script - Rik

1 Answers

2
votes

The problem is that the running of a script actually needs a node to run on, so in your case the cause of the error is that you try to run a bat command outside of a node context

node {
...
}
stage('delete') {
    if(DeleteOnSuccess)
    {
        bat 'SET /p VM_NAME=<DeleteVM.txt' // <- this is actually causing the error
        echo "Deleting VM_NAME: %VM_NAME%"
        def job = build job: 'remove-vm', parameters: [[$class:    'StringParameterValue', name: 'VM_NAME', value: '${VM_NAME}']]
    }
}   

You can fix this by wrapping this part also inside a node by either putting it inside the first node or add a new one, depending on what you want

Besides that, if the DeleteOnSuccess variable is a build parameter, it will be a string. I am not sure, but I think this is because it is injected as an environment variable, which are also strings (even if it is of type BooleanParameter. I guess that is only a UI thing so it will show up as checkbox). You can check that by echoing DeleteOnSuccess.class. This will tell you its class

 if(DeleteOnSuccess) { ... }

will always run the conditional block. You can fix this by either converting it to a bool using the toBoolean() extension method, or checking it against the the string true: DeleteOnSuccess == "true", like you did. The extension method has the advantage that it will also allow values "1" and "True" as true