如何使用 Bitbucket 中的 JSON 文件动态填充 Jenkins 中的选择框?

问题描述 投票:0回答:1

我正在开发 Jenkins 管道,我想创建一个选择框,该选择框使用开发分支动态填充存储在 Bitbucket 存储库中的 JSON 文件。基于此 JSON 选择,我将运行下一步,目标是我的 JSON 文件。

这就是我的目标:

  1. 从特定 Bitbucket 存储库中获取 JSON 文件列表。
  2. 使用这些 JSON 文件名填充 Jenkins 中的选择框。
  3. 允许 Jenkins 构建执行器选择这些文件之一作为管道的输入。

我一直在探索使用 Bitbucket API 或 Jenkins 插件等选项,但我不确定如何继续。任何人都可以提供指导或示例脚本来实现这一目标吗?任何帮助或指示将不胜感激。谢谢

json bitbucket cicd
1个回答
0
投票

您需要了解 Jenkins 的主要限制之一 - 在构建开始之前无法加载参数。对参数列表和/或其可能值的任何更改都只会在触发构建后应用,这基本上是不需要的,对于任何严肃的使用来说甚至是危险的。

因此,要解决这个问题,基本上只有两种方法(不包括从 Jenkins 切换到另一个 CI/CD 工具):

根据我的经验,自定义参数在理论上提供了很大的灵活性,但这也有一些缺点:

  • 即使对于简单的情况,它们也极难使用,因为您必须编写和维护大量复杂的 Groovy 脚本;
  • 这些脚本可能需要管理员批准,并且所有脚本都必须经过仔细验证,以便它们不会获得对敏感 Jenkins 内部 API 的批准访问权限 - 这使得它在大型环境中不可行;
  • 他们可能有(如果事实上通常有)未解决的安全漏洞;
  • 管道无法在另一个螺栓连接的 Jenkins 实例上重复使用。

因此,我根据与上述问题相关的 Jenkins Jira 问题下的长期讨论中学到的信息,想出了一个简单的解决方案。它仅依赖于现有的 Jenkins 管道功能,不需要任何依赖项。

首先,我定义一个阶段,该阶段从工作区中的文件读取参数值,并使用声明性语法和脚本语法的组合动态定义参数。请注意,有

Abort on parameter change
参数专用于将来控制构建行为:

stage('Update the job parameters') { 
    steps {
        script {
            def jobParameters = readJson file: 'job_parameters.json'

            properties([
                parameters([
                    choice(
                        name: 'Some choices',
                        description: 'Select from these choices',
                        choices: jobParameters.someChoices
                    ),
                    // all your parameters go here
                    string(
                        name: 'Some value',
                        description: 'Define this value',
                        defaultValue: jobParameters.someDefaultValue,
                        trim: true
                    ),
                    booleanParam(
                        name: 'Abort on parameter change',
                        description: 'Enable to update the build parameters',
                        defaultValue: false
                    )
                ])
            ])
        }
    }
}

您可以使其更加灵活,并将整个参数定义放入该(或其他)文件中,使用带有

readYaml
的 YAML 文件而不是 JSON,或者添加
git
步骤来检查包含参数的不同存储库。例如,我使用它来使用 Ansible 库存中的值填充参数。

然后,我定义了由于参数更改而应停止作业执行的条件。基本上,Jenkins 应该在每个新分支的第一个构建中执行此操作,并且可以根据我们的意愿随时执行此操作。添加

beforeAgent: true
以避免浪费资源也是有意义的。您还可以添加其他条件,但这些是安全使用的最低条件:

stage('Abort the build due to the change in parameters') {
    when {
        anyOf {
            expression { env.BUILD_NUMBER == '1' }
            expression { params.'Abort on parameter change' }
        }
        beforeAgent: true
    }
    steps {
        script {
            currentBuild.result = 'NOT_BUILT'
            currentBuild.displayName = 'marked as not built to update the parameters'
            currentBuild.description = """
                I also add a meaningful description here with all the handy links 
                so that the users see it on build status page and just 
                click the rebuild link with no worries or questions.
            """.stripMargin().stripIndent().trim()
            error "Job parameters were updated - please check ${env.BUILD_URL} and re-run the build"
        }
    }
}

currentBuild.result = 'NOT_BUILT'
确保参数更新不会使构建统计数据变得更糟 - 构建不会被标记为失败并且构建天气不受影响。

此脚本也可以移动到共享库并在任何地方重复使用。

© www.soinside.com 2019 - 2024. All rights reserved.