Tuesday, December 6, 2011

Passing upstream parameters to downstream builds in Jenkins

Jenkins has cool Pipeline plugin, which helps to create build pipelines, that for example, can organize and visualize deployment process. Plugin is very cool and looks great, but lacks few basic features, like passing generated parameters or variables from upstream builds to downstream.
For example, I wanted to pass SVN revision number into downstream builds.
Fortunately it is easy to do it with little hacking. Here is what I did.
  1. Add Groovy plugin.
  2. Add new build step Execute system Groovy script, now it is possible to hook into Jenkins internals.
  3. Then just add Groovy script there:
import hudson.model.*
def thr = Thread.currentThread()
def build = thr?.executable
build.addAction(new ParametersAction(new StringParameterValue('SVN_UPSTREAM', build.getEnvVars()['SVN_REVISION'])))


What this script does it just creates new parameter and adds SVN_REVISION from environment variables there. And pipeline can pass parameters with this mechanism to downstream builds (all of them).
Easy!

Monday, November 28, 2011

Encoding problems in Grails

If there are problems with charsets while submitting forms in Tomcat, and headers for responses/requests looks correct, you can try to fix it by adding URIEncoding="UTF-8" into your server.xml. This fixed things nicely to me. Read more about it at http://wiki.apache.org/tomcat/FAQ/CharacterEncoding.

Thursday, November 24, 2011

MySQL dump without locking tables

If you just do MySQL dump without any parameters, it locks tables and consumes a lot of memory. If you want to avoid it and you have InnoDB, best way to do it is to add single-transaction (run in single transaction) and q (dump one row at once) properties, for example like:

mysqldump -q --single-transaction -u login -ppassword db > dump.sql


Monday, November 14, 2011

Time difference with DST

If you need to calculate time difference that include Daylight Saving Time in Groovy, it is super simple with groovy.time.TimeCategory:

TimeDuration hours = TimeCategory.minus(end.time, start.time)

Monday, October 24, 2011

CSS word-wrap in table

When

word-wrap: break-word;

is applied to table cell, it does seems to wrap anything.
If you want to make it work, just add

table-layout:fixed;

to table definition and it works nicely.

Friday, October 21, 2011

HTTPS without certificate validation check

By default Java validates certificates when you do HTTPS queries. If it is not needed (for test environments, for example), it is possible to disable it (even it is much more complicated then it should be). Just add this code somewhere during initialization (this is groovy, so sorry some formatting):


  import javax.net.ssl.*
  import java.security.cert.*

  javax.net.ssl.TrustManager tm = new javax.net.ssl.X509TrustManager() {
    public boolean isClientTrusted(X509Certificate[] chain) { return true; }
    public boolean isHostTrusted(X509Certificate[] chain) { return true; }
    public boolean isServerTrusted(X509Certificate[] chain) { return true; }
    public X509Certificate[] getAcceptedIssuers() { return null; }
    public void checkClientTrusted(X509Certificate[] chain, String s){  }
    public void checkServerTrusted(X509Certificate[] chain, String s) {  }
  }
  HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) { return true; }
  }
  TrustManager[] trustAllCerts = new TrustManager[1];
  trustAllCerts[0] = tm;
  SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  HttpsURLConnection.setDefaultHostnameVerifier(hv);



Wednesday, October 19, 2011

JUnit report formatting in Grails

Sometimes it is needed to add some additional information to JUnit report. For example, if you want to add screenshots and so on. It can be easily done in Grails.
First, you need to modify scripts/_Events.groovy and redefine reporting template folder.

eventAllTestsStart = {
  junitReportStyleDir = "src/templates/reports/"
}

Then create src/templates/reports folder and just add junit-frames.xsl and junit-noframes.xsl from lib folder under Grails home.

Now Grails will use these files to format test results.