Giter Site home page Giter Site logo

Comments (8)

chiranjitghosh85 avatar chiranjitghosh85 commented on July 20, 2024 5

@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()
                  }
                }
              } 

from helm-charts.

DaniJG avatar DaniJG commented on July 20, 2024 2

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

from helm-charts.

stale avatar stale commented on July 20, 2024

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.

from helm-charts.

kjenney avatar kjenney commented on July 20, 2024

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.

from helm-charts.

kjenney avatar kjenney commented on July 20, 2024

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)
                      }
                    }
                  }
                }
              }

from helm-charts.

Semmu avatar Semmu commented on July 20, 2024

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

from helm-charts.

JonathanRRogers avatar JonathanRRogers commented on July 20, 2024

@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.

from helm-charts.

damaestro avatar damaestro commented on July 20, 2024

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") {
      [...]
    }
  }
}

from helm-charts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.