managerger
managerger

Reputation: 790

Jenkinsfile - get parent path in environment block

I need to get the parent directory of the current WORKSPACE and set the value to the environment variable. I tried the following:

pipeline {

    agent {
        label 'test'
    }
    
    environment{
        COMMON = "${Paths.get(env.WORKSPACE).getParent()}"
    }
    
    stages { ... }

But when I run it I get the following exception java.lang.IllegalArgumentException: One or more variables have some issues with their values: COMMON.

I have a solution as a last resort -> run powershell/bash script as the first stage and calculate the path there. But in this case, the step will be displayed on UI, which is undesired. Any thoughts about how to calculate the parent directory value directly in the environment block?

Upvotes: 0

Views: 3845

Answers (1)

Steve
Steve

Reputation: 71

Af far as I know and as you stated yourself, the only way to set an environment variable dynamically is by using bat/sh/powershell (see here). To assign the return value to a variable you want to use returnStdout which will suppress output in the console log. e.g. COMMON = bat(script:"dir", returnStdout: true)

Upvotes: 1

Related Questions