Groovy子字符串Jenkinsfile

问题描述 投票:0回答:2

我有一个关于常规子字符串的问题。我在詹金斯(Jenkins)中有一个环境变量,想要将其余的变量从第4个字符开始保存到新变量中。

例如myvar =“ abcdefghi”

我需要4个字符之后的所有字符,最多N个,所以:fghi

我的Jenkinsfile当前看起来像我想使用子字符串,不幸的是,代码无法正常工作,我在变量中的某个地方有错误?

pipeline{
    agent {
        label 'windows'
    }
    options {
        buildDiscarder(logRotator(numToKeepStr: '15'))
    }
    environment {
        get_cd_tag = """${bat(
            script: '@git describe --tags --abbrev=0', 
            returnStdout: true
            )}"""
        type = "${get_tag[0..1]}"
        count = "${get_tag[3,4]}"
        branch = "${GIT_BRANCH}"
        temppath = 'E:\\Temp\\'
    }
    stages {
        stage('info'){
            steps{
              script {
                def ver = "${get_tag.substring(4)}"
                def version = ver.split("_")
              }
            }
        }   

错误:1。

没有这样的属性:类的版本:groovy.lang.Binding

jenkins groovy jenkins-pipeline pipeline
2个回答
0
投票

要获得最后4个字符,您可以使用常规范围访问:

def myvar = "abcdefghi"
def last4 = myvar[-4..-1]

结果fghi


0
投票

如果这是一个完整的文件-您确实在3个地方使用了get_tag变量,但不要在任何地方声明它

© www.soinside.com 2019 - 2024. All rights reserved.