Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline and per project configuration support #8

Merged
merged 10 commits into from
Nov 26, 2018
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
*.swp
.project
target
.idea
*.iml
work
167 changes: 166 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,171 @@ Please fork and send pull requests to [zulip/zulip-jenkins-plugin](https://githu

----

This plugin sends notifications of build statuses to Zulip.
This plugin sends [messages](#zulip-send) and [notifications](#zulip-notification) of build statuses to Zulip.

It began its life as a fork of the [Jenkins Campfire plugin](https://github.com/jenkinsci/campfire-plugin).

## Table of contents
1. [Global Configuration](#global-configuration)
1. [Zulip Notification](#zulip-notification)
1. [Zulip Send](#zulip-send)
1. [Troubleshooting](#troubleshooting)

## Global configuration

The Zulip server is configured globally for the whole Jenkins instance.

Minimal configuration is:
* **Zulip Url** = The Url of your Zulip installation visible from Jenkins instance
* **Zulip User Email** = E-mail of Zulip bot that will be used to send messages to streams
* **Zulip API key** = The API key of bot that will be used to send messages to streams

Other attributes are optional:
* **Default Stream Name** = The stream messages will be sent to by default.
You can override this setting per project, but be not to leave it blank in both places
or your messages will fail to send.
* **Default Topic Name** = The topic messages will be sent to by default.
You can override this setting per project. If you leave it blank at both places,
Jenkins job name will be used as a topic instead
* **Enable Smart Notification** = If enabled, successful build notification will be sent out only if
one of following criteria is met:
* There was no previous build
* The current build failed
* Previous build failed

In another words, the notifications will not be sent for continuously succesful builds.
* **Jenkins URL** = :warning: This parameter is kept just for the sake of backward compatibility.:warning:
Instead of setting this, configure Jenkins URL in "Manage Jenkins" > "Configure System" > "Jenkins Location" > "Jenkins URL"



![Global Settings](docs/global-settings.png)

## Zulip Notification

Zulip notification is a post build action step, that posts notification about build statuses to Zulip Streams.
The step allows you to configure two optional parameters:
* **Stream** = The stream messages will be sent to for this job. Will override default stream from global settings.
Be sure not to leave it blank in both places
* **Topic** = The topic messages will be sent to for this job. Will override default topic from global settings.
If blank in both places, job name will be used as topic.

#### Freestyle project

For freestyle project, simply select a post build action from the dropdown
and optionally configure destination stream and topic.

![Zulip Notification](docs/zulip-notification.png)

#### Scripted Pipeline

Scripted pipeline have no concept of post build actions, but you can still use the ```zulipNotification```
step in the try/catch or preferably using the ```catchError``` step

```jenkins
node {
catchError {
// ... Your build stages ...
}
zulipNotification stream: 'coolproject', topic: 'jenkins'
}
```

#### Declarative Pipeline

In declarative pipeline, simply use the ```zulipNotification``` step inside your post actions

```jenkins
pipeline {
agent any
stages {
// ... Your build stages ...
}
post {
always {
zulipNotification()
}
}
}
```


## Zulip Send

Zulip send is a build step, that allows you to post arbitrary messages to Zulip stream throughout the build process.
You can use this e.g. to notify Zulip that build has started or about various phases the build goes through.
The step allows you to configure:
* **Stream** = Optional stream the message will be sent to for this job. Will override default stream from global settings.
Be sure not to leave it blank in both places
* **Topic** = Optional topic the message will be sent to for this job. Will override default topic from global settings.
If blank in both places, job name will be used as topic.
* **Message** = The message that will sent out. You can use build variables inside the message.

#### Freestyle project

For freestyle project, simply select a build action from the dropdown and fill in desired message.
Optionally configure destination stream and topic.

![Zulip Send](docs/zulip-send.png)

#### Scripted Pipeline

In scripted pipeline, simply use the ```zulipSend``` step in any stage of the build

```jenkins
node {
stage('Prepare') {
zulipSend message: 'Started build #${BUILD_NUMBER} of project ${JOB_NAME}...'
// ... Perphaps SCM checkout here ...
}
// ... Other build stages ...
}
```

#### Declarative Pipeline

In declaretive pipeline, simply use the ```zulipSend``` step in any stage of the build

```jenkins
pipeline {
agent any
stages {
stage('Prepare') {
steps {
zulipSend message: 'Started build #${BUILD_NUMBER} of project ${JOB_NAME}...'
// ... Perphaps SCM checkout here ...
}
}
// ... Other build stages ...
}
}
```

## Troubleshooting

#### No such DSL method

If you're using 1.x version of Jenkins and 1.x version of workflow job plugin (previous name for the pipeline jobs)
you will encounter exceptions like ```java.lang.NoSuchMethodError: No such DSL method zulipSend found among [...]```.

In that case, you will have to use the metastep instead. Simply replace:

```jenkins
zulipSend message: 'Test'
```
with
```jenkins
step([$class: 'HumbugSendStep', message: 'Test'])
```

and

```jenkins
zulipNotification()
```

with

```jenkins
step([$class: 'HumbugNotifier'])
```
Binary file added docs/global-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/zulip-notification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/zulip-send.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 100 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.544</version>
<version>3.19</version>
</parent>

<properties>
<jenkins.version>1.653</jenkins.version>
<java.level>7</java.level>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow.version>1.11</workflow.version>
<structs.version>1.2</structs.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<powermock.version>1.7.4</powermock.version>
<objenesis.version>2.6</objenesis.version>
<mockserver.version>5.4.1</mockserver.version>
</properties>

<artifactId>humbug</artifactId>
<packaging>hpi</packaging>
<version>0.1.4-SNAPSHOT</version>
Expand All @@ -24,21 +37,98 @@
</scm>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>2.0</version>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
</dependency>
<!-- For @Symbol named pipeline steps support -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>${structs.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1-rc1</version>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>${objenesis.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>${mockserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>${mockserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>2.0</compatibleSinceVersion>
</configuration>
</plugin>
</plugins>
</build>

<developers>
<developer>
<id>wdaher</id>
Expand Down
Loading