从参数派生的詹金斯声明性管道设置变量 [英] jenkins declarative pipeline set variables derived from parameters

查看:128
本文介绍了从参数派生的詹金斯声明性管道设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a declarative pipeline in a Jenkinsfile but I would like to derive some variables from a parameter. For example given:

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

我想添加一个块,例如:

I would like to add a block like:

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

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

Such that I can use variables elsewhere in the pipeline. For example:

agent { 
   dockerfile {
       filename "$DockerFile"
   }
}

等.

但是脚本在参数environment&中是非法的代理商部分. 它只能逐步使用.

but script is illegal in the parameter, environment & agent sections. It can only be used in steps.

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

I need to use the parameter in the agent block and I want to avoid repeating myself where the variables are used in different steps.

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

Is there a sane way to achieve this? My preferences in order are:

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

这里共享库可能是合适的,而不管它是否实际上是共享的.

A shared library might be appropriate here regardless of whether it is actually shared.

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

The intention is to support a multi-configuration project by creating a parameterised build and invoking it for different parameter sets with a red/blue status light for each configuration. It could be that I have assumed an 'old fashioned' design. In which case an acceptable answer would explain the modern best practice for creating a multi-configuration multi-branch pipeline. Something like: https://support.cloudbees.com/hc/en-us/articles/115000088431-Create-a-Matrix-like-flow-with-Pipeline or Jenkins Pipeline Multiconfiguration Project

另请参阅 Jenkins中的多配置/矩阵构建管道,以获取针对最佳实践的较不具体讨论.

See also Multiconfiguration / matrix build pipeline in Jenkins for less specific discussion of best practices.

推荐答案

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

Never really used the Jenkins declarative pipeline before but I think the way you refer to params is incorrect?

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

I think it might be: ${params.Platform} or params.Platform instead of param.

那么类似下面的内容?

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

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

As I said, never really used it before so not 100%. I was just looking at the syntax used for parameters on the docs: https://jenkins.io/doc/book/pipeline/syntax/#parameters

这篇关于从参数派生的詹金斯声明性管道设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