def flag_prod1 = true
def flag_prod2 = false
def prods = [
'prod1',
'prod2'
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
prods.each
{ prd ->
stage("stage $prd")
{
if (flag_${prd})
{
echo "Do something with $prd..."
}
}
}
}
}
}
}
}
The if statement does not work: if (flag_${prd}).
What should be the syntax of the if statement where part of the variable name needs to be taken from the other variable name?
why not to declare known variable with all possible flags like this:
def prods = [
'prod1',
'prod2'
]
def flags = [
prod1: true,
prod1: false,
]
prods.each{prd->
if( flags[prd] ){
//do something
}
}
or you could try to define all in one:
def prods = [
prod1: [
flag: true
],
prod2: [
flag: false
]
]
for(prd in prods){
if( prd.flag ){
//do smth
}
}
This works (using iterator key/value, and toBoolean() conversion):
def jobs = [
'prod1': true,
'prod2': false
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
jobs.each
{
if ("$it.value".toBoolean())
{
stage("Stage $it.key")
{
echo "Do something..."
}
}
}
}
}
}
}
}
Related
I have this output.txt file:
cg-123456
cg-456789
cg-987654
cg-087431
Is it possible to get these values into a jenkins dropdown, like by using choice-parameter or active-choice-reactive parameter?
You can do something like the below.
pipeline {
agent any
stages {
stage ("Get Inputs") {
steps {
script{
script {
def input = input message: 'Please select the choice', ok: 'Ok',
parameters: [
choice(name: 'CHOICE', choices: getChoices(), description: 'Please select')]
}
}
}
}
}
}
def getChoices() {
filePath = "/path/to/file/output.txt"
def choices = []
def content = readFile(file: filePath)
for(def line : content.split('\n')) {
if(!line.allWhitespace){
choices.add(line.trim())
}
}
return choices
}
properties([gitLabConnection(gitLabConnection: 'GitLab Connection', jobCredentialId: ''), [$class: 'GitlabLogoProperty', repositoryName: ''], parameters([extendedChoice(multiSelectDelimiter: ',', name: 'choice', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'mongo, mysql', visibleItemCount: 10)])])
pipeline {
agent anyenter image description here
stages {
stage('mongo') {
when {
expression { choice == 'mongo' }
}
steps {
echo "${params.choice}"
}
}
stage('mysql') {
when {
expression { choice == 'mysql' }
}
steps {[enter image description here][1]
echo "${params.choice}"
}
}
}
}
When I select both mongo and mysql checkbox then both stage should work but both stage mongo and mysql are skipped
I couldn't get the extendedChoice Parameters setup in the declarative pipeline. Instead, I used Boolean Parameters. Please refer to the following for your use case. Note I have declared a global variable named choice.
def choice = ""
pipeline {
agent any
stages {
stage("Get details") {
steps{
timeout(time: 300, unit: 'SECONDS') {
script {
// Select the product image
choice = input message: 'Please select the product', ok: 'Build',
parameters: [
booleanParam(defaultValue: false, name: 'mongo'),
booleanParam(defaultValue: false, name: 'mysql')]
}
}
}
}
stage('Echo') {
steps {
script {
echo "::: Product : ${choice}"
}
}
}
stage('mongo') {
when {
expression { return choice.get("mongo") }
}
steps {
echo "MONGO"
}
}
stage('mysql') {
when {
expression { return choice.get("mysql") }
}
steps {
echo "MYSQL"
}
}
}
post {
success {
echo 'The process is successfully Completed....'
}
}
}
I want to run jenkins job using jenkinsfile with list of all parameters value or with individual value from parameter list.
def deploy(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.ID}"
]])
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
parameters {
choice(
name: 'ID',
choices: PARAMETERS,
)
stage (DEV') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
deploy('devl') ===> this will call my deploy function
}
else {
echo "$params.ID"
deploy('devl') ===> this will call my deploy function
}
}
}
}
I was able to run job using bye selecting each value from droplist. But I also want to the run the job with all values from ID list. I tried with all but it is not taking all the the values 'id1', 'id2', 'id3', 'id4', 'id5'
You would need to define the choices in a var outside of the parameter and then use that as choices e.g.
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
pipeline {
agent any
parameters {
choice(name: "ID", choices: PARAMETERS )
}
stages {
stage('Test') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
}
else {
echo "$params.ID"
}
}
}
}
}
}
I have list of json value that I need to group according to loc (Location) which has elements of date.
{
"2":[
{
"loc":2,
"date":{
"2020-08-09":"2790.0"
}
},
{
"loc":2,
"date":{
"2020-08-10":"402.0"
}
},
{
"loc":2,
"date":{
"2020-08-11":"522.0"
}
}
],
"12":[
{
"loc":12,
"date":{
"2020-08-10":"765.0"
}
}
],
"13":[
{
"loc":13,
"date":{
"2020-08-11":"135.0"
}
}
]
}
My rails code that I use map and create new object format
result = #weekly_sales_chart.map{ |k|
{
loc: k['location_id'],
date: { k['date']=>k['total']} ,
}
}
respond_with result.group_by { |h| h[:loc]}.map{|f, d| d.inject(:merge)}
Output: (It only group date only one)
[
{
"loc":2,
"date":{
"2020-08-11":"522.0"
}
},
{
"loc":12,
"date":{
"2020-08-10":"765.0"
}
},
{
"loc":13,
"date":{
"2020-08-11":"135.0"
}
}
]
Desired Result: Loc is Group with the same loc value and merge all date under this loc object
[
{
"loc":2,
"date":{
"2020-08-09":"2790.0",
"2020-08-10":"402.0",
"2020-08-11":"522.0"
}
},
{
"loc":12,
"date":{
"2020-08-10":"765.0"
}
},
{
"loc":13,
"date":{
"2020-08-11":"135.0"
}
}
]
Hope someone can help me. Thanks
#Sebastian Palma I already resolved my issue. I refactor your sample code that helps me to produce my desired result:
result = #weekly_sales_chart.map{ |k|
{
loc: k['location_id'],
date: { k['date']=>k['total']} ,
}
}
data = result.group_by { |h| h[:loc]}
respond_with data.map{|_, location_id | location_id.reduce({}) { |result, loc_id|
result.merge(loc_id) do |key, oldval, newval|
key == :date ? oldval.merge(newval) : oldval
end
} }
Thanks for helping out
For example I have following job and I would like to put condition using when, right now it doesn't allow me to put when inside parallel, I only want to run testusers if bool is true
dir("abc") {
parallel (users: {
sh "add_user users.json"
},
when{ ${TEST_USERS} == "true" }
testusers: {
sh "add_user testusers.json"
})
}
}
}
when blocks only operate on stages, so something like:
parallel {
stage('testusers') {
when {
environment name: 'TEST_USERS', value: 'true'
}
steps {
sh "add_user testusers.json"
}
stage('users') {
steps {
sh "add_user users.json"
}
}
}
In scripted, you would just use an if block. This looks like:
parallel ([
'users': {
sh 'add_user users.json'
},
'testusers': {
if (TEST_USERS == 'true') {
sh 'add_user testusers.json'
}
}
])