Jenkins

From miki
Revision as of 14:44, 4 February 2021 by Mip (talk | contribs) (→‎igfx_ error)
Jump to navigation Jump to search

Links

Groovy reference

Strings

// https://stackoverflow.com/questions/50029296/extracting-part-of-a-string-on-jenkins-pipeline
def url = "git@github.com:project/access-server-pd.git"
final beforeColon = url.substring(0, url.indexOf(':'))  // git@github.com
final afterLastSlash = url.substring(url.lastIndexOf('/') + 1, url.length())  // access-server-pd.git
println beforeColon

// Note: def / final optional

String a = "Hello World Hello";
println(a.matches("Hello(.*)")); // true
println(a.replaceAll("^Hello","Bye"));  // Bye World Hell

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 ..'
 }

Checkout git with submodule, not recursive

Say we have a repo with submodules, also with submodules, but only want to checkout first level.

  • Add parent repo
  • Select Advanced sub-modules behaviours, but don't click recursively update submodules.

Increase perf on Windows

  • Disable anti-virus, or move master/slave workspace in not-scanned directory
  • Careful slave: on Windows, the slave uses the launch directory as base directory. Move that, or define user.dir in the slave environment variables (within Jenkins).
  • Disable Windows search

Increase job number

First get a listing all available job in the script console:

Jenkins.instance.getAllItems(AbstractItem.class).each {
    println it.fullName + " - " + it.class
};

Then set the RC for the selected job with:

Jenkins.instance.getItemByFullName("your/job/name").updateNextBuildNumber(128)

Troubleshoot

igfx_ error

Doing this:

println new ProcessBuilder('sh','-c','ls').redirectErrorStream(true).start().text

we got a igfx_ crash message in Windows. PC not accessible through remote desktop, and had to reboot.

Multibranch failed because of tag/branch name conflict (some local refs could not be updated; try running)

This occurs when for instance a branch named dev/foo is deleted and a new branch dev/foo/bar is created afterwards.

To fix:

  • Go to multibranch log, and check for line like:
Creating git repository in C:\Jenkins\caches\git-1f7143a5b7a29bcbc3c47f31fc7a597c
 > git init C:\Jenkins\caches\git-1f7143a5b7a29bcbc3c47f31fc7a597c # timeout=10
  • Go to the server, and delete that cache directory.