Tuesday, October 18, 2016

Jenkins - How to Automate CI and CD Pipeline Use DSL (2)

So in my previous tutorial (http://tonylixu.blogspot.ca/2016/10/jenkins-how-to-automate-ci-and-cd.html), we learned how to create a simple Jenkins job from DSL. In this tutoril, we will learn how to create a pipeline from DSL.

For a structure view of the souce code, please go to "https://github.com/tonylixu/jenkins-dsl/tree/master/microservice-dsls"

- "config" contains global configuration files include company url, jenkins plugin version, and microservice definitions.
- "microservice_pipeline.dsl" is the groovey script to generate nested views.

The "dsl" files in config directory are pretty straightforward, I am not going to explain it in details. Let's focus on "microservice_pipeline.dsl". All microservices are defined in "Json" format, so we import "groovy.json.JsonSlurper" to parse it. Then we load "versions.dsl" and "urls.dsl". After that we define a LinkedHashMap multiPut function, this allows you to append multiple values to the same key. In case of two microservices have the same parent project, we don't want to create two separate pipelines (in my case, two microservices are shareing the same build and release steps so we integrate into one pipeline), you can create two separate pipelines, that's totally fine.

Then we load and parse "services.dsl", aggregate each microservice into ArrayList
def service_objects = slurper.parseText(readFileFromWorkspace('microservice-dsls/config/services.dsl'))
service_objects.each { name, data ->
    for (item in data) {

Then for each service, we create jenkins jobs:
service_lists.each {
    // Add each service into map, this is for nestedView later
    service_map[it.name] = it

    // Creating master build, release and piranha deploy jobs
    println "Create master branch builder job for ${it.parent_name}-service"
    createMasterBuildJob(it.parent_name, it, versions, urls)
    println "Create release job for ${it.parent_name}-service"
    // Eliminate weird jenkins duplicate entries
    createReleaseJob(it.parent_name, versions, urls)
    println "Create deploy job for ${it.name}-service"
    createDeployJob(it.parent_name, it, versions)

After we create all the jenkins jobs, we generate "Nested View" for all pipelines. The best way to try this is modify the "jenkins-seed" job, configure the "Source Code Management" section and "Process Job DSLs" section like the following:

The final view looks like:

In the case of two micro services share the same build and parent project, you can just define the "services.dsl" to:
    "service1": [{
        "parent_name": "service1",
        "name": "service1",
        "group": "Microservice Pipeline",
        "service_directory": "service1",
        "system_test_project": "system-tests"
    "service2": [{
        "parent_name": "service2",
        "name": "service2-1",
        "group": "Microservice Pipeline",
        "service_directory": "service2-1",
        "system_test_project": "system-tests1"
    }, {
        "parent_name": "service2",
        "name": "service2-2",
        "group": "Microservice Pipeline",
        "service_directory": "service2-2",
        "system_test_project": "system-tests2"

and re-run the job generator, the new pipeline will look like:

No comments: