jenkinsci / helm-charts

Jenkins helm charts
https://artifacthub.io/packages/helm/jenkinsci/jenkins
Apache License 2.0
561 stars 885 forks source link

document how to configure jobs via JCasC #28

Open torstenwalter opened 4 years ago

stale[bot] commented 3 years ago

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.

kjenney commented 3 years ago

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.

kjenney commented 3 years ago

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)
                      }
                    }
                  }
                }
              }
Semmu commented 3 years ago

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

chiranjitghosh85 commented 3 years ago

@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 commented 2 years ago

@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 commented 1 year ago

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

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

damaestro commented 6 months ago

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