❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Idea Implementation #1 – Automating Daily Quiz Delivery to Telegram with GitHub Actions

25 December 2024 at 17:53

Telegram Group: https://t.me/parottasalna

Github Repo: https://github.com/syedjaferk/daily_quiz_sender

In this blog, I’ll walk you through the journey of transitioning from a PythonAnywhere server to GitHub Actions for automating the delivery of daily quizzes to a Telegram group https://t.me/parottasalna . This implementation highlights the benefits of GitHub Actions to run a cronjob.

Problem Statement

I wanted to send a daily quiz to a Telegram group to keep members engaged and learning. Initially, I hosted the solution on a PythonAnywhere server.

Some of the limitations are,

  1. Only one cron job is allowed for a free account.
  2. For every x days, i need to visit the dashboard and reactive my account to make it work.

Recently, started learning the Github Actions . So thought of leveraging the schedule mechanism in it for my usecase.

Key Features of My Solution

  • Automated Scheduling: GitHub Actions triggers the script daily at a specified time.
  • Secure Secrets Management: Sensitive information like Telegram bot tokens and chat IDs are stored securely using GitHub Secrets.
  • Serverless Architecture: No server maintenance; everything runs on GitHub’s infrastructure.

Implementation Overview

I am not explaining the code here. Please checkout the repo https://github.com/syedjaferk/daily_quiz_sender

The core components of the implementation include

  1. A Python script (populate_questions.py) to create questions and store each question in the respective directory.
  2. A Python script (runner.py) which takes telegram_bot_url, chat_id and category as input to read the correct question from the category directory and send the message.
  3. A GitHub Actions workflow to automate execution.

Workflow Yaml File


name: Docker Quiz Sender

on:
  schedule:
    - cron: "30 4 * * *"

  workflow_dispatch:

jobs:
  run-python:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: Run script with secrets0
        run: python runner.py --telegram_url ${{ secrets.TELEGRAM_BOT_URL }} --chat_id ${{ secrets.CHAT_ID }} --category docker

Benefits of the New Implementation

  1. Cost-Effectiveness: No server costs thanks to GitHub’s free tier for public repositories.
  2. Reliability: GitHub Actions ensures the script runs on schedule without manual intervention.
  3. Security: GitHub Secrets securely manages sensitive credentials.
  4. Centralization: All code, configuration, and automation reside in a single repository.

Lessons Learned

  • Environment Variables: Using environment variables for secrets keeps the code clean and secure.
  • Error Handling: Adding error-handling mechanisms in the script helps debug issues effectively.
  • Time Zones: GitHub Actions uses UTC; be mindful when scheduling tasks for specific time zones.

Future Enhancements

  1. Dynamic Quizzes: Automate quiz generation from a database (sqlite3) or external API.
  2. Logging: Store logs as GitHub Actions artifacts for better debugging and tracking.
  3. Failure Notifications: Notify via Telegram or email if the workflow fails.
  4. Extend for other categories

You can check out the project here. Feel free to fork it and adapt it to your needs!

Learning Notes #01 – Github Actions

19 December 2024 at 16:31

GitHub Actions is a CI/CD tool that automates workflows directly from your GitHub repository. I am writing this notes for my future self.

Github: https://github.com/syedjaferk/gh_action_docker_build_push_fastapi_app

0. Github Actions

Github Actions is a managed CI/CD pipeline offering. It provides free runners for running the code.

1. Workflow, Jobs, Steps

A workflow is a collection of jobs defined in a .yml file inside .github/workflows. Each workflow consists of jobs, and jobs have steps.


name: My First Workflow
on: push
jobs:
  example-job:
    runs-on: ubuntu-latest
    steps:
      - name: Print a message
        run: echo "Hello, GitHub Actions!"

2. Availability and Pricing

GitHub Actions is free for public repositories and has free usage limits for private repositories, depending on the plan. Paid plans increase these limits. For detailed pricing, visit the GitHub Actions pricing page.

3. First workflow with basic echo commands

Start with a workflow triggered by any push event. Here’s a simple example,


name: Echo Workflow
on: push
jobs:
  echo-job:
    runs-on: ubuntu-latest
    steps:
      - name: Say Hello
        run: echo "Hello from my first workflow!"

4. Multiline Shell Commands

You can use the | symbol to write multiline shell commands.


name: Push Event Workflow
on:
  push:
    branches:
      - main
jobs:
  push-job:
    runs-on: ubuntu-latest
    steps:
      - name: On Push
        run: echo "Code pushed to main branch!"

5. A new workflow with push events

Workflows can be triggered by specific events like push. This example triggers only on push to the main branch.


name: Push Event Workflow
on:
  push:
    branches:
      - main
jobs:
  push-job:
    runs-on: ubuntu-latest
    steps:
      - name: On Push
        run: echo "Code pushed to main branch!"

6. Using actions in workflow (Marketplace and Open Source)

GitHub Actions Marketplace offers reusable actions. For example, using the actions/checkout action.


steps:
  - name: Checkout Code
    uses: actions/checkout@v3

7. Checkout in code

Checking out your code is essential to access the repository content. Use actions/checkout


steps:
  - name: Checkout Code
    uses: actions/checkout@v3

8. Adding workflow job steps

Each job can have multiple steps.


jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1 - Checkout Code
        uses: actions/checkout@v3
      - name: Step 2 - Run a Script
        run: echo "Script running in step 2."

9. Jobs in Parallel and Sequential

To run jobs in parallel, define them without dependencies. For sequential jobs, use needs.

Parallel Jobs:

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Job 1"

  job2:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Job 2"

Sequential Jobs:


jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Job 1"

  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - run: echo "Job 2 after Job 1"

10. Using Multiple Triggers

A workflow can be triggered by multiple events.


on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

11. Expressions and Context Objects

Expressions are used to access variables and contexts like github or env.


steps:
  - name: Use an Expression
    run: echo "Triggered by ${{ github.event_name }}"

❌
❌