question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For loop not accepted in JCasc jobdsl

See original GitHub issue

Your checklist for this issue

  • [ x ] Jenkins version
  • [ x ] Plugin version
  • [ x ] OS

I am using the Jenkins helm chart to deploy Jenkins on Kubernetes. Helm chart version 1.23.0. (There have been a lot of updates on the chart lately, the version is from last week).

  • Jenkins Version: lts
  • JCasc Version: 1.41
  • OS: Official Jenkins docker image

Description

I am inserting a jobdsl script with a loop to generate a couple of multibranch jobs. I am using helm to inject these values. The helm chart then mounts this script locally. Everything is correctly inserted. The following lines show a cat of the inserted script:

root@jenkins-5969d5b49-z8chv:/# cat /var/jenkins_home/casc_configs/jobs.yaml
jobs:
- script: >
    def scriptpaths = ['one','two']
    for( i in scriptpaths) {
       multibranchPipelineJob("${i}") {
          branchSources {
             git {
                   id("${i}") // IMPORTANT: use a constant and unique identifier
                   remote('git@xxxxxxxx')
                   credentialsId('cloud-jenkins-ssh-private-key')
                   includes('dev')
             }
          }

          orphanedItemStrategy {
             discardOldItems {
                   numToKeep(20)
             }
          }
       }
    }

When executing through jcasc the following error is thrown:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script: 1: expecting EOF, found 'for' @ line 1, column 33.
   def scriptpaths = ['one','two'] for( i in scriptpaths) {
                                   ^

1 error
[....]

When I do a copy & paste of the job script, paste it manually into a jobdsl build job, everything works.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
timjacommented, Jun 2, 2020

I think you’re trimming whitespace,

can you try with | instead of > after your script:

0reactions
jetersencommented, Jun 17, 2020

@njtman as I mentioned it matters with YAML literal you use.

Your best bet is | or |- again see the https://yaml-multiline.info/

jobs:
- script: |
    def scriptpaths = ['one','two']
    for( i in scriptpaths) {
       multibranchPipelineJob("$i") {
          branchSources {
             git {
                   id("$i") // IMPORTANT: use a constant and unique identifier
                   remote('git@xxxxxxxx')
                   credentialsId('cloud-jenkins-ssh-private-key')
                   includes('dev')
             }
          }

          orphanedItemStrategy {
             discardOldItems {
                   numToKeep(20)
             }
          }
       }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass Variable on a Per-File Basis in Jenkins Configuration as ...
In the example above, the SUPERHERO variable looks to be a global, but I need a way to reuse the same groovy include...
Read more >
jenkinsci/configuration-as-code-plugin - Gitter
I've been looking up a lot of documentation about JCasC and Job DSL plugin. ... Jenkins sees the folder as job but will...
Read more >
Different behavior with configure when looping vs using each
Is something special being done in each that's not being done during the for that the Job DSL code runs the configure block...
Read more >
Jenkins Tutorial: Implementing a Seed Job - HappyCoders.eu
2.1 Loading the Jenkins Job DSL code from the project repository ... Otherwise, you get the error message "script not yet approved for...
Read more >
Jenkins Configuration as Code (JCasC) together with JobDSL ...
Unflexible configurations, because everything must fit for everybody (all-in-one instances for whole companies); No real existing backup ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found