Conditional input step in declarative pipeline - jenkins-pipeline

using Jenkins v2.138.2 and workflow-aggregator v2.6, I'm trying to define a conditional input step that depends on the value of a job parameter as follows:
stage('apply') {
when { expression { params.apply_plan != 'no' } }
if (params.apply_plan != 'yes') {
input {
message 'Apply this plan?'
}
}
steps {
withAWS(region: 'us-east-1', role: assume_role) {
dir(path: tf_dir) {
sh "make apply"
}
}
}
}
However this if { ( ... ) input { ...} } syntax gives me a run-time error:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.support.steps.input.InputStep.message expects class java.lang.String but received class org.jenkinsci.plugins.workflow.cps.CpsClosure2
Any idea how to do this?
Thanks,
Chris

I think you are using the wrong syntax here. The input { … } is only valid as a directive (outside of the steps directly below stage). What you want to use is the input step which is described here. Basically you just need to remove the curly braces and put it into a script within steps:
stage("stage") {
steps {
script {
if (params.apply_plan != 'yes') {
input message: 'Apply this plan?'
}
}
withAWS(region: 'us-east-1', role: assume_role) {
dir(path: tf_dir) {
sh "make apply"
}
}
}
}

Related

Laravel Pint json config not wokring

I've been playing around with Laravel Pint and I can't seem to make it work the way I want.
My goal is to have the curly brackets inline with class or function
so instead
function test()
{
}
I want to have a format like this
function test() {
}
I have this on pint.json but doesn't change anything.
{
"preset": "laravel",
"braces": {
"position_after_functions_and_oop_constructs": "same",
"position_after_anonymous_constructs": "same"
}
}
I event tried using psr12 preset and still does not change anything
{
"preset": "psr12"
}
Additionally, I'd like to know how I can allow this format
if ( !$num )
return;
it changes to this after running pint, (it removes the space between if condition and added a space after ! and wrap the state with brackets)
if (! $num) {
return;
}
The rule in a pint.json will be
{
"preset": "laravel",
"rules": {
"not_operator_with_successor_space": false,
"curly_braces_position": {
"functions_opening_brace": "same_line",
"classes_opening_brace": "same_line"
}
}
}
As per PHP-CS-Fixer Rule, use curly_braces_position
Ref: How with pint can I remove space after negative “!” symbol?

GraphQL using a field as argument in the same query

I have constructed the following query in Gitlab's GraphQL Explorer to show my question:
https://gitlab.com/-/graphql-explorer
{
project(fullPath: "gitlab-org/gitlab") {
name
repository {
rootRef
}
mergeRequests(state: merged, targetBranches: "master") {
nodes {
sourceBranch
targetBranch
}
}
}
}
What I would like to achieve is using the value of repository { rootRef } as the argument for targetBranches.
Something like this:
mergeRequests(state: merged, targetBranches: repository { rootRef }) {
Can it be done?
Thank you!

How to use choice parameter integer value as array in Jenkins Declarative pipeline file

I have a Jenkins choice parameter which is integer value. I need to loop through the parameter value in my Jenkins File to run a function [i] times.
Say for Ex: Choice Parameter has ['1','2','3','4'] in drop down. If I chooses 4, the loop should go through 4 times.
But my below code only displays I choose in the parameter which is '4' while echoing it. Could someone help me in loop through.
script {
def loop_value = "${params.choiceparameter}"
loop_value.each() {
echo it
}
}
Use it like this:
pipeline {
agent any
parameters {
choice choices: ['1', '2', '3', '4', '5'], description: '', name: 'choiceParameter'
}
stages {
stage("stage1") {
steps {
script {
for (i = 0; i < params.choiceParameter.toInteger(); i ++) {
print(i)
}
}
}
}
}
}
Groovy Solution
Here is a groovy solution more similar to your own
script {
def loop_value = "${params.choiceparameter}"
1.upto(loop_value.toInteger()) {
println it
}
}

Single value vs Array in GraphQL Enum input types

How do you define an input type that either accepts a single enum value or an array of values in GraphQL?
According to GitHub GraphQL API,
{
securityVulnerabilities(first: 3, ecosystem: RUBYGEMS) {
nodes {
advisory {
description
}
}
}
}
But I think array can be good because user can search across ecosystem.
{
securityVulnerabilities(first: 3, ecosystem: [RUBYGEMS, NPM]) {
nodes {
advisory {
description
}
}
}
}
You can do this by defining the input value as an array [] of your defined Enum, something like:
enum MyEnum {
RUBYGEMS
NPM
}
type Query {
securityVulnerabilities(ecosystem: [MyEnum!]): MyReturnObject
}
And then you can query it like:
{
securityVulnerabilities(ecosystem: [RUBYGEMS, NPM]) {
....
}
}
And it works with both an array or a single value:
{
securityVulnerabilities(ecosystem: RUBYGEMS) {
....
}
}
The GraphQL spec explains the following:
If the value passed as an input to a list type is not a list and not
the null value, then the result of input coercion is a list of size
one [...]

Jenkins pipeline to verify multpile file existence in when condition

I need to verify multiple file existence in single when condition using && operation.
check for any file that ends with .doc along with final.txt and proceed further.
Second fileexistence(final.txt) seems to be working fine seperately. Please suggest on this
when {
expression
{
return (fileExists("""ls ${Path}/${version}/test/*.doc""")) && !(fileExists("""${Path}/${Version}/test2/final.txt"""))
}
}
You could use allOf and not
when {
allOf {
expression {
return fileExists("ls ${Path}/${version}/test/*.doc")
}
not {
expression {
return fileExists("${Path}/${Version}/test2/final.txt")
}
}
}
}

Resources