Surcouf
Surcouf

Reputation: 91

How to pass pipeline variables to post build gerrit message?

I have a Pylint running in a Jenkins pipeline. To implement it, I used Gerrit trigger plugin and Next Generation Warnings plugin. Everything is working as expected - Jenkins is joining the review, checks change with pylint and generates report.

Now, I'd like to post pylint score in a custom "Build successful" message. I wanted to pass the pylint score to a environment variable and use it in dedicated window for Gerrit plugin message.

Unfortunately no matter what I try, I cannot pass any "new" variable to the message. Passing parameters embedded in pipeline works (e.g. patchset number).

I created new environment variable in Configure Jenkins menu, tried exporting to shell, writing to it (via $VAR and env. syntax) but nothing works - that is, build message displays raw string like $VAR instead of what variable contains.

What should I do to pass local pylint score (distinct for every pipeline occurence) to the custom build message for Gerrit?

Upvotes: 2

Views: 1613

Answers (1)

uncletall
uncletall

Reputation: 6842

I don't think the custom message can be used for this. This is just supposed to be a static message.

They way I do this is to use the SSH command to perform the review. You can also achieve the same using the REST API.

First I run my linting and white space checking script that will generate a json file with the information I would like to pass to Gerrit. Next I send it to Gerrit using SSH. See below my pipeline script and an example json file.

As a bonus I have added the robot comments. This will now show up in your review as a remark from Jenkins that line 8 of my Jenkins file has a trailing white space. You can easily replace this with your lint result of you like or just ignore it and only put the message. It is easier to use a json file as it will make it easier to create multi line messages


node('master') {
  sh """
    cat lint_change.json | ssh -p ${env.GERRIT_PORT} ${env.GERRIT_HOST} gerrit review ${env.GERRIT_PATCHSET_REVISION} --json
    """
}

Example json file:

{
  "labels": {
    "Code-Style": "-1"
  }, 
  "message": "Lint Bot Review\nLint Results:\n  Errors: 0\n  Warnings: 0\n\nWhitespace results:\n  Errors: 1", 
  "robot_comments": {
    "Jenkinsfile": [
      {
        "robot_id": "lint-bot", 
        "line": "8", 
        "message": "trailing whitespace."
      }
    ]
  }
}

Alternatively, you may want to look at a new gerrit-code-review-plugin that should make this things even easier. However, I have not tried this yet.

Upvotes: 2

Related Questions