To set up a Jenkins job for sending emails, SMS, downloading YouTube videos, and sending WhatsApp messages, you’ll need to use a combination of plugins, external APIs, and scripts. Here’s a breakdown of how you can configure each action:
Prerequisites
- Jenkins Plugins: Install the Email Extension Plugin to send emails from Jenkins.
- Third-Party APIs:
- Twilio API for SMS and WhatsApp messaging.
- YouTube-DL or yt-dlp for downloading YouTube videos.
3. API Credentials: Obtain Twilio API keys and YouTube API keys (if applicable).
4. System Setup: Ensure Jenkins has access to install packages like yt-dlp
for downloading YouTube videos.
Step 1: Send Email
To send emails, you can either use the Jenkins Email Extension Plugin or write a custom script with mail
or sendmail
.
- Configure Jenkins Email Settings:
- Go to Manage Jenkins > Configure System > Extended E-mail Notification.
- Add SMTP details and credentials.
2. Jenkins Pipeline Script:
stage('Send Email') { steps { mail to: '[email protected]', subject: "Automated Jenkins Notification", body: "This is a test email from the Jenkins job." } }
Step 2: Send SMS Using Twilio
Twilio allows sending SMS and WhatsApp messages with a simple REST API.
- Create Twilio Account:
- Register and get your
ACCOUNT SID
,AUTH TOKEN
, and a Twilio phone number.
2. Install Twilio CLI (optional for local testing):
pip install twilio
Pipeline Script for SMS:
stage('Send SMS') { steps { script { def accountSid = 'YOUR_TWILIO_ACCOUNT_SID' def authToken = 'YOUR_TWILIO_AUTH_TOKEN' def fromNumber = 'YOUR_TWILIO_PHONE_NUMBER' def toNumber = 'RECIPIENT_PHONE_NUMBER' def message = 'This is a test SMS from Jenkins' sh """ curl -X POST "https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json" \ --data-urlencode "Body=${message}" \ --data-urlencode "From=${fromNumber}" \ --data-urlencode "To=${toNumber}" \ -u "${accountSid}:${authToken}" """ } } }
Step 3: Download YouTube Video
For downloading YouTube videos, use yt-dlp (a more maintained fork of youtube-dl).
- Install yt-dlp:
sudo apt update sudo apt install yt-dlp -y
Pipeline Script to Download YouTube Video:
stage('Download YouTube Video') { steps { script { def videoURL = 'https://www.youtube.com/watch?v=VIDEO_ID' def outputPath = '/path/to/save/video.mp4' sh """ yt-dlp -o ${outputPath} ${videoURL} """ } } }
- Note: Replace
VIDEO_ID
with the actual ID of the YouTube video.
Step 4: Send WhatsApp Message Using Twilio
Twilio can also send WhatsApp messages. Ensure that you have a WhatsApp-enabled Twilio number.
- Pipeline Script for WhatsApp:
stage('Send WhatsApp Message') { steps { script { def accountSid = 'YOUR_TWILIO_ACCOUNT_SID' def authToken = 'YOUR_TWILIO_AUTH_TOKEN' def fromNumber = 'whatsapp:+YOUR_TWILIO_WHATSAPP_NUMBER' def toNumber = 'whatsapp:+RECIPIENT_WHATSAPP_NUMBER' def message = 'This is a test WhatsApp message from Jenkins' sh """ curl -X POST "https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json" \ --data-urlencode "Body=${message}" \ --data-urlencode "From=${fromNumber}" \ --data-urlencode "To=${toNumber}" \ -u "${accountSid}:${authToken}" """ } } }
Full Jenkins Pipeline Example
Here’s how the full pipeline might look, integrating all the stages:
pipeline {
agent any
stages {
stage('Send Email') {
steps {
mail to: '[email protected]',
subject: "Automated Jenkins Notification",
body: "This is a test email from the Jenkins job."
}
}
stage('Send SMS') {
steps {
script {
def accountSid = 'YOUR_TWILIO_ACCOUNT_SID'
def authToken = 'YOUR_TWILIO_AUTH_TOKEN'
def fromNumber = 'YOUR_TWILIO_PHONE_NUMBER'
def toNumber = 'RECIPIENT_PHONE_NUMBER'
def message = 'This is a test SMS from Jenkins'
sh """
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json" \
--data-urlencode "Body=${message}" \
--data-urlencode "From=${fromNumber}" \
--data-urlencode "To=${toNumber}" \
-u "${accountSid}:${authToken}"
"""
}
}
}
stage('Download YouTube Video') {
steps {
script {
def videoURL = 'https://www.youtube.com/watch?v=VIDEO_ID'
def outputPath = '/path/to/save/video.mp4' sh """
yt-dlp -o ${outputPath} ${videoURL}
"""
}
}
}
stage('Send WhatsApp Message') {
steps {
script {
def accountSid = 'YOUR_TWILIO_ACCOUNT_SID'
def authToken = 'YOUR_TWILIO_AUTH_TOKEN'
def fromNumber = 'whatsapp:+YOUR_TWILIO_WHATSAPP_NUMBER'
def toNumber = 'whatsapp:+RECIPIENT_WHATSAPP_NUMBER'
def message = 'This is a test WhatsApp message from Jenkins' sh """
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json" \
--data-urlencode "Body=${message}" \
--data-urlencode "From=${fromNumber}" \
--data-urlencode "To=${toNumber}" \
-u "${accountSid}:${authToken}"
"""
}
}
}
}
}
Summary
- Send Email: Configure Jenkins email or use
mail
command. - Send SMS: Use Twilio’s REST API with Jenkins shell command.
- Download YouTube Video: Use
yt-dlp
to download the video from a provided URL. - Send WhatsApp Message: Use Twilio’s REST API to send a message to a WhatsApp number.
This pipeline will automate each of these tasks, allowing you to schedule them or trigger them as needed.