Can manually triggered jobs of pipeline take user-input for paramters? - jenkins

I have two jobs (Job1 & Job2). Both are parameterized with the same parameters but the parameter values differ and are designed using the Active-Choice-Parameter (uno) plugin.
I wish to run both jobs in pipeline however, below is the exact requirement.
When the pipeline is executed Job1 executes and prompts user to enter parameters (UI). The user enters / selects the values and triggers it to build.
Once the build on Job1 completes the user is prompted for (approval) to proceed to the next Job2. The user approves by clicking "OK/Proceed" button; & thereby Job2 of the pipeline gets triggered.
Note: I have achieved this using "input" feature of Groovy Script.
The parameter values of Job1 should be passed and should showup in Job2; however the user should be able to see and modify the passed values for any parameter in Job2 (UI).
Note: I'm able to pass the parameter values using "Parameterized Trigger Plugin" on "Post-Build-Actions" of Job1
Problem statement:
Running the pipeline does not show users parameter screen (UI) for either Job1 or Job2 so that the user could enter / select and change the parameters for either Job1 or Job2 during the pipeline run.
Note:
I'm able to overcome the Problem Statement by using Build Pipeline Plugin:
But the reason i do not wish to consider this solutions is
I don't know how can I inject the groovy pipeline script input element which prompts for approval between jobs.
I have read that using the pipeline plugin has advantages over using Build Pipeline Plugin
Below is Groovy script (Pipeline script)
agent any //agent specifies where the pipeline will execute.
stages {
stage ("build PROD") { //an arbitrary stage name
steps {
build 'job1' //this is where we specify which job to invoke.
}
}
stage ("build DR") { //an arbitrary stage name
input{
message "Press Ok to continue"
submitter "user1,user2"
parameters {
string(name:'username', defaultValue: 'user', description: 'Username of the user pressing Ok')
}
}
steps {
echo "User: ${username} said Ok."
build 'job2' //this is where we specify which job to invoke.
}
}
}
}
Any solution would be of great help. Thanks.

Is there a reason you are keeping the jobs separate? What I would do is re-evaluate your job flow and see if it makes more sense to have the jobs merge into one pipeline.

You could simply use the parameter https://jenkins.io/doc/book/pipeline/syntax/#parameters
Then you have the default user interface, which is simpler then you custom groovy code.

Related

How to trigger Automation Pipeline after an Build Pipeline is successfully and only triggered by a specific user

Suppose Jenkins Build A is Successfully Triggered and after that Automation Pipeline on that Build is to be executed.
The above Scenario is possible using Jenkins Build Triggers using : Build after other projects are built
But in addition when we want to trigger Automation only if the build is generated by a specific used
For e.g.
User A, User B, Users C
So the Automation Pipeline must get generated only if the Build Pipeline is triggered by User A and User B.
The Automation Pipeline must not be triggered if the Build is generated by User C.
If I understand your requirement correctly, you have two Jobs, Build
and Automation. If Build Job is triggered by UserA, UserB.... you want to trigger the Automation Job. In order to achieve this, in your Build job, before triggering the Automation Job you can check who triggered the Build Job and decide whether to trigger the Automation job or not. Check the example below.
pipeline {
agent any
stages {
stage ('BuildJob') {
steps {
script {
def userList = ["admin2", "UserA", "UserB", "UserC"]
def buildTrigger = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
// Here checking username and triggering the automation build
if(buildTrigger != null && !buildTrigger.userId.disjoint(userList)) {
echo "Build the Second JOb========"
build job: 'AutomationJob'
}
}
}
}
}
}

How to access stringParam in groovy Jenkins pipeline

I have a job which takes stringParam and i am trying to access that stringParam from workflow, however i am getting No such property exception.
parameters {
stringParam("COMMIT_SHA", "", "[Required] Short SHA of the commit used to build the image")
}
print "The commit sha val is: $env.COMMIT_SHA"
I have tried different options like ${params.COMMIT_SHA}, ${COMMIT_SHA}
Solution
Modify your parameters block as follows
parameters {
string(name: 'COMMIT_SHA', defaultValue: '', description: '[Required] Short SHA of the commit used to build the image')
}
You should then be able to reference it using ${params.COMMIT_SHA} or ${COMMIT_SHA} or ${env.COMMIT_SHA}
Your syntax is for the Jobs DSL plugin but you stated you are using a pipeline. If you are, in fact, using a Freestyle Job to create other jobs instead of a Pipeline please provide the entire script and I will update my answer. However, your post appears to want to set the parameter for a pipeline ( not specify the metadata for the creation of a separate job via the Jobs DSL plugin )
The following Job DSL script, when executed, will create a Job called "example" with COMMIT_SHA as a parameter. It won't be added as a parameter to the job that contains the Job DSL script
job('example') {
parameters {
stringParam('myParameterName', 'my default stringParam value', 'my description')
}
}
In other words, there is nothing to print in the job that contains the stringParam code because that code configures a different job to contain a string parameter, it doesn't add that parameter to the current job

How to access trigger event properties in a Jenkins pipeline script

I have a Jenkins job config that uses the "Build whenever the specified event is seen" trigger (supported by the Cloudbee's Notification API plugin) and specifies a Jmespath Query (e.g. ref=='refs/heads/master') and runs a pipeline script. I want to access other properties in the trigger event (e.g. repository.full_name) from within the pipeline script. How can I do this?
Found the answer. The data I was looking for is in the com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause instance of the build causes. For example, the following code finds all the commits:
def newCommits = currentBuild.rawBuild.getCauses().findAll {
it instanceof com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause
}.collect{
it.getEvent().commits
}

Pass large amount of parameters between jobs

I have two Jenkins jobs that tun on separate computers. On computer 1 I read properties file and use it for environment variables. But i need the same file on PC 2 and it only exist on the first one. When the first Jenkins job finishes it starts the second one and it can pass parameters file via job but I have to receive with creation of separate parameter with Parameterized Trigger Plugin for each parameter, and I have a lot and don`t want to do so. Is there simple solution for this issue?
Forget Jenkins 1 and the plugins Parameterized Trigger Plugin. Using Jenkins 2, here's an example of your need:
node ("pc1") {
stage "step1"
stash name: "app", includes: "properties_dir/*"
}
node ("pc2") {
stage "step2"
dir("dir_to_unstash") {
unstash "app"
}
}

RunListener and QueueListener not invoked in pipeline?

I'm trying to write a plugin that listens for node executions during a Jenkins pipeline. The pipeline will have some code like this:
stage ('production deploy') {
input 'enter change ticket #'...
node('prod') {
// production deploy code here
}
}
Either on allocation of node, or before any tasks run on the node, I want to verify a change management ticket has been approved. For Freestyle jobs I could use QueueListener or RunListener, but neither of these are invoked when I run a pipeline.
I can't put this code in the pipeline script because anyone that can edit the pipeline script could remove the verification.
Are there any other listeners I could hook into before, or just after a node is allocated in a pipeline?
In my previous implementation for freestyle builds, I had overridden the setUpEnvironment method. I didn't realize this was not called in pipeline runs - makes sense. I then implemented onStarted in my RunListener and I successfully broke into my code. Just confusion on my part.

Resources