Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document how to configure jobs via JCasC #28

Open
torstenwalter opened this issue Sep 4, 2020 · 8 comments
Open

document how to configure jobs via JCasC #28

torstenwalter opened this issue Sep 4, 2020 · 8 comments
Labels
documentation Improvements or additions to documentation lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@torstenwalter
Copy link
Member

No description provided.

@torstenwalter torstenwalter added the documentation Improvements or additions to documentation label Sep 4, 2020
@stale
Copy link

stale bot commented Oct 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Any further update will cause the issue/pull request to no longer be considered stale. Thank you for your contributions.

@stale stale bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 4, 2020
@kjenney
Copy link

kjenney commented Oct 4, 2020

This should be documented. I have not been able to successfully created jobs with XML but previously (outside of this chart) been able to create jobs with JCasC.

@stale stale bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 4, 2020
@kjenney
Copy link

kjenney commented Oct 4, 2020

Here is an example of a couple jobs that I was just able to create using this chart (and that have worked with JCasC in the past). NOTE: These depend on an additional plugin (parameterized-trigger:2.35.2).

master:
  JCasC:
    configScripts:
      freestyle-jobs: |
        jobs:
          - script: >
              job('Triggered') {
                parameters {
                  fileParam('build.properties')
                }
                steps {
                  copyArtifacts('Triggeranotherjob')
                  environmentVariables {
                    propertiesFile('build.properties')
                  }
                  shell('echo $AMI_ID;cat build.properties')
                }
              }
          - script: >
              job('Triggeranotherjob') {
                wrappers {
                  credentialsBinding {
                    string('PASSWORD','PASSWORD')
                  }
                }
                steps {
                  shell('echo AMI_ID=test > build.properties;cat build.properties')
                }
                publishers {
                  archiveArtifacts('build.properties')
                  downstreamParameterized {
                    trigger('Triggered') {
                      condition('SUCCESS')
                      parameters {
                        propertiesFile('build.properties', true)
                      }
                    }
                  }
                }
              }

@torstenwalter torstenwalter added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. hacktoberfest labels Oct 5, 2020
@Semmu
Copy link

Semmu commented Nov 29, 2020

This plugin is also very useful when seeding default jobs on installation: https://plugins.jenkins.io/job-dsl/

@chiranjitghosh85
Copy link

chiranjitghosh85 commented Dec 1, 2020

@kjenney using job-dsl plugin I am able to create jobs.
I am using the below configurations in the value file. Refer documentation here https://jenkinsci.github.io/job-dsl-plugin/

  additionalPlugins: 
    - job-dsl:1.77

  JCasC:
    defaultConfig: true
    configScripts:
      welcome-message: |
        jenkins:
          systemMessage: Welcome to our CI\CD server.  This Jenkins is configured and managed 'as code'.
      pipeline-job: | 
        jobs:
          - script: >                                     
              pipelineJob('job-dsl-plugin') {
                definition {
                  cpsScm {
                    scm {
                      git {
                        remote {
                          url('https://github.com/jenkinsci/job-dsl-plugin.git')
                          credentials('bitBucketUser')
                        }
                        branch('*/master')
                      }
                    }
                    scriptPath("JenkinsFile")
                    lightweight()
                  }
                }
              } 

@JonathanRRogers
Copy link

@kjenney How did you figure how to configure jobs via JCasC? I haven't found anything about that in the official documentation and the JCasC yaml dump of my running Jenkins has no "jobs" section.

@DaniJG
Copy link

DaniJG commented Mar 29, 2023

