In this post, we looks into how to set up notification emails at the end of CI pipelines in a containerized Jenkins system. First, we look into conventional Jenkins system (directly hosted) that has direct communication to the SMTP server. After that, we will look into adjustments required for a containerized Jenkins system to run in the same environment.
Sending emails in standard Jenkins setup
We first look at a typical Jenkins setup, where the Jenkins instance is installed directly on a host machine (VM or bare-metal) and has direct communication to the SMTP server. For corporate network, you may have to use an SMTP relay server instead. For those cases, you can configure SMTP communication by setting up Postfix. In CentOS, it could be a simple “sudo yum install -y mailx”.
After installing, update /etc/postfix/main.cf with correct relay information: myhostname, myorigin, mydestination, relayhost, alias_maps, alias_database. An example /etc/postfix/main.cf is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
We can test the setup by sending a test email with the following command:
1 2 3 |
|
After the postfix
service is up, Jenkins can be configured to send email with Mailer plugin.
Mail server can be configured in Manage Jenkins page, E-mail Notification section.
Please visit Kohei Nozaki’s blog post for more detailed instructions and screenshots.
We can also test the configuration by sending test e-mail in the same E-mail Notification section.
Sending email from container
Many Jenkins-based CI systems have been containerized and deployed on Kubernetes cluster (in conjunction with Kubernetes plugin).
For email notifications in such CI systems, one option is to reuse postfix
service, which is usually configured and ready on the Kubernetes nodes, and expose it to the Docker containers.
There are two changes need to be made on Postfix to expose it to Docker containers on one host.
- Exposing Postfix to the docker network, that is, Postfix must be configured to bind to localhost as well as the docker network.
- Accepting all incoming connections which come from any Docker containers.
Docker bridge (docker0
) acts a a bridge between your ethernet port and docker containers so that data can go back and forth.
We achieve the first requirement by adding the IP of docker0
to inet_iterfaces
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
For the second requirement, the whole docker network as well as localhost should be added to mynetworks
.
In our kubernetes setup, the docker network should be flannel0
and its subnet’s CIDR notation is added to the mynetworks
line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Note the differences in inet_interfaces
and mynetworks
from the last section.
One can simply enter the Docker container/Kubernetes pod to verify such setup.
Note that application mailx
maybe not available in a container since we tend to keep the containers light-weight.
Instead, prepare a sendmail.txt
file (based on this) with the following SMTP commands and use nc
to send out the email as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
For containerized Jenkins system, mail server can also be configured in same Manage Jenkins page, E-mail Notification section.
The only difference is the IP/hostname provided to SMTP server option.
Instead of providing the known SMTP server’s IP and host, one should use the IP of docker0
, as explained above.
In the case of many nodes in Kubernetes cluster with different docker0
IP, the Docker container of Jenkins master should reside only on one host and docker0
’s IP on that host should be used.
References
- Standard email setup in Jenkins
- Setup Postfix
- Configure Postfix for Docker Containers
- More on Postfix for Docker Containers
1 2 |
|