Creating a Multi-Purpose Jenkins Job: Emails, SMS, YouTube Downloads, and WhatsApp Messages

In today’s interconnected world, automation isn’t just about deploying code or running tests. Sometimes, we need to automate communication and media handling tasks. In this blog post, we’ll explore how to create a versatile Jenkins job that can send emails, SMS, download YouTube videos, and send WhatsApp messages.

Prerequisites

Before we begin, ensure you have:

  1. A Jenkins server set up
  2. The necessary plugins installed (Email Extension Plugin)
  3. A Twilio account for SMS and WhatsApp messaging
  4. youtube-dl installed on your Jenkins server

Setting Up the Jenkins Job

We’ll create a pipeline job in Jenkins that handles all these tasks. Here’s a breakdown of each stage:

1. Sending Emails

We use the emailext step to send emails. This requires the Email Extension Plugin.

stage('Send Email') {
steps {
script {
emailext body: 'This is a test email from Jenkins',
subject: 'Jenkins Test Email',
to: params.EMAIL_TO
}
}
}

2. Sending SMS

For SMS, we’re using Twilio’s API. Make sure to set up Twilio credentials in Jenkins.

stage('Send SMS') {
steps {
script {
sh """
curl -X POST https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json \
--data-urlencode "Body=This is a test SMS from Jenkins" \
--data-urlencode "From=${TWILIO_PHONE_NUMBER}" \
--data-urlencode "To=${params.SMS_TO}" \
-u ${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}
"""
}
}
}

3. Downloading YouTube Videos

We use youtube-dl to download YouTube videos. Ensure it’s installed on your Jenkins server.

stage('Download YouTube Video') {
steps {
script {
sh "youtube-dl ${params.YOUTUBE_URL}"
}
}
}

4. Sending WhatsApp Messages

Similar to SMS, we use Twilio’s API for WhatsApp messages.

stage('Send WhatsApp Message') {
steps {
script {
sh """
curl -X POST https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json \
--data-urlencode "Body=This is a test WhatsApp message from Jenkins" \
--data-urlencode "From=whatsapp:${TWILIO_WHATSAPP_NUMBER}" \
--data-urlencode "To=whatsapp:${params.WHATSAPP_TO}" \
-u ${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}
"""
}
}
}

Running the Job

When you run this job, Jenkins will prompt you to enter the email address, phone numbers, and YouTube URL as parameters. The job will then execute each stage, sending messages and downloading the specified video.

pipeline {
agent any
parameters {
string(name: 'EMAIL_TO', defaultValue: '[email protected]', description: 'Email recipient')
string(name: 'SMS_TO', defaultValue: '+1234567890', description: 'SMS recipient number')
string(name: 'WHATSAPP_TO', defaultValue: '+1234567890', description: 'WhatsApp recipient number')
string(name: 'YOUTUBE_URL', defaultValue: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', description: 'YouTube video URL')
}
environment {
TWILIO_ACCOUNT_SID = credentials('twilio-account-sid')
TWILIO_AUTH_TOKEN = credentials('twilio-auth-token')
TWILIO_PHONE_NUMBER = credentials('twilio-phone-number')
TWILIO_WHATSAPP_NUMBER = credentials('twilio-whatsapp-number')
}
stages {
stage('Send Email') {
steps {
script {
emailext body: 'This is a test email from Jenkins',
subject: 'Jenkins Test Email',
to: params.EMAIL_TO
}
}
}
stage('Send SMS') {
steps {
script {
sh """
curl -X POST https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json \
--data-urlencode "Body=This is a test SMS from Jenkins" \
--data-urlencode "From=${TWILIO_PHONE_NUMBER}" \
--data-urlencode "To=${params.SMS_TO}" \
-u ${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}
"""
}
}
}
stage('Download YouTube Video') {
steps {
script {
sh "youtube-dl ${params.YOUTUBE_URL}"
}
}
}
stage('Send WhatsApp Message') {
steps {
script {
sh """
curl -X POST https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json \
--data-urlencode "Body=This is a test WhatsApp message from Jenkins" \
--data-urlencode "From=whatsapp:${TWILIO_WHATSAPP_NUMBER}" \
--data-urlencode "To=whatsapp:${params.WHATSAPP_TO}" \
-u ${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}
"""
}
}
}
}
}

Enhancing the Job

To make this job more robust and useful:

  1. Add error handling for each stage
  2. Implement more complex email templates
  3. Add options to customize message content
  4. Implement file attachment for emails
  5. Add a stage to process or analyze downloaded YouTube videos

Security Considerations

Remember to:

  1. Use Jenkins credentials to securely store sensitive information like API keys
  2. Be cautious with YouTube downloads to avoid copyright issues
  3. Ensure compliance with messaging regulations in your region

Conclusion

This multi-purpose Jenkins job demonstrates the versatility of Jenkins beyond traditional CI/CD tasks. By combining various APIs and tools, we’ve created a powerful automation tool for communication and media handling.
As you become more comfortable with this setup, you can expand it to include other communication channels or media processing tasks, making it a central hub for your automation needs.
Remember, with great power comes great responsibility. Use this automation wisely and always respect privacy and copyright laws when handling communications and media content.

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

Learn more Creating a Multi-Purpose Jenkins Job: Emails, SMS, YouTube Downloads, and WhatsApp Messages

Leave a Reply