0

I want to setup a Jenkins from code to

  1. Create one initial pipeline
  2. Create the Job DSL seed job and executing it to configure jobs used in the pipeline
  3. Configure Jenkins settings
    • Locales - set locale to EN
    • Access control - Lock down system

I read many tutorials and questions and found the following ideas

  • Using the Jenkins CLI
  • Some Job DSL interface for setting up a job as described here at the bottom
  • Using JenkinsSCI interface within a Groovy file located in init.groovy.d - see below

For testing I use Docker and have the following sample already running.

Dockerfile

# https://github.com/jenkinsci/docker/blob/master/README.md
FROM jenkins/jenkins:lts

USER root

COPY groovy/* /usr/share/jenkins/ref/init.groovy.d/

USER jenkins

EXPOSE 8080

ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]

groovy/jobs/test1-basic.groovy

#!/usr/bin/env groovy

import hudson.model.*
import jenkins.model.Jenkins;
import hudson.tasks.Shell;

job = Jenkins.instance.createProject(FreeStyleProject, 'test1-basic')
job.buildersList.add(new Shell('echo hello world'))
job.save()

The sample sadly lacks the

  • configuration part, as I do not know how to access the locale plugin from within the groovy code
  • Job DSL integration, how to read the seed job and execute it ones

I really did an intensive research and could not find much about this initial setup part. It seems many people do this manually, or the legacy way copying XML files. Could you help me out solving this and making it a "best practice documentation" for other?

lony
  • 6,733
  • 11
  • 60
  • 92

1 Answers1

0

If you are familiar with configuration management tool like chef you can use it for configuring your jenkins instance. There is a jenkins community cookbook which can be utilized to write a wrapper to suite your needs.

jenkins_job resource in this cookbook lets you create any type of job be it pipeline, free style etc, you just need to supply the required job configuration. You can template this with variables so based on what you supplied, job will be created accordingly. Not just jobs, you can configure almost everything you do manually with chef using a resource corresponding to that.

One of the best part about using chef is you can source control it and update configuration based on requirements at any point of time.

If you are not planning to use a configuration management tool, you can check out the discussion here on how to achieve job creation with plugins

slashpai
  • 1,141
  • 7
  • 12
  • Thank you, I'm not a big fan of Chef but use Ansible a lot. Your recommendation to use the cookbook is a really good one! I try to look how it is achieved there and then extract the code into my small script. As for my use case configuration management it to bloated. Thanks – lony Dec 18 '17 at 11:56