groovy Jenkins动态声明性管道参数

mu0hgdu0  于 2022-11-01  发布在  Jenkins
关注(0)|答案(5)|浏览(375)

Jenkins声明性管道中的参数可以是动态的吗?
我希望在运行时用函数填充选项值。下面的代码生成了一个选项列表,但它们似乎过时了--可能是我第一次运行这段代码时生成的。如果AMI列表发生变化,选项保持不变。我希望每次选择build with parameters时都运行这段代码。

  1. def findAMIs() {
  2. // Find relevant AMIs based on their name
  3. def sout = new StringBuffer(), serr = new StringBuffer()
  4. def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
  5. ' --owners OWNER --filter Name=name,Values=PATTERN \
  6. ' --query Images[*].{AMI:Name} --output text'.execute()
  7. proc.consumeProcessOutput(sout, serr)
  8. proc.waitForOrKill(10000)
  9. return sout.tokenize()
  10. }
  11. def AMIs = findAMIs().join('\n')
  12. pipeline {
  13. // a declarative pipeline
  14. agent any
  15. parameters {
  16. choice(name: 'Release',
  17. choices: AMIs)
  18. }
  19. ...
  20. }

EDIT我最后使用的是jenkins-job-builder,带有扩展的选择参数。它目前不支持groovyScript参数,所以我修改了它。https://review.openstack.org/#q,I 0 c6 ac 0 b49 c24 b8 d3 afbc 06 b 003847 de 2 e043 c2 b8,n,z
EDIT上述链接已失效,因此这里是另一个指向openstack的链接:https://review.opendev.org/#/c/477003/但问题的要点是我向jenkins-job-builder添加了一个名为'groovyScriptFile'的新参数,它被合并了。

t2a7ltrp

t2a7ltrp1#

那么用户输入:

  1. def findAMIs() {
  2. return UUID.randomUUID().toString().split('-').join('\n')
  3. }
  4. node{
  5. def userInput = input(
  6. id: 'userInput', message: 'input parameters', parameters: [
  7. [
  8. $class: 'ChoiceParameterDefinition',
  9. name: 'ami',
  10. choices: findAMIs(),
  11. description: 'AMI',
  12. ],
  13. ]
  14. )
  15. echo ("Selected AMI :: "+userInput)
  16. }
展开查看全部
ppcbkaq5

ppcbkaq52#

还有另一种解决办法:你可以在“pipeline”之前使用“properties”步骤-在那里你也可以使用活动的选择插件:

  1. properties([
  2. parameters([
  3. [
  4. $class: 'ChoiceParameter',
  5. choiceType: 'PT_SINGLE_SELECT',
  6. description: '',
  7. filterable: false,
  8. name: 'Release',
  9. randomName: 'choice-parameter-21337077649621572',
  10. script: [
  11. $class: 'GroovyScript',
  12. fallbackScript: '',
  13. script: '''// Find relevant AMIs based on their name
  14. def sout = new StringBuffer(), serr = new StringBuffer()
  15. def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
  16. ' --owners OWNER --filter Name=name,Values=PATTERN \
  17. ' --query Images[*].{AMI:Name} --output text'.execute()
  18. proc.consumeProcessOutput(sout, serr)
  19. proc.waitForOrKill(10000)
  20. return sout.tokenize()'''
  21. ]
  22. ]
  23. ])
  24. ])
  25. pipeline {
  26. ...
  27. }

唯一的问题是,第一次启动构建时,它会失败。第二次启动时,它应该是一个“带参数的构建”。
希望能有所帮助。

展开查看全部
w6mmgewl

w6mmgewl3#

对于任何需要声明性管道语法选项的人,我在another question中找到了一个很好的解决方案,这对我很有帮助。
这是我基于它的建议。您应该能够使用创建${WORKSPACE}/list文件的代码生成一个更动态的列表

  1. pipeline {
  2. agent any
  3. stages {
  4. stage("Release scope") {
  5. steps {
  6. script {
  7. // Prepare a list and write to file
  8. sh "echo \"patch\nminor\nmajor\" > ${WORKSPACE}/list"
  9. // Load the list into a variable
  10. env.LIST = readFile (file: "${WORKSPACE}/list")
  11. // Show the select input
  12. env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
  13. parameters: [choice(name: 'RELEASE_SCOPE', choices: env.LIST, description: 'What is the release scope?')]
  14. }
  15. echo "Release scope selected: ${env.RELEASE_SCOPE}"
  16. }
  17. }
  18. }
  19. }

我希望这对你有帮助

展开查看全部
dfty9e19

dfty9e194#