I have successfully used the following approach, which might be helpful to others:

  • As part of the CasC chart values, I have a script that uses the Job-DSL plugin to create a Jenkins job which runs a pipeline that I consider the "seed job root".
    The pipeline for this job is defined in a separate file in my repo (in my case /jenkins/pipelines/seed-job-bootstrap.jenkins), and itself will find and process the individual seed-jobs that also live in my repo
    controller:
      JCasC:
        configScripts:
          seed-jobs: |
            security:
              # disable if you want to skip manual approval for the scripts used in seed-jobs
              globalJobDslSecurityConfiguration:
                useScriptSecurity: false
            jobs:
              # Create the seed-job root job
              - script: >
                  pipelineJob('seed-jobs') {
                    displayName('seed-jobs')
    
                    // any other properties you want, like triggers, number of runs to keep, etc
    
                    definition {
                      cpsScm {
                        scm {
                          // point to your own repo where your root seed-job pipeline is defined
                          git {
                            remote {
                              url('https://github.com/my-org/my-repo.git')
                              credentials('my-github-credentials')
                            }
                            branches('*/main')
                          }
                        }
                        scriptPath('jenkins/pipelines/seed-job-bootstrap.jenkins')
                      }
                    }
                  }
              # Queue a run of the root seed job as part of CasC
              - script: queue('seed-jobs')
    
    
  • The seed-job-bootstrap.jenkins Jenkins job uses itself the jobDsl step introduced by the Job-DSL to collect and process all the individual seed-job files that are used to create all my jobs.
    In other words, my repo has a individual seed job files defined inside the /jenkins/seed-jobs folder, each of them creating an individual Jenkins job. The role of the seed-job-bootstrap.jenkins pipeline is to collect and process them all, something done using the jobDsl step:
    def label = "default"
    
    // Runs on the default jenkins agent configured by default when installing the helm chart
    podTemplate(
      label: label,
    ){
      node (label) {
        // checkout repo that contains all the individual seed-jobs (This can perfectly be the same repo that contains this very same seed-job-bootstrap.jenkins)
        stage('checkout') {
            checkout scm: scmGit(
              // NOTE: you could pin seed jobs to a branch/tag other than main
              // branches: [[name: 'refs/tags/${project_tag}']]
              branches: [[name: '*/main']],
              extensions: [],
              userRemoteConfigs: [[credentialsId: 'my-github-credentials', url: 'https://github.com/my-org/my-repo.git']]
            )
        }
    
        // Find all the individual seed-jobs and process them, creating the jobs they define
        stage('create jobs') {
          jobDsl(
            removedJobAction: 'DELETE',
            removedViewAction: 'DELETE',
            targets: 'jenkins/seed-jobs/*/*.jenkins',
            lookupStrategy: 'SEED_JOB'
          )
        }
      }
    }
    
    

If it helps, happy to send a PR and add these steps to the README instructions

@damaestro
Copy link

There is no good documentation for how to set all of this up. It would be great if someone could write something up specifically for initializing jobs. The instructions for setting up a seed job to then load and run the DSL definitions is good if it works and if you need to continually/automatically update the jobs.

If you are looking for just bootstrapping your jobs and don't expect to add any more jobs, or are willing to just re-provision, there is a simple way to bootstrap jobs via a groovy configuration.

controller:
  JCasC:
    configScripts:
      seed-jobs: |
        security:
          globalJobDslSecurityConfiguration:
            useScriptSecurity: false
        jobs:
          - url: https://raw.githubusercontent.com/path/to/your/repo/jenkins.groovy

This will setup the pipelines defined in your groovy. For an example jenkins.groovy:

pipelineJob("p1") {
   description("Pipeline P1 is cool.")
   displayName("Pipeline P1")
   parameters {
      choiceParam("Choice", [1, 2], "Note.")
      stringParam("Works", "yes", "Is it working?")
   }
   definition {
      cpsScm {
         scm {
            git {
               remote {
                  url 'https://github.com/path/to/your/repo.git'
               }
               branch('main')
            }
         }
         // This will be cloned and then ran when Pipeline P1 is ran
         scriptPath('path/to/your/job/in/the/repo/Jenkinsfile')
      }
   }
}
pipelineJob("p2") {
[...]
}
[...]

An example path/to/your/job/in/the/repo/Jenkinsfile is:

pipeline {
  agent any

  environment {
    CHOICE="${params['Choice']}"
    WORKS="${params['Works']}"
  }

  stages {
    stage("S1") {
      steps {
        echo "step 1"
        echo "choice: ${CHOICE}"
        echo "works: ${WORKS}"
        sleep 10
      }
    }
    stage("S2") {
      [...]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests

8 participants