Maybe some of you know PacktPub – part of the Pack Publishing Limited Company. They publish once a day a free IT e-books, which can be saved and downloaded from the account. Every day one can download another e-book from the very big library. It’s a really really great possibility, but it has one, serious problem. We must remember to take a look at the page every day. Without this, we may miss something interesting. Moreover, manual checking is not what we want to do. You wouldn’t be there if you like doing manual things, which can be done automatically! A long time ago my friend and ex-coworker found an idea, how to automate checking for the new e-books. He wrote an Ansible role which adds a simple bash script to the Linux cron. That script checks every day for the new e-books and sends the link to the Slack channel.
Easy, right? Yes, I do like that very, very much. However, I prefer to have a centralized scheduler and guess what is the best choice in this case in my opinion? Yes, Jenkins!
You can get to the Grzyboll’s repository by clicking the image above. There is an Ansible role, which you can use if you want to make a traditional cron job (because maybe you don’t have and/or don’t need a Jenkins server). Hereinafter I describe you how to implement this role in Jenkins. PLEASE NOTICE – the author of the Ansible Role Free E-Books is Grzyboll – I’ve just created a Jenkins implementation.
Free IT e-books with Jenkins
I assume you have your own Jenkins installation. Now, you need to create a Freestyle job (it’s the simplest way), but first, there’s one more thing. The Slack token. It’s required by the script, which sends data to the Slack channel. You can read about how to create an incoming webhooks in the Slack documentation. After you configure your incoming webhook, you should have something like this:
That URL shouldn’t be published or exposed. With webhook, anyone can send to your channels anything. Of course, the above webhook doesn’t exist anymore, it was created only for this post. So, when you have an incoming webhook, there is only one simple step – you should add this incoming webhook as the Jenkins’ credential. I have described in another post how to create credentials with Jenkins. Here you need a secret text. In my case, I left the https://hooks.slack.com/services/ unencrypted, and as the credential, I used only the rest of the URL.
Jenkins Freestyle Job
This scenario covers only the simplest way – Freestyle Job. Log in to your Jenkins master and create a new job.
After that, configure your project. You should pass some parameters like channel or username, hence mark the “This project is parametrized” checkbox. Below you can see parameters in my Jenkins Job.
If you want you can hardcode some of them. For example, $packtpuburl doesn’t need to be changed.
- channel – channel name you choose for this job on your Slack
- packtpuburl – URL with the free IT e-books
- pretext – some text which will be printed each time on your channel (example below)
- username – username variable displayed in the Slack notification
When you’re done with parameters, create a credential binding. Do you remember your incoming webhook? Yes, I’m writing about it. If you do not know how to pass credential in Jenkins, please visit my post where I explained it in details. The endpoint should look like this one:
That binding allows you to pass previously created credential to your Job. After that, go to the Build section and create a “Shell execute” step. There you can paste the bash script from the Grzyboll’s repository and modify it a little bit. The script was created as the part of the Ansible role, so – what is expected – it contains Ansible specific syntax. Here you can see how it should look like:
There is only one more thing to do. Setting up the schedule. You can do that in the “Build Triggers” section by marking the “Build periodically” checkbox. Jenkins’ scheduler is similar to the cron. In the example below, it will check for the new free IT e-books every day between 7:00 A.M. and 7:05 A.M. – for more details and examples you can click the question mark in your Jenkins server.
Test your configuration
After all these steps, you can finally enjoy your new Job! Just wait for scheduled time or run that job manually. If everything is configured properly, you should receive a notification of today’s free IT e-book. Moreover, you should receive these notifications every day. Below you can see the example, how it can look on your Slack channel.