helm-charts
helm-charts copied to clipboard
document how to configure jobs via JCasC
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.
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.
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)
}
}
}
}
}
This plugin is also very useful when seeding default jobs on installation: https://plugins.jenkins.io/job-dsl/
@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()
}
}
}
@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.
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 repocontroller: 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 thejobDsl
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 theseed-job-bootstrap.jenkins
pipeline is to collect and process them all, something done using thejobDsl
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
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") {
[...]
}
}
}