3

I want to show a dynamically generated form (for example with a list of currently existing branches) then trigger a job with the selected parameters.

There is Pipeline Input Step but:

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.

I need after clicking "Build" to show the custom form immediately.

A solution I see is to have a third party web-server which generates the form then remotely triggers the job, but I would rather use something available inside Jenkins, so that I would have access to Jenkins internals when rendering the form template (using Groovy).

Is there a solution?

warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • I'm intrigued. Tell me if I understood well : (01) When you click in option "build" of your job, a custom form will be rendered ? (02) Why @s-spieker answer is not correct? (03) What are the rules to generate your custom form ? Maybe a predefined input parameters could be enough. – JRichardsz Jul 12 '18 at 15:09
  • @JRichardsz I think I found my answer -- I accepted it. Thank you for your time! – warvariuc Jul 14 '18 at 10:48

4 Answers4

2

Have you tried the git Parameter Plugin ??
https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin
The final result will populate the list of branches from your repo and you can select the branch you would like to build enter image description here

Configuration

enter image description here

It's straight forward and it will build the branch that you have selected :) This will build only 1 branch i.e. no multiple option

IF you want to do the multiple option then it gets tricky....

You have to get the Active Choices Plug-in https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin enter image description here

Create a shell script(provide permissions)-- get_git_branches.sh
add the following in the shell

#!/bin/bash
GIT_URL=$1
git ls-remote --heads --tags ${GIT_URL} | awk -F" " '{print $NF}'

Configuration

Add the following

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

Pipeline

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

I have given the example based on your "e.g. requirement" but if you wish to do something different like you mentioned creating a form Active choice plugin is the best bet as you can create your custom script with choice,textbox,multiple select (basically do a javascript page rendering) the wiki which I have posted above has great examples.
Hope it helps :)

Community
  • 1
  • 1
rohit thomas
  • 2,302
  • 11
  • 23
  • Yes, Active choice plugin is the thing. Somehow I missed it. Maybe I was confused by its description: "The Active Choices plugin allows the creation of dynamic and interactive parameters for freestyle Jenkins jobs". I though if it's for freestyle jobs, it doesn't work with pipelines. – warvariuc Jul 13 '18 at 15:42
1

You can write your own Jenkinsfile in groovy syntax and generate your parameters for the build. You can even create your own HTML files with that. It is part of the Pipeline Multibranch Plugin

You can get a view like this:

build with parameter

With following 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 ...
    }
}

There is also a nice tutorial how to generate the choices from a json: Jenkins dynamic parameters using Extended Choice Parameter Plugin and Groovy

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
  • I think Active choice plugin does what I need simpler. But I explore your solution too. Thank you for your time! – warvariuc Jul 14 '18 at 10:49
0

As far as I know ,there is no direct solution for this . What you can try (if it is extremely required ) is you can break this step into two jobs :

  • A
  • B

In job A you can hit build and then send a confirmation mail or slack with a form and you can fill the variables from the build . Then in that mail you have to give a url on submit which also passes the required parameters as a GET or POST request . This url will point to a server side script where with the help of these parameters you can build the job B .If you decide to go with this and get stuck somewhere I can further elaborate .

Siddhant Mishra
  • 488
  • 2
  • 12
  • This solution is not much different from having an input step, because it requires you to fill the form looking somewhere else. – warvariuc May 22 '18 at 04:28
0

I see in JENKINS-46971 there is a choice parameter:

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

You could adapt that to your case, defining a branches variable with the list of Git branches:

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

And use that in your input step.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `input` shows a form, but only if you go to the corresponding page, find the link on the right side. > 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. < This is tedious. I want the form to be shown immediately. – warvariuc Jul 13 '18 at 14:19
  • @warvariuc Agreed. After testing, this does not seem a good option. – VonC Jul 13 '18 at 14:20
  • The other problem I see with this, that the build is stuck until you input the parameters... (For example someone forgets about the form). – warvariuc Jul 13 '18 at 14:22
  • @warvariuc In't that the goal though: to have a form shown immediately, in order for the user to input a choice? – VonC Jul 13 '18 at 14:24
  • Yes, that's the goal. My comment was related to the `input` step. – warvariuc Jul 13 '18 at 15:41