Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagating logs in shared library to jenkins job console

I am trying to write a shared libray which combines of global variables and shared functions to perform automated task of build and deployment for our project

The project layout as below:

enter image description here

The project has two major parts:

  1. Global shared variables which are placed in vars folder

  2. Supporting groovy scripts to abstract logics which in turn will be called in the global variable.

enter image description here

Inside the groovy class, I am using println to log debugging information

enter image description here

But it never got printed out when it is invoked through jenkins pipeline job

enter image description here

The log console of jenkins job as below:

enter image description here

Can someone show me how to propage logs from groovy class to jenkins job console as I can see only log from println in the global shared script is shown in log console.

like image 905
Joey Trang Avatar asked Nov 16 '17 02:11

Joey Trang


2 Answers

I just found a way to do it by calling println step that is available in the jenkins job

Basically I create a wrapper function as below in Groovy class PhoenixEurekaService:

enter image description here

The steps is actually the jenkins job environment passed into Groovy class via constructor. By this way we can call any steps available in jenkins job in Groovy class.

enter image description here

In global groovy script PhoenixLib.groovy

enter image description here

I am not sure is there other way to do that...

like image 197
Joey Trang Avatar answered Nov 04 '22 10:11

Joey Trang


All the commands/DSL e.g: println, sh, bat, checkout etc can't be accessed from shared library. ref: https://jenkins.io/doc/book/pipeline/shared-libraries/.

You can access steps by passing them to shared library.

//your library
package org.foo
class Utilities implements Serializable {
  def steps
  Utilities(steps) {this.steps = steps}
  def mvn(args) {
    steps.println "Hello world"
    steps.echo "Hello echo"
    //steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
  }
} 

jenkinsfile

@Library('utils') import org.foo.Utilities
def utils = new Utilities(this)
node {
  utils.mvn '!!! so this how println can be worked out or any other step!!'
}
like image 24
old-monk Avatar answered Nov 04 '22 09:11

old-monk