{"id":73828,"date":"2025-08-05T11:23:41","date_gmt":"2025-08-05T05:53:41","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=73828"},"modified":"2025-08-13T18:33:10","modified_gmt":"2025-08-13T13:03:10","slug":"automating-cron-jobs-email-alerts-with-github-actions-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/automating-cron-jobs-email-alerts-with-github-actions-javascript\/","title":{"rendered":"Automating Cron Jobs &amp; Email Alerts with GitHub Actions + JavaScript"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>You probably already know how powerful GitHub Workflows are. They can transform the way you consume software and significantly boost your productivity. With them, you can automate almost anything.<\/p>\n<p>If you\u2019re new to GitHub Actions or want to understand them in depth, I highly recommend Sudarshan\u2019s excellent post, \u201c<a href=\"https:\/\/www.tothenew.com\/blog\/github-actions-for-seamless-ci-cd\/\">GitHub Actions for Seamless CI\/CD<\/a>,\u201d which also covers the fundamentals for beginners.<\/p>\n<p>Have you ever wanted an assistant that gives you your daily schedule, collects all the relevant data on your behalf, and creates an action plan for the day? If so, <a href=\"https:\/\/docs.github.com\/en\/actions\/get-started\/understand-github-actions\">GitHub Actions can help automate most of that process<\/a>.<\/p>\n<p>In this blog post, we\u2019ll build a scheduled GitHub Action (a.k.a. a cron job) that runs JavaScript code and emails you the weather report for the day \u2014 and all you need is a free GitHub repository to get started.<\/p>\n<h2>What we will build<\/h2>\n<ul>\n<li>A GitHub Action that runs on a schedule (e.g., every day at 8 AM IST)<\/li>\n<li>A Node.js script that fetches weather data from <a href=\"https:\/\/api.open-meteo.com\/v1\/forecast?latitude=28.6139&amp;longitude=77.2090&amp;hourly=temperature_2m\">Open-Meteo API<\/a><\/li>\n<li>An email sender using Gmail SMTP (check this guide to create SMTP credentials)<\/li>\n<li>Secrets management to securely store email credentials<\/li>\n<\/ul>\n<h3>Folder Structure<\/h3>\n<div id=\"attachment_73823\" style=\"width: 822px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-73823\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73823 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/folder-structure.png\" alt=\"folder-structure\" width=\"812\" height=\"441\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/folder-structure.png 812w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/folder-structure-300x163.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/folder-structure-768x417.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/folder-structure-624x339.png 624w\" sizes=\"(max-width: 812px) 100vw, 812px\" \/><p id=\"caption-attachment-73823\" class=\"wp-caption-text\">Folder structure for GitHub workflows<\/p><\/div>\n<p>You can follow this simple structure or create your own for advanced use cases. Here, we\u2019ve kept things simple and modular. You can easily add more workflows to this same repository in the future for different use cases.<\/p>\n<h3>GitHub Workflow: daily-report.yml<\/h3>\n<div id=\"attachment_73825\" style=\"width: 910px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-73825\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73825\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report-1024x821.png\" alt=\"workflows-daily-report\" width=\"900\" height=\"721\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report-1024x821.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report-300x240.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report-768x616.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report-624x500.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-daily-report.png 1502w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><p id=\"caption-attachment-73825\" class=\"wp-caption-text\">Code for daily report GitHub workflow<\/p><\/div>\n<pre>on:\r\n  schedule:\r\n    - cron: '0 3 * * *'<\/pre>\n<p>This is how you set the timer for your workflow execution. The time is in UTC, and you can adjust it to run at your preferred time. You can also add multiple cron expressions if you want to schedule the workflow to run multiple times a day. For example:<\/p>\n<pre>on:\r\n  schedule:\r\n    - cron: '30 2 * * *' # 08:00 AM IST\r\n    - cron: '30 8 * * *' # 02:00 PM IST\r\n    - cron: '30 14 * * *' # 08:00 PM IST<\/pre>\n<p>The script initializes a virtual environment on an Ubuntu-based machine with Node.js 22. It then installs your NPM dependencies and runs the script. In our case, we don&#8217;t have any packages to install, so we can skip the dependency installation step.<\/p>\n<h3>The Script: sendReport.js<\/h3>\n<div id=\"attachment_73826\" style=\"width: 910px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-73826\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73826\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-815x1024.png\" alt=\"workflows-send-daily-report\" width=\"900\" height=\"1131\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-815x1024.png 815w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-239x300.png 239w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-768x965.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-1222x1536.png 1222w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report-624x784.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/workflows-send-daily-report.png 1502w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><p id=\"caption-attachment-73826\" class=\"wp-caption-text\">Code to fetch weather report and send email.<\/p><\/div>\n<p>This script fetches hourly temperature data from the Open-Meteo API and selects the 8 AM temperature for the report. You can customize and enhance it to include the full day&#8217;s data or the day&#8217;s high and low temperatures. The script is quick, lightweight, and runs on GitHub infrastructure \u2014 so you don\u2019t have to pay anything.<\/p>\n<h3>Final Steps &amp; Setting Secrets in GitHub<\/h3>\n<p>Now we&#8217;re ready to test the script. Create a new repository on your GitHub account, push the code, and prepare it for testing.<\/p>\n<p>To send emails, you&#8217;ll need to set secrets in GitHub. These secrets will be injected as environment variables, which the script will read to authenticate and send the email using the proper credentials.<\/p>\n<p>To keep your credentials safe:<\/p>\n<ol>\n<li>Go to your repo \u2192 Settings \u2192 Secrets and variables \u2192 Actions.<\/li>\n<li>Add the following secrets:<br \/>\nEMAIL_USER \u2013 Your Gmail address<br \/>\nEMAIL_PASS \u2013 Your Gmail App Password [<a href=\"https:\/\/myaccount.google.com\/apppasswords\">Link<\/a>]<\/li>\n<\/ol>\n<p>Now that you&#8217;ve set the secrets, it&#8217;s time to manually test the workflow.<br \/>\nNavigate to your repository \u2192 Actions \u2192 Daily Weather Report, and click &#8220;Run workflow&#8221;.<\/p>\n<p>Refer to the attached screenshot for guidance.<\/p>\n<div id=\"attachment_73827\" style=\"width: 910px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-73827\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73827\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow-1024x233.png\" alt=\"run-workflow\" width=\"900\" height=\"205\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow-1024x233.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow-300x68.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow-768x175.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow-624x142.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/run-workflow.png 1413w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><p id=\"caption-attachment-73827\" class=\"wp-caption-text\">Manually run workflow from GitHub actions page.<\/p><\/div>\n<h3>What Else Can You Automate?<\/h3>\n<p>Here are some cool ideas to boost your productivity by automating daily tasks:<\/p>\n<ul>\n<li>Daily stock price reports with Google Sheets integration.<\/li>\n<li>Top 5 Hacker News or Reddit posts from your favorite niche.<\/li>\n<li>Reading your calendar and sending daily summaries and reminders for important meetings.<\/li>\n<li>Breaking news alerts that might be important for you.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>GitHub Actions + JavaScript is a powerful combo \u2014 and best of all, it\u2019s free for most use cases. For private repositories, there is a monthly usage limit, which you can verify on GitHub. The good news is that it\u2019s free to use for public repositories.<\/p>\n<p>However, the guarantee for running scheduled workflows (cron jobs) exactly on the specified time is limited on free accounts. I have noticed occasional delays in receiving email notifications, but there\u2019s a simple workaround: schedule your cron jobs a bit earlier than needed.<\/p>\n<p>To be honest, the sky\u2019s the limit for automation with GitHub, and you should start offloading daily tasks to these automation scripts. Integration with OpenAI and other large language models opens doors to exciting new possibilities.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction You probably already know how powerful GitHub Workflows are. They can transform the way you consume software and significantly boost your productivity. With them, you can automate almost anything. If you\u2019re new to GitHub Actions or want to understand them in depth, I highly recommend Sudarshan\u2019s excellent post, \u201cGitHub Actions for Seamless CI\/CD,\u201d which [&hellip;]<\/p>\n","protected":false},"author":1722,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":201},"categories":[5876],"tags":[7739,1853,4252,4561,503,55,693,2076],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73828"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1722"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=73828"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73828\/revisions"}],"predecessor-version":[{"id":73891,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73828\/revisions\/73891"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=73828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=73828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=73828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}