显示自定义表单然后触发作业

Show custom form then trigger a job

我想显示一个动态生成的表单(例如,包含当前存在的分支列表),然后使用所选参数触发作业。

Pipeline Input Step 但是:

You can optionally request information back, hence the name of the step. The parameter entry screen can be accessed via a link at the bottom of the build console log or via link in the sidebar for a build.

我需要点击 "Build" 后立即显示自定义表单。

我看到的一个解决方案是让第三方网络服务器生成表单然后远程触发作业,但我宁愿使用 Jenkins 内部可用的东西,这样我就可以在渲染时访问 Jenkins 内部结构表单模板(使用 Groovy)。

有解决办法吗?

据我所知,对此没有直接的解决方案。您可以尝试(如果非常需要)将此步骤分成两个工作:

  • 一个
  • B

在作业 A 中,您可以点击构建,然后发送一封确认邮件或带有表单的 slack,您可以从构建中填充变量。然后在该邮件中,您必须在提交时提供 url ,它还将所需参数作为 GET 或 POST 请求传递。 这个 url 将指向一个服务器端脚本,在这些脚本的帮助下,您可以在其中构建作业 B。如果您决定使用它并卡在某个地方,我可以进一步详细说明。

你试过git参数插件吗??
https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin
最终结果将填充您的 repo 中的分支列表,您可以 select 您想要构建的分支

配置

它很简单,它将构建您 selected 的分支 :) 这将只构建 1 个分支,即没有多个选项

如果你想做多个选项,那就有点棘手了....

您必须安装 Active Choices 插件 https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin

创建shell脚本(提供权限)--get_git_branches.sh
在 shell

中添加以下内容
#!/bin/bash
GIT_URL=
git ls-remote --heads --tags ${GIT_URL} | awk -F" " '{print $NF}'

配置

添加以下内容

tags = []
text = "get_git_branches.sh https://user:pass@bitbucket.org/project/repo_name.git".execute().text
text.eachLine { tags.push(it) }
return tags

管道

node {
    echo States
    def values = Branches.split(',')
    
    for(value in values)
    println value //build them 'mvn build value'
}

我已经根据您的“例如要求”给出了示例,但是如果您希望做一些不同的事情,就像您提到的创建表单 Active choice plugin 是您最好的选择可以使用 choice,textbox,multiple select 创建您的自定义脚本(基本上做一个 javascript 页面渲染)我在上面发布的 wiki 有很好的例子。
希望对您有所帮助:)

您可以使用 groovy 语法编写自己的 Jenkinsfile 并为构建生成参数。您甚至可以用它创建自己的 HTML 文件。它是 Pipeline Multibranch Plugin

的一部分

您可以获得这样的视图:

与以下 Jenkinsfile:

#!/usr/bin/env groovy

def buildInfo
def gitEnv

pipeline {
  agent any

  options {
    timeout(time: 1, unit: 'HOURS')
  }

  tools {
    nodejs 'NodeJS LTS 8.9.4'
  }
  parameters {
    choice(
        choices: 'BUILD_AND_DEPLOY\nBUILD\nDEPLOY_ONLY',
        description: '''
            Sets the desired build and deployment level
            <table>
            <thead>
                <tr>
                <th>Level</th>
                <th>Effect</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <td>BUILD_AND_DEPLOY</td>
                <td>BUILD and DEPLOY afterwards</td>
                </tr>
                <tr>
                <td>BUILD</td>
                <td>Builds the project and uploads the resulting artifact(s)</td>
                </tr>
                <tr>
                <td>DEPLOY_ONLY</td>
                <td>Fetches the latest service artifact and deploys it. Replaces existing service on the agent if present.</td>
                </tr>
            </tbody>
        ''',
        name: 'deploymentLevel'
    )
  }
  stages {
    stage ('Preparation') {
        steps {
            script {
                // maybe do something special with a tag
                gitEnv = checkout scm
            }
        }
    }

    stage ('Install Dependencies') {
        when {
            anyOf {
                expression { return params.deploymentLevel == 'BUILD' }
                expression { return params.deploymentLevel == 'BUILD_AND_DEPLOY' }
            }
        }
        steps {
          bat 'npm install'
        }
    }
    stage ('and more ...')
    {
       // build and ...
    }
}

还有一个很好的教程如何从 json 生成选择:Jenkins dynamic parameters using Extended Choice Parameter Plugin and Groovy

我在JENKINS-46971中看到有一个选择参数:

def environmentChoices = ['blue', 'green'].join('\n') 
def stagingEnvironmentInpit = input( message: "apistaging.evolution-software.com is currently pointed to ${currentEnvironment}. Where do you want to promote to?", ok: 'Deploy', parameters: [choice(choices: environmentChoices, name: 'RELEASE_ENVIRONMENT')] ) 
 echo env.RELEASE_ENVIRONMENT

您可以根据自己的情况进行调整,使用 list of Git branches:

定义分支变量
def branches = sh(returnStdout: true, script: "git for-each-ref --format='%(refname:short)' refs/heads/")

并在您的输入步骤中使用它。