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

时间:2018-05-19 10:58:43

标签: jenkins jenkins-plugins jenkins-pipeline

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

Pipeline Input Step但是:

  

您可以选择返回信息,因此可以指示步骤的名称。可以通过构建控制台日志底部的链接或侧栏中的链接访问参数输入屏幕以进行构建。

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

我看到的解决方案是使用第三方Web服务器生成表单,然后远程触发作业,但我宁愿使用Jenkins内部可用的东西,以便在呈现表单模板时可以访问Jenkins内部(使用Groovy)。

有解决方案吗?

4 个答案:

答案 0 :(得分:1)

您是否尝试过git参数插件?
 https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin
最终结果将在您的仓库中填充分支列表,您可以选择要构建的分支 enter image description here

配置

enter image description here

这很简单,它将建立您选择的分支:) 这只会建立1个分支,即没有多重选择

如果您想执行多重选择,那么它将变得很棘手。...

您必须获得Active Choices插件 https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin enter image description here

创建shell脚本(提供权限)-get_git_branches.sh
在外壳程序中添加以下内容

#!/bin/bash
GIT_URL=$1
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

enter image description here

管道

node {
    echo States
    def values = Branches.split(',')

    for(value in values)
    println value //build them 'mvn build value'
}

我已经根据您的“例如要求”给出了示例,但是如果您希望做其他事情,例如您提到的创建表单主动选择插件,那是最好的选择,因为您可以创建自定义脚本通过选择,文本框,多次选择(基本上是执行javascript页面渲染),我上面发布的wiki就是很好的例子。
希望对您有所帮助:)

答案 1 :(得分:1)

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

的一部分

您可以获得这样的视图:

build with parameter

带有以下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

答案 2 :(得分:0)

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

  • A

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

答案 3 :(得分:0)

我在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定义一个branchs变量:

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

并在输入步骤中使用它。