Create a Jenkins job to send emails, SMS, download YouTube videos, and send WhatsApp messages.

To create a Jenkins job that sends emails, SMS, downloads YouTube videos, and sends WhatsApp messages, you can use various tools and libraries integrated within a Jenkins pipeline. Below, I'll provide a step-by-step guide to set up this Jenkins job, including the necessary scripts and tools.

Prerequisites

  1. Jenkins Installed: Ensure Jenkins is installed and running.

  2. Python Installed: Have Python installed on your Jenkins node (you can install Python libraries using pip).

  3. API Keys: Obtain the necessary API keys for sending SMS and WhatsApp messages. For example:

    • Twilio for SMS.

    • WhatsApp Business API or a service like Twilio for WhatsApp messages.

  4. YouTube Download Tool: Use youtube-dl or yt-dlp for downloading videos.

Step-by-Step Guide

Step 1: Install Required Python Packages

Before setting up the Jenkins job, install the following Python packages on your Jenkins node:

pip install requests twilio youtube-dl

You can use yt-dlp instead of youtube-dl for better performance and support:

pip install yt-dlp

Step 2: Create a New Pipeline Job

  1. Open your Jenkins dashboard.

  2. Click on "New Item".

  3. Enter a name for your job (e.g., Send Notifications and Download Videos).

  4. Select "Pipeline" and click OK.

Step 3: Configure the Pipeline Job

  1. In the job configuration page, scroll down to the "Pipeline" section.

  2. Choose "Pipeline script" from the dropdown.

Step 4: Add the Pipeline Script

In the "Pipeline" script text area, add the following script:

pipeline {
    agent any

    environment {
        TWILIO_ACCOUNT_SID = credentials('twilio-account-sid')   // Jenkins credentials ID for Twilio Account SID
        TWILIO_AUTH_TOKEN = credentials('twilio-auth-token')       // Jenkins credentials ID for Twilio Auth Token
        TWILIO_PHONE_NUMBER = 'your_twilio_phone_number'          // Your Twilio phone number
        RECIPIENT_PHONE_NUMBER = 'recipient_phone_number'          // Recipient phone number for SMS
        WHATSAPP_NUMBER = 'whatsapp:+recipient_whatsapp_number'    // WhatsApp recipient number
        YOUTUBE_VIDEO_URL = 'https://www.youtube.com/watch?v=your_video_id' // YouTube video URL
        EMAIL_SENDER = 'your_email@example.com'                    // Your email address
        EMAIL_RECIPIENT = 'recipient_email@example.com'            // Recipient email address
        SMTP_SERVER = 'smtp.example.com'                           // SMTP server address
        SMTP_PORT = '587'                                          // SMTP server port
        SMTP_USER = 'your_email@example.com'                       // SMTP username
        SMTP_PASSWORD = credentials('smtp-password')               // Jenkins credentials ID for SMTP password
    }

    stages {
        stage('Send Email') {
            steps {
                script {
                    def emailBody = 'This is a test email sent from Jenkins!'
                    sh """
                    python -c "
import smtplib
from email.mime.text import MIMEText

msg = MIMEText('${emailBody}')
msg['Subject'] = 'Jenkins Notification'
msg['From'] = '${EMAIL_SENDER}'
msg['To'] = '${EMAIL_RECIPIENT}'

with smtplib.SMTP('${SMTP_SERVER}', ${SMTP_PORT}) as server:
    server.starttls()
    server.login('${SMTP_USER}', '${SMTP_PASSWORD}')
    server.send_message(msg)
                    "
                    """
                }
            }
        }

        stage('Send SMS') {
            steps {
                script {
                    sh """
                    python -c "
from twilio.rest import Client

client = Client('${TWILIO_ACCOUNT_SID}', '${TWILIO_AUTH_TOKEN}')
message = client.messages.create(
    body='This is a test SMS sent from Jenkins!',
    from_='${TWILIO_PHONE_NUMBER}',
    to='${RECIPIENT_PHONE_NUMBER}'
)
                    "
                    """
                }
            }
        }

        stage('Download YouTube Video') {
            steps {
                script {
                    sh """
                    yt-dlp -o 'downloads/%(title)s.%(ext)s' '${YOUTUBE_VIDEO_URL}'
                    """
                }
            }
        }

        stage('Send WhatsApp Message') {
            steps {
                script {
                    sh """
                    python -c "
from twilio.rest import Client

client = Client('${TWILIO_ACCOUNT_SID}', '${TWILIO_AUTH_TOKEN}')
message = client.messages.create(
    body='This is a test WhatsApp message sent from Jenkins!',
    from_='whatsapp:${TWILIO_PHONE_NUMBER}',
    to='${WHATSAPP_NUMBER}'
)
                    "
                    """
                }
            }
        }
    }
}

Explanation of the Pipeline Script

  1. Environment Variables: The environment section defines credentials and other configuration settings. Make sure to replace placeholders with your actual values and configure Jenkins credentials for sensitive information.

  2. Send Email Stage: Uses Python's smtplib to send an email.

  3. Send SMS Stage: Uses the Twilio Python library to send an SMS.

  4. Download YouTube Video Stage: Uses yt-dlp to download a YouTube video to a specified directory.

  5. Send WhatsApp Message Stage: Uses the Twilio Python library to send a WhatsApp message.

Step 5: Save and Run the Job

  1. Click "Save" at the bottom of the job configuration page.

  2. Go back to the job's main page and click "Build Now" to execute the job.

Additional Considerations

  • Permissions: Ensure that the Jenkins user has the necessary permissions to execute Python scripts and has access to the network for sending messages and downloading videos.

  • Error Handling: Consider adding error handling in your Python scripts to manage potential failures.

  • Logging: You may want to log outputs from the commands for debugging purposes.

Conclusion

By following these steps, you have successfully created a Jenkins job that can send emails, SMS, download YouTube videos, and send WhatsApp messages. This setup can be customized further to fit your specific needs.