Select Page

Check this out, that can be interesting

I prepared few groovy scripts that i wrote, when i was needed to update a lot of jobs on Jenkins.

What this scripts do

  1. Delete “Publish JUnit report” step from all Jenkins freestyle jobs
  2. Replace word “junit-report.xml” on “test_group_result.xml”
  3. Replace big part of text on a new one

 


import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
import groovy.util.XmlParser
//Job name where will be execute script
def partOfJobNameToSearch = "Run_Test_Suite"
def okCounter = 0
def count = 0

Jenkins.instance.getAllItems(AbstractProject.class).each {item ->
  //Total number of actions
  count++     
  //Searching jobs    
  if(item.name.toLowerCase().contains(partOfJobNameToSearch.toLowerCase())){
    def config = item.getConfigFile()
    println("---------------------------------")
    println("[INFO] loading xml: " + config)
    File file = config.getFile()
    String fileContents = file.getText('UTF-8')
    xml=new XmlParser().parseText(fileContents)
    if(xml.publishers."hudson.tasks.junit.JUnitResultArchiver"){
        def nodeToDel=xml.publishers."hudson.tasks.junit.JUnitResultArchiver"   
        def parent = nodeToDel[0].parent()
        println nodeToDel[0]
        //remove node with JUnit report
        parent.remove(nodeToDel[0]) 
        okCounter++
        println "[INFO] save changes in config.xml"
        file.withWriter { w ->
            w.write(XmlUtil.serialize(xml))
        }
        // reload jenkins job config file
        InputStream is = new FileInputStream(file)
        item.updateByXml(new StreamSource(is))
        item.save()
    }       
  }
}
println("---------------------------------")
println count
println(okCounter + " items with status successful  :)")
#2


import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
import groovy.util.XmlSlurper
import groovy.util.XmlParser


def partOfJobNameToSearch = "Your job name"
def contain_text="Parameter_contain_this_test"
def old_part="junit_results.xml"
def new_part="test_group_result.xml"

def okCounter = 0
def count = 0
Jenkins.instance.getAllItems(AbstractProject.class).each {item ->
  count++
  if(item.name.toLowerCase().contains(partOfJobNameToSearch.toLowerCase())){
    def config = item.getConfigFile()
    println("---------------------------------")
    println("[INFO] loading xml: " + config)
    File file = config.getFile()
    String fileContents = file.getText('UTF-8')
    xml=new XmlParser().parseText(fileContents)

    def nodeToModify=xml.buildWrappers.findAll{n-> 
        if(n."EnvInjectBuildWrapper".info.propertiesContent){
            if(n."EnvInjectBuildWrapper".info.propertiesContent.text().toLowerCase().contains(contain_text.toLowerCase())){
                println "this is vbs"
                n."EnvInjectBuildWrapper".info.propertiesContent.value=n."EnvInjectBuildWrapper".info.propertiesContent.text().replace(old_part, new_part)
                println "[INFO] save changes in config.xml"
                file.withWriter { w ->
                    w.write(XmlUtil.serialize(xml))
                }
                // reload jenkins job config file
                InputStream is = new FileInputStream(file)
                item.updateByXml(new StreamSource(is))
                item.save()
            }
        }    
    }       
  }
}
println("---------------------------------")
println count
println(okCounter + " items with status successful  :)")

import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
import groovy.util.XmlSlurper
import groovy.util.XmlParser


def partOfJobNameToSearch = "Run_Test_Suite"
def old_part='''echo "Starting convertation"
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Load("D:\\some-tranform.xslt")
$xslt.Transform("$env:ENV_VAR\\TestResult.xml", "$env:env:ENV_VAR\\TestResult.html")

$xslt.Load("D:\\nunit-transform.xslt")
$xslt.Transform("env:ENV_VAR\\TestResult.xml", "env:ENV_VAR\\junit-results.xml")
$xslt.Transform("env:ENV_VAR\\TestResult.xml", "env:ENV_VAR\\junit-results.xml")'''

def new_part='''echo "Starting convertation"
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Load("D:\\some-transform.xslt")
$xslt.Transform("$env:ENV_VAR\\TestResult.xml", "$env:ENV_VAR\\TestResult.html")

$xslt.Load("$env:ENV_VAR\\dmsTest\\$env:ENV_VAR\\Transformation.xslt")
$xslt.Transform("$env:ENV_VAR\\TestResult.xml", "$env:ENV_VAR\\test_group_result.xml")
$xslt.Transform("$env:ENV_VAR\\TestResult.xml", "$env:ENV_VAR\\test_group_result.xml")'''


def okCounter = 0
def count = 0
Jenkins.instance.getAllItems(AbstractProject.class).each {item ->
  count++
  if(item.name.toLowerCase().contains(partOfJobNameToSearch.toLowerCase())){
    def config = item.getConfigFile()
    println("---------------------------------")
    println("[INFO] loading xml: " + config)
    File file = config.getFile()
    String fileContents = file.getText('UTF-8')
    xml=new XmlParser().parseText(fileContents)

    def nodeToModify=xml.builders.findAll{n-> 
        if(n."hudson.plugins.msbuild.MsBuildBuilder".msBuildName.text()){
            println "This is CSharp job"
            if(n."hudson.plugins.powershell.PowerShell".command[1] && n."hudson.plugins.powershell.PowerShell".command[1].text() == old_part){
                okCounter++
                println n."hudson.plugins.powershell.PowerShell".command[1].text()           
                n."hudson.plugins.powershell.PowerShell".command[1].value = new_part
                println "[INFO] save changes in config.xml"
                file.withWriter { w ->
                    w.write(XmlUtil.serialize(xml))
                }
                // reload jenkins job config file
                InputStream is = new FileInputStream(file)
                item.updateByXml(new StreamSource(is))
                item.save()      
            }
        }     
    }       
  }
}
println("---------------------------------")
println count
println(okCounter + " items with status successful  :)")