How can a Jenkins input step be completed via APIs? - jenkins

I have a Jenkins pipeline defined that includes an input step. A human can provide the input by clicking in the Jenkins UI and there is an HTTP endpoint to provide input as well.
Is it possible to provide the input via Groovy API calls? For instance, could a parallel step in the same pipeline provide the input values? Or, could a completely different build provide input values via Groovy code?
The reason I'd like to use Groovy is to keep the input providing entirely in the Jenkins system and avoid having to provide authentication credentials for the HTTP endpoint.

We had a similar problem (one pipeline should be able to trigger input steps in other pipelines).
This worked in the Jenkins script console and should work in a pipeline:
import org.jenkinsci.plugins.workflow.support.steps.input.*
def build = Jenkins.instance.getItemByFullName("TestInputPipeline").getLastBuild()
def action = build.getAction(InputAction.class)
action.getExecutions().get(0).proceed([])
TestInputPipeline is the name of a test pipeline with a single input.
If your input has parameters, you will probably be able to provide them with the map in the proceed call.
This Input Step Plugin test code helped us: https://github.com/jenkinsci/pipeline-input-step-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/support/steps/input/InputStepRestartTest.java
JavaDoc can be found here: https://javadoc.jenkins.io/plugin/pipeline-input-step/index.html

Related

How to Send Gradle Parameters TO Jenkins?

I’ve seen many articles for sending Jenkins parameters to Gradle, which are passed as System variables, but have not come across doing the opposite. In my build.gradle file I have specified 2 parameters for a file name and version number. In order to pass this file name to the next job in the pipeline, I’d like to send the parameters to the next Jenkins job so it can make use of the file.
Is sending gradle params back to Jenkins possible using built in functionality??

Jenkins Pipeline: how to trigger a build as a downstream that requires an input

I have a Jenkins pipeline job defined. The Jenkinsfile of this job has an input defined using a dropdown. So when the build is triggered the user is requested to select an input.
For manually triggering this is working great!
However, if we want the same job to be triggered by another job as a downstream. How can we do this? providing the input parameters.
Thanks
As #StephenKing pointed out to get setup we need, we have to move away from using input step and make the build parameterized.
The answer I was looking for is here

I Want to pass Jenkins parameters into a build DSL Script. It is not taking this parameter.

I have defined a build flow with the following script in Jenkins in a Build Flow
Build("Server Repository","ENV")
where ENV is a choice parameter defined in the Build Flow Item.When I am building it is not at all using my chosen parameter. It is taking default and same values at every build.
Build("Server Repository",$ENV)
Has been also tried. It does not work.Plus declaring Jenkins Parameters too have been tried.
ENV=${ENV}
Build("Server Repository",ENV)
Has been tried too. Suggest some possible ways to work this out.

How to send first pipe result to second one in Jenkins pipeline?

we use Jenkins as CI tools.
we want to separate login from other process.
we define a job for login, in this job we validate user and if user is valid we get user id.
at other job we need to have user id to generate result,Our problem is how we can send first job result(here:user id) to second one?
You can do this with the use of two plugins:
EnvInject Plugin
Parameterize Trigger Plugin
EnvInject allows you to inject variables into the Jenkins environment so they are available even after that build step.
Parameterize Trigger plugin allows you to pass information in this build job to another build job you want to start as parameters.
Once you've determined the username (I assume in some sort of batch or bash, you don't note the OS) you'll need to write it to a file on the system using a key=value pair. Then use EnvInject to get the value from the file into the jenkins environment. After that you'll use the parameterize trigger plugin to build the next job with parameters. This will require that you check the This build is parameterized box in the second job and that you define the appropriate parameters (perhaps with a default value that you can use to intentionally fail the build if you don't get a good value).

How to call a jenkins Job based on User inputs

The issue here is once the first Job in a Jenkins pipeline is done, we need to ask some inputs from user and based on the user Inputs to decide the next job to be triggered(Job2 or Job3)
tried build flow and parameterzied trigger plugin but didn't find any suitable option under these.
Any other plugin or jenkins feature which can help in achieving the above scenario?
There are a few plugins I have tried which collect user input on manually triggered jobs in a build pipeline: Active Choices Plug-in 1.2 and Extensible Choice Parameter 1.3.2.
With Active Choices you define a list of selections and a default value. With Extensible Choice Parameter you can have a text box and a default value.
This is how they work for me in Build Pipeline 1.4.8 on jenkins 1.628
If you run the manual step directly in the pipeline the default is used and other parameters propagate through correctly.
If you open the step there is an option to Build with Parameters which will ask for the user input. This works but other parameters like the build number do not propagate through so the pipeline is broken, and the pipeline screen does not show the status.
Jenkins will never pause and ask a user for inputs. It is an automated build system. It doesn't expect anyone sitting at the console watching the progress.
You can provide "inputs" or parameters when you manually trigger the job, i.e on the first job in your pipeline. You can them pass these parameters to downstream jobs, either through the Parameterized Trigger plugin or through a file copied between jobs.
If you need a human decision in the middle of your build flow, consider Promoted Builds plugin. With this plugin, a human can select a build, and then decide which "Promotion" to execute (which could branch your workflow as you need). The promotions can also be automated if needed, based on criteria and not human input.

Resources