Noah Sparks
Noah Sparks

Reputation: 1762

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}"

Upvotes: 3

Views: 4279

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28749

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)}"
}

Upvotes: 6

Related Questions