Skip to content

Jenkins jobDSL: simple questions and answers

January 13, 2016

During getting with Jenkins jobDSL I faced some questions.

It took time to find right solutions so to save your time I post some tips in question-answer manner.

I hope it will help you in your difficult automation path.

 

 

How to use Jenkins parameters in groovy code?
Sample code:

...

// define parameters as
parameters {
    stringParam('VERSION', '10.2.0.0', '')
    stringParam('BRANCH', 'master', '')
}

...

// use it like this

branch('remotes/origin/pr/\$BRANCH/merge')

How to inject environment variables?
Sample code:

environmentVariables {
    env 'TEST_SUITE', 'suite_tests.xml'
}

How to use github?

Sample code:

git {
    remote {
        github('owner/project')
        credentials('41df22az-1e1f-405d-99fe-bb34450831a') // take it from jenkins' credentials.xml
        branch('remotes/origin/pr/\$BRANCH/merge')
        refspec('+refs/pull/*:refs/remotes/origin/pr/*')
    }

    cloneTimeout 20
    relativeTargetDir('checkout_dir_name')
    wipeOutWorkspace true
}

How to use gradle wrapper?

Firstly you need to add gradle wrapper support. Here one can find out how to build wrapper https://docs.gradle.org/current/userguide/gradle_wrapper.html

gradle('clean build',
        '-xtest',
        true) {
    it / wrapperScript('gradlew')
    it / rootBuildScriptDir('\$workspace/path/to/build/gradle/script')
    it / fromRootBuildScriptDir(false)
    it / makeExecutable(true) // important otherwise it will be without exec permission
}

How to build with ant?

ant {
    targets([])
    buildFile("\$WORKSPACE/" + this.antBuildFile)
    antInstallation('Default')

    props('version': "\$VERSION",
            'base.source.dir': "\$WORKSPACE/" + this.antSourceDir,
            'base.build.dir': "\$WORKSPACE/\$JOB_NAME",
            'flex.sdk.dir': this.antFlexSdkDir,
            'test.mode': "\$FLEX_TEST_MODE")
}

How to create list views?

Map<String, String> views = ['flex'         : 'build_flex.*',
                             'DEV'          : '(build_e.*)|(build_i.*)|(build_t.*)',
                             'seed'         : '(.*seed)']

String dslText = ''

views.each { k, v ->

    dslText += String.format(
            '''
listView('%s') {
description('')
filterBuildQueue()
filterExecutors()
jobs {
regex('%s')
}

jobFilters {
status {
status(Status.UNSTABLE)
}
}

columns {
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
}
}
''', k, v)
}

job('views_seed') {
    scm {
        git {
            remote {
                github 'projet/tobuild'
            }

            wipeOutWorkspace true
        }
    }

    triggers {
        cron('@hourly')
    }

    steps {
        gradle('clean test', '', true)
                {
                    it / wrapperScript('gradlew')
                    it / makeExecutable(true)
                }

        if (views) {
            dsl {
                text(dslText)
            }
        }
    }
}

How to pass/take artifacts form upper job in multi job?

steps {
    phase('Build robot and sap') {
// mb to choose which one to build
        phaseJob('build_pos_cash_tar') {
            currentJobParameters(false)
            parameters {
                currentBuild()
            }
        }

        phaseJob('emulators_build_sap') {
            currentJobParameters(false)
            parameters {
                currentBuild()
            }
        }

    }

    phase('Run tests') {
        phaseJob('rus_test_run_without_deployment') {
            parameters {
                currentBuild()
            }

            copyArtifacts('build_pos_cash_tar') {
                includePatterns('**/*.zip',)
                buildSelector {
                    latestSuccessful(true)
                }
            }
        }
    }
}

Useful links:

Advertisements

From → Automation, Jenkins

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: