Jenkins: Difference between revisions

From miki
Jump to navigation Jump to search
Line 21: Line 21:
Jenkins offers a button <code>Pipeline Syntax</code> to generate Groovy scripts.
Jenkins offers a button <code>Pipeline Syntax</code> to generate Groovy scripts.
Very useful for adding new commands.
Very useful for adding new commands.

=== Custom git scm checkout with another submodule ===

The first line was created with the pipeline syntax tool in Jenkins. Then we simply checkout the branch we want in the submodule.

def checkoutUsk() {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', trackingSubmodules: false]], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://user@server.com/project.git']]])
sh 'cd my_submodule && git checkout origin/master && cd ..'
}

Revision as of 17:32, 30 January 2021

Tips

Cancel older builds if new one starting

Using milestone, we can cancel older builds if a new commit is pushed.

Here an example where all new build cancels the older ones, except on the master branch. This is useful to increase throughput of Jenkins slaves. This policy makes sense since non-master branches typically do not require thorough testing/analysis.

    stage('UTs')
    {
        // if not on master and older builds are ongoing, cancel them !
        if ( env.BRANCH_NAME != 'master' )
        {
            def buildNumber = env.BUILD_NUMBER as int
            if (buildNumber > 1) milestone(buildNumber - 1)
            milestone(buildNumber)
        }
        // ...
    }

Use 'Pipeline Syntax' to write Groovy script

Jenkins offers a button Pipeline Syntax to generate Groovy scripts. Very useful for adding new commands.

Custom git scm checkout with another submodule

The first line was created with the pipeline syntax tool in Jenkins. Then we simply checkout the branch we want in the submodule.

 def checkoutUsk() {
     checkout([$class: 'GitSCM', branches: name: '*/master', doGenerateSubmoduleConfigurations: false, extensions: $class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', trackingSubmodules: false, submoduleCfg: [], userRemoteConfigs: url: 'ssh://user@server.com/project.git'])
     sh 'cd my_submodule && git checkout origin/master && cd ..'
 }