从参数派生的詹金斯声明性管道设置变量

时间:2019-05-07 18:10:27

标签: jenkins jenkins-pipeline jenkins-groovy

我在Jenkinsfile中使用了声明性管道,但我想从参数派生一些变量。 例如:

parameters {
   choice(name: 'Platform',choices: ['Debian9', 'CentOS7'], description: 'Target OS platform', )
}

我想添加一个块,例如:

script {
  switch(param.Platform) {
     case "Centos7":
         def DockerFile = 'src/main/docker/Jenkins-Centos.Dockerfile'
         def PackageType = 'RPM'
         def PackageSuffix = '.rpm'
         break
     case "Debian9":
     default:
         def DockerFile = 'src/main/docker/Jenkins-Debian.Dockerfile'
         def PackageType = 'DEB'
         def PackageSuffix = '.deb'
         break
  }
}

这样我可以在管道的其他地方使用变量。例如:

agent { 
   dockerfile {
       filename "$DockerFile"
   }
}

等等。

但是脚本在参数,环境和代理部分中是非法的。 它只能逐步使用。

我需要在agent块中使用参数,并且我想避免在不同步骤使用变量的地方重复我自己。

是否有理智的方法来实现这一目标?我的偏好依次为:

  • 声明性管道
  • 脚本化管道(不好)
  • 通过Jenkins UI的插件(效果不佳)

这里共享库可能是合适的,无论是否实际共享。

目的是通过创建参数化的内部版本并为每个配置调用带有红色/蓝色状态灯的不同参数集来支持多配置项目。 可能是我假设了“老式”设计。在这种情况下,可接受的答案将解释创建多配置多分支管道的现代最佳实践。类似于:https://support.cloudbees.com/hc/en-us/articles/115000088431-Create-a-Matrix-like-flow-with-PipelineJenkins Pipeline Multiconfiguration Project

有关最佳做法的较不具体讨论,另请参见Multiconfiguration / matrix build pipeline in Jenkins

4 个答案:

答案 0 :(得分:1)

以前从未真正使用过Jenkins声明性管道,但我认为您引用参数的方式不正确?

我认为可能是:${params.Platform}params.Platform而不是param

那么类似下面的内容?

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
                script {
                    switch(${params.Platform}) {
                        ...
                    }
                }
            }
        }
    }
}

正如我所说,从来没有真正使用过它,所以不是100%。我只是在查看文档中用于参数的语法:https://jenkins.io/doc/book/pipeline/syntax/#parameters

答案 1 :(得分:0)

我认为解决问题的关键是声明变量。如果要从其他阶段访问变量,请不要使用def

以下是解决您的问题的示例:

pipeline{
    agent none
    parameters {
        choice(name: 'Platform',choices: ['Debian9', 'CentOS7'], description: 'Target OS platform', )
    }
    stages{
        stage('Setting stage'){
            agent any
            steps {
                script {
                    switch(params.Platform){
                        case 'CentOS7' :
                            DockerFile = 'src/main/docker/Jenkins-Centos.Dockerfile'
                            PackageType = 'RPM'
                            PackageSuffix = '.rpm'
                            break
                        case 'Debian9' :
                            DockerFile = 'src/main/docker/Jenkins-Debian.Dockerfile'
                            PackageType = 'DEB'
                            PackageSuffix = '.deb'
                            break
                    }
                }
            }
        }
        stage('Echo stage'){
            agent { 
                dockerfile {
                    filename "$DockerFile"
                }
            }
            steps{
                echo PackageType
                echo PackageSuffix
            }
        }
    }
}

答案 2 :(得分:0)

在Windows上肯定可以实现的功能:

stage('var from groovy') {
    steps {
        script {
             anvar = "foo"
        }

        bat "${anyvar}"
    }
}

答案 3 :(得分:-1)

这是我生产中的一个例子

def dest_app_instance = "${params.Destination}"

switch(dest_app_instance) {
  case "CASE1":
    dest_server = "server1"
    break
  case "CASE2":
    dest_server = "server2"
    break 
}