buildingTag()始终返回false

时间:2019-05-07 20:55:11

标签: jenkins jenkins-pipeline

每当我尝试使用buildingTag()创建条件阶段时,即使当前提交是一个标记,也总是会跳过该阶段。这是我的Jenkinsfile:

pipeline {
  agent {
    docker {
      image 'node:10'
    }

  }
  stages {
    stage('Build') {
      steps {
        sh 'yarn install'
        sh 'node scripts/build.js'
      }
    }
    stage('Lint') {
      steps {
        sh 'yarn lint'
      }
    }
    stage('Deploy') {
      when {
        buildingTag()
      }
      environment {
      }
      steps {
        sh 'node scripts/deploy.js'
        sh 'node scripts/publish.js'
      }
    }
  }
}

3 个答案:

答案 0 :(得分:0)

可能是由于此错误造成的:

https://issues.jenkins-ci.org/browse/JENKINS-55987

解决方法是:

            when {
                expression {
                    return !isVersionTag(readCurrentTag())           
                }
            }

具有:

def boolean isVersionTag(String tag) {
    echo "checking version tag $tag"

    if (tag == null) {
        return false
    }

    // use your preferred pattern
    def tagMatcher = tag =~ /\d+\.\d+\.\d+/

    return tagMatcher.matches()
}

// workaround https://issues.jenkins-ci.org/browse/JENKINS-55987
def String readCurrentTag() {

    return sh(returnStdout: true, script: "git describe --tags").trim()           
}


答案 1 :(得分:0)

我一直在使用 soru 的解决方案,但是我在构建带有标记的分支时遇到了问题,所以我尝试了这个,它似乎有效:

def boolean isVersionTag(String tag) {
    echo "checking version tag $tag"

    if (tag == null) {
        return false
    }

    // use your preferred pattern
    def tagMatcher = tag =~ /\d+\.\d+\.\d+/

    return tagMatcher.matches()
}

def String readCurrentTag() {
    return sh(returnStdout: true, script: 'echo ${TAG_NAME}').trim()
}

答案 2 :(得分:0)

buildingTag() 要求设置 TAG_NAME 环境变量。

这不会在简单(非多分支)管道中自动设置。

pipeline {
    agent any
    environment {
        // To get the tag like shown soru's answer:
        // TAG_NAME = sh(returnStdout: true, script: "git describe --tags").trim()

        // In my case I already have a tag saved as an environment variable:
        // gitlabBranch=refs/tags/tagname
        TAG_NAME = "${env.gitlabBranch.split('/')[2]}"
    }
    stages {
        stage('buildingTag') {
            when { buildingTag() }
            steps {
                echo 'buildingTag works here.'
            }
        }
    }
}