管道Jenkins,使用变量的正确方法是什么

时间:2017-05-12 12:41:11

标签: jenkins jenkins-pipeline

我们刚刚使用buildflow插件转移到管道,因为不建议使用buildflow&建议尽可能转移到管道上。

我们之前的构建流程如下所示:

def rioBranchName = params["rioBranch"]
def restBranchName = params["restBranch"]
def deployServer =  params["deploymentServer"]
def tagNumber = build.number
def automationBuild

build( "Restart Tomcat", deploymentServers: deployServer)

parallel (
  { build( "Rio", branch: rioBranchName, deploymentServer: deployServer, extraTasks:"multiReDeploy", gradleSwitches: "--settings-file packageSettings.gradle") },
  { build( "REST - Smoke Tests", branch: restBranchName,  deploymentServer: deployServer, extraTasks:"multiReDeploy") }, 
  { automationBuild = build( "Rio Automation", branch: rioBranchName) }
)


build("Restart Tomcat", deploymentServers: deployServer)

build("SCTM Trigger - Develop - Smoke", ,  automationTestBuildNumber: automationBuild.build.number)

我试图将其转换为管道流程。

def automationBuild

pipeline {
    agent any
    stages {
        stage('Build'){
            steps {
                parallel buildAtlas: {
                     build job: 'Rio', parameters: [string(name: 'branch', value: params["rioBranch"])]
                }, buildRest: {
                    build job: 'REST - Smoke Tests', parameters: [string(name: 'branch', params["restBranch"])]
                }, buildAutomation: {
                    automationBuild = build job: 'Rio Automation', parameters: [string(name: 'branch', value: params["rioBranch"])]
                },
                failFast: false
            }
        }
        stage('Test commit'){
            steps {
                build job: 'Restart Tomcat', parameters: [string(name: 'deploymentServers', value: params["deploymentServer"])]
                build job: 'SCTM Trigger - Develop - Smoke', parameters: [string(name: 'automationTestBuildNumber', value: automationBuild.build.number)]
            }
        }
    }
}

我一直都会收到错误。

WorkflowScript: 11: Expected a step @ line 11, column 6.
def automationBuild = build job: 'Rio Automation', parameters: [string(name: 'branch', value: params["rioBranch"])]

获取刚刚触发的构建RioAutomation版本号的新方法是什么。

1 个答案:

答案 0 :(得分:4)

您需要将非步骤赋值指令包装在script块中以使其lint/validate清除:

def rioBranchName = params["rioBranch"]
def restBranchName = params["restBranch"]
def deployServer =  params["deploymentServer"]
def tagNumber = build.number
def automationBuild

build( "Restart Tomcat", deploymentServers: deployServer)

parallel (
  { build( "Rio", branch: rioBranchName, deploymentServer: deployServer, extraTasks:"multiReDeploy", gradleSwitches: "--settings-file packageSettings.gradle") },
  { build( "REST - Smoke Tests", branch: restBranchName,  deploymentServer: deployServer, extraTasks:"multiReDeploy") }, 
  { 
    script {
      automationBuild = build( "Rio Automation", branch: rioBranchName)
    }
  }
)


build("Restart Tomcat", deploymentServers: deployServer)

build("SCTM Trigger - Develop - Smoke", ,  automationTestBuildNumber: automationBuild.build.number)

请注意script让你的运行几乎任何可以在声明性管道步骤内的脚本管道中运行的groovy代码。