Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkinsfile setting environment variable with substring extraction

Having trouble attempting to set a environment variable that uses substring extraction of another environment variable.

pipeline {
    agent any
    environment {           
        NODE_BASE_NAME = "ui-node-${GIT_COMMIT:0:6}"
    }
    stages {
        stage ("test") {
            steps {
                echo "${NODE_BASE_NAME}"
            }
        }
    }
}

Results in

WorkflowScript: 4: expecting '}', found ':' @ line 4, column 49.
   NAME = "ui-node-${GIT_COMMIT:0:6}"
like image 702
Noah Sparks Avatar asked Nov 25 '25 02:11

Noah Sparks


1 Answers

The intrinsic method for doing a substring in Groovy is String substring(int beginIndex, int endIndex). Therefore, the correct syntax for interpolating your string assigned to NODE_BASE_NAME with a GIT_COMMIT substring is:

environment {           
  NODE_BASE_NAME = "ui-node-${GIT_COMMIT.substring(0, 6)}"
}
like image 118
Matt Schuchard Avatar answered Nov 27 '25 22:11

Matt Schuchard