在我进入同一条船并阅读了https://www.jenkins.io/doc/book/pipeline/jenkinsfile/“处理参数”部分的说明后,我得到了一个非常简单的方法,因为我们可以在其他一切之前运行属性。
把这个快速的例子和修改你的喜好,我正在创建2个多选择选项,一个将是硬编码,另一个将使用在底部的函数(管道节点外)下的选择选项,你可以使用脚本{}步骤。

  1. properties([
  2. [
  3. $class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false],
  4. parameters
  5. (
  6. [
  7. choice(choices: ['opt1', 'opt2', 'opt3'], description: 'desc', name: 'bla'),
  8. choice(choices: script{return_list()}, description: 'some letter', name: 'ble')
  9. ]
  10. )
  11. ]
  12. )
  13. pipeline {
  14. agent{
  15. label "Linux"
  16. }
  17. stages{
  18. stage("frist"){
  19. steps{
  20. echo "${params.bla}"
  21. echo "${params.ble}"
  22. }
  23. }
  24. }
  25. }
  26. def return_list(){
  27. if ("${JOB_NAME}".contains("bla")){
  28. env.list_users = "1\n 2\n 3\n"
  29. }else{
  30. env.list_users = "a\n b\n c\n"
  31. }
  32. return env.list_users
  33. }

你也可以把它们放到一个步骤里

  1. stages{
  2. stage("add more params"){
  3. steps{
  4. properties([
  5. parameters([
  6. choice(choices: [script{return_list()}
  7. ],
  8. description: 'This is the branch that we will build',
  9. name: 'param3')
  10. ])
  11. ])
  12. }
  13. }

请注意,您必须运行它一次,以便下一个作业将获取参数。(只有最新的属性节点将出现在UI中)
使用此方法,您甚至可以决定是否要在UI中显示某个参数。例如

  1. properties([
  2. [
  3. $class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false],
  4. parameters
  5. (
  6. script{
  7. list_arguments()
  8. }
  9. )
  10. ]
  11. )
  12. pipeline {
  13. agent{
  14. label "Linux"
  15. }
  16. stages{
  17. stage("frist"){
  18. steps{
  19. echo "${params.bla}"
  20. echo "${params.ble}"
  21. }
  22. }
  23. }
  24. }
  25. def return_list(){
  26. if ("${JOB_NAME}".contains("bla")){
  27. env.list_users = "1\n 2\n 3\n"
  28. }else{
  29. env.list_users = "a\n b\n c\n"
  30. }
  31. return env.list_users
  32. }
  33. def list_arguments(){
  34. lista = return_list()
  35. if ("${JOB_NAME}".contains("word")){
  36. ch = [
  37. choice(choices: ['opt1', 'opt2', 'opt3'], description: 'desc', name: 'bla'),
  38. choice(choices: ["${lista}"], description: 'some letter', name: 'ble')
  39. ]
  40. }else{
  41. ch = [
  42. choice(choices: ['opt1', 'opt2', 'opt3'], description: 'desc', name: 'bla')
  43. ]
  44. }
  45. return ch
  46. }
展开查看全部
5w9g7ksd

5w9g7ksd5#

2022年更新:最初在2017年的X1 E2 F1 X的X1 E3 F1 X中提到的X1 E0 F1 X(X1 E1 F1 X)已经进化。
其最新的2.60.2版本(2022年6月)确保支持Jenkins 2.335+,因为最近的Jenkins版本确实修改了DOM,如JENKINS-68013所示。
当前示例:

  1. properties([
  2. parameters([
  3. [
  4. $class: 'ChoiceParameter',
  5. choiceType: 'PT_SINGLE_SELECT',
  6. name: 'Environment',
  7. script: [
  8. $class: 'ScriptlerScript',
  9. scriptlerScriptId:'Environments.groovy'
  10. ]
  11. ],
  12. [
  13. $class: 'CascadeChoiceParameter',
  14. choiceType: 'PT_SINGLE_SELECT',
  15. name: 'Host',
  16. referencedParameters: 'Environment',
  17. script: [
  18. $class: 'ScriptlerScript',
  19. scriptlerScriptId:'HostsInEnv.groovy',
  20. parameters: [
  21. [name:'Environment', value: '$Environment']
  22. ]
  23. ]
  24. ]
  25. ])
  26. ])
  27. pipeline {
  28. agent any
  29. stages {
  30. stage('Build') {
  31. steps {
  32. echo "${params.Environment}"
  33. echo "${params.Host}"
  34. }
  35. }
  36. }
  37. }

请注意,JENKINS-63284PR 47将允许Groovy属性的初始管道作业支持。
此功能将启用Jenkinsfile、管道、cron-scheduled和通过其他方式触发的作业,而无需使用Jenkins的GUI/HTML。

展开查看全部

相关问题