Jenkins pipelines have become more widespread recently. Now we can automated a deployment from code merge to production. Along the way we might build, test and deploy the application in mutliple different environments all in a single pipeline. Sometimes the pipeline requires a manual approval step to move the pipeline along some stages in the pipeline. Implementing this is trivial in Jenkins with a single line of groovy code. When this is implemented, an approver can log in to Jenkins and open the job and approve the activity.
- Jenkins Slack App
- Jenkins Slacksend Attachments
- Jenkins Slack Notification Plugin
- Jenkins Slack Notifications
But that task can sometimes be tedious and Jenkins sessions expire after 30 minutes. In the Labs, as we are constantly working on creating pipelines with our customers, we ended looking at the Jenkins login screen many times a day. Sure, we could alter the timeout expiration to extend the session but there is another way and it is more fun and usually more transparent to those interested in that pipeline. That way is using Slack to proceed and abort pipelines and to integrate that we can use Hubot.
Hubot is a tool developed by Github to facilitate a lot of DevOps activities they needed each day. Hubot is an application that provides an integration point to many different applications. Here I will demonstate how to implement a simple Jenkins pipeline that will connect to a Hubot we deploy in OpenShift 3 and approve a pipeline step using Slack.
Slack The Slack app for Jenkins X provides integration with Jenkins X and Slack This has been developed and tested so far using pipelines triggered by commits from GitHub and deployed using Jenkins X on Google Container Engine. Extreme Hydrological Events: New Concepts for Security NATO Science Series A Series presenting the results of scientific meetings supported under the NATO Science Programme.
The first thing you will need is a Slack workspace that you are able to configure. You can create one at slack.com for free.
Then find the Hubot app at /apps/manage
. Search for Hubot in the search box. Follow the steps to create an integration point. For this you need:
Jenkins Slack App
- The name of the bot. It can be anything. When invoking Hubot will we proceed the command with this name. I would consider making this an easy to type name
- The integration token. This will allow Hubot to interact with Slack. It can be regenerated at anytime.
- A default channel to send messages. We will take advantage of this in the example but the bot will respond in any channels it is a member of.
Invite your new point to the default channel.
This example deploys the application to OpenShift so you will need to have access to OpenShift. At https://developers.redhat.com you can a free localmachine version called MiniShift. Follow the install instructions there or use any available OpenShift instance. You need to have privileges to create projects for the code used here. You will also need the oc client that will interact with Ansible. If you are using MiniShift this client is provided with the install found somewhere like ~/.minishift/cache/oc/v3.6.173.0.5/oc
login
This uses Ansible and the OpenShift Applier. The example was run with Ansible 2.4. To verify you can run the command in your terminal
For this example, all the code you need for this example to run. The code is located at https://github.com/mcanoy/ocp-examples.git. You can clone this or fork and clone it and modify. Once it is cloned you have all the code you need. The Hubot code was developed by following the getting started guide at https://hubot.github.com/docs/. The code was then modified to allow Jenkins authentication by OCP token instead of user credentials.
Change into the directory hubot-slack.
Modify the file params/hubot/build-deploy
by adding your slack token to the parameter named HUBOT_SLACK_TOKEN
If necessary, modify the file params/jenkins/deploy
by adding your slack channel name to the parameter named HUBOT_DEFAULT_ROOM
First import the ansible applier code (one time only)
Then run the playbook (as needed)
The applier applies several templates that create applications in OpenShift in a new project named hubot. If something fails you can fix the templates or parameters and re-run the applier it will update only the changes you made. Navigate to the hubot project in OpenShift web console.
Observe the build and deploy process working. After a few minutes everything should be up and running and the first request for approval will appear automatically in your default chat room.
The inventory of applications resides in the file inventory/group_vars/all.yml
and lists the template url and parameter file for each of Jenkins, Hubot and a sample application.
Hubot
Jenkins Slacksend Attachments
The hubot code and OpenShift template resides in another github.com repository https://github.com/mcanoy/labs-ci-cd.git which is a fork of the rht-labs repository. It provides a build config with a base image of nodejs:6
and a deployment config for the hubot. It also provides a service, imagestream and route. It is an all in one template to get hubot running.
Jenkins
Jenkins is split into 2 OpenShift templates. A build template and deploy template. The deploy template is customize to be able to reference hubot in the application. The code to build Jenkins resides in the https://github.com/mcanoy/labs-ci-cd.git. This code provides some add ons to the standard OpenShift Jenkins image - relevant for us is the inclusion of the hubot plugin.
Sample App
To put it all together we need a sample Jenkins pipeline to invoke and make the request to hubot and slack. The code for that reside in the repository you cloned in the directory sample-app which contains a single Jenkinsfile. This pipeline should be invoke as soon Jenkins comes online. It is possible it may fail if the hubot application has not yet come online. If this is the case (or you simple want to invoke the pipeline) you can go to the OpenShift Console in the hubot project, select Builds –> Pipelines and start the sample-app pipeline to kick off a build.
Slack
When the pipeline is invoked you should see a message appear in the default channel specified for Slack. You can resond with a message to approve the step
Thanks for reading!
DevOps aims to increase the communication and collaboration between Development and Operations teams in the workplace. However, this communication ideal can be hard to fully execute.
Many organizations traditionally use email for any internal communication. However, this is difficult to maintain in a fast-paced development environment. There are numerous problems that can arise from using email: people don’t see the emailsor see them too late, people forget to respond in a timely manner or never respond at all. One single place where teams can come together to discuss the Development Lifecycle is extremely beneficial to organizations
Many enterprise organizations are now turning to the platform Slack in order to increase communication in the workplace
Use Slack with DevOps Applications
Slack has the capability to integrate with several of the most popular DevOps tools, such as GitHub, Jenkins CI, Jira, and Azure Pipelines. Inedo’s BuildMaster is also able to integrate with Slack. Slack is able to integrate seamlesslyinto the existing development toolchain, making it easier to identify and remedy any builds, errors, or general problems that might occur.
How to integrate Slack with Jenkins? You need to install slack notifications plug-in in Jenkins.
Pre-requistes:
Install Slack notifications plug-in
Steps to integrate Slack with Jenkins:
1. Go to www.slack.com and signup with your email, create your workspace.
2. Create a channel in Slack.
3. --> Click connect an app
4. enter Jenkins CI and choose that.
5. Click on Add configuration
6. Add Jenkins CI configuration and select the channel from the drop down.
7. once you added, you will see the below page:
Jenkins Slack Notification Plugin
11. Click on Build Now, Now you will see results in Slack channel.