Fullmenu null

 

19 March 2018

In the previous post Docker and Groovy we saw how can we use the official Groovy image for Docker in order to execute our scripts in the host system removing the need to install Groovy. Also with this image we can use all characteristics of Docker as volumes, containers networks, and so on, and in this way our scripts can work with others containers and/or the host.

In this example our script will be executed periodically consuming a remote XML file who contains real time issues in the city of Madrid ( Ayuntamiento de Madrid ). The script will filter issues not planning as problems with traffic light, etc and it’ll send a tweet for everyone.

The script will be execute using the Schedule Pipeline Gitlab’s feature.

To run this scripts, you need:

  • Gitlab account with a repository with our scripts commited.

  • Twitter account with OAUTH credentials for applications.

IncidenciasMadrid.groovy
@Grab(group='org.twitter4j', module='twitter4j-core', version='4.0.6')

import twitter4j.TwitterFactory
import twitter4j.StatusUpdate
import twitter4j.conf.ConfigurationBuilder

tf = TwitterFactory.singleton
if( new File('twitter4j.properties').exists() == false ){	//(1)
	def env = System.getenv()
	ConfigurationBuilder cb = new ConfigurationBuilder()
	cb.setDebugEnabled(true)
	  .setOAuthConsumerKey(env['CONSUMER_KEY'])
	  .setOAuthConsumerSecret(env['CONSUMER_SECRET'])
	  .setOAuthAccessToken(env['ACCESS_TOKEN'])
	  .setOAuthAccessTokenSecret(env['ACCESS_SECRET']);
	tf = new TwitterFactory(cb.build())
}
twitter = tf.instance

body = new URL("http://informo.munimadrid.es/informo/tmadrid/incid_aytomadrid.xml").newReader()
NewDataSet = new XmlSlurper().parse(body)	//(2)
NewDataSet.Incidencias.each{

	if( "$it.incid_prevista" == 'N' && "$it.incid_planificada"=='N' ){

		String tweet="""
@101GroovyScript te informa
AtenciĆ³n, incidencia no prevista
$it.nom_tipo_incidencia:
$it.descripcion
"""

		try{
			twitter.updateStatus tweet	//(3)
		} catch(e){
			println "no se ha enviado"
		}
	}
}
  1. We can use env variables to configure our scripts

  2. Retrieve and filter last issues

  3. Send a tweet per issue

Gitlab

Gitlab Pipeline use an especial file into our repository to know how/what execute called .gitlab-ci.yml. This file must to be in the root of our repository. See https://about.gitlab.com/features/gitlab-ci-cd/ for more information

gitlab-ci.yml
execute incidencias:    //(1)
 image:
   name: groovy:2.4-jdk8    //(2)
 only:
  - schedules       //(3)
 stage: build
 script:
  - groovy IncidenciasMadrid.groovy //(4)
  1. name your job as you want. You can have many jobs

  2. we’ll use the official Groovy image

  3. this jobs will be execute only in a schedule environment

  4. command to execute every time

Scheduler

From the web console of Gitlab we can configure when we want to execute our pipeline, using a chron expresion. Also we can configure the branch and specify environment variables. In our case we’ll configure twitter credentials:

gitlab schedule

Result

As result we’ll have a system who send tweets with the issues using our account, similar to this:

tweet schedule

As you can see, using the Groovy image for Docker we can execute our scripts without intervention and using all characteristics of Groovy as REST, SOAP, database, etc


Script
@Grab(group='org.twitter4j', module='twitter4j-core', version='4.0.6')

import twitter4j.TwitterFactory
import twitter4j.StatusUpdate
import twitter4j.conf.ConfigurationBuilder

tf = TwitterFactory.singleton
if( new File('twitter4j.properties').exists() == false ){	//(1)
	def env = System.getenv()
	ConfigurationBuilder cb = new ConfigurationBuilder()
	cb.setDebugEnabled(true)
	  .setOAuthConsumerKey(env['CONSUMER_KEY'])
	  .setOAuthConsumerSecret(env['CONSUMER_SECRET'])
	  .setOAuthAccessToken(env['ACCESS_TOKEN'])
	  .setOAuthAccessTokenSecret(env['ACCESS_SECRET']);
	tf = new TwitterFactory(cb.build())
}
twitter = tf.instance

body = new URL("http://informo.munimadrid.es/informo/tmadrid/incid_aytomadrid.xml").newReader()
NewDataSet = new XmlSlurper().parse(body)	//(2)
NewDataSet.Incidencias.each{

	if( "$it.incid_prevista" == 'N' && "$it.incid_planificada"=='N' ){

		String tweet="""
@101GroovyScript te informa
AtenciĆ³n, incidencia no prevista
$it.nom_tipo_incidencia:
$it.descripcion
"""

		try{
			twitter.updateStatus tweet	//(3)
		} catch(e){
			println "no se ha enviado"
		}
	}
}