Automating Daily Blog Posts with GitHub Actions and Blogger API

In this blog post, we'll explore a GitHub Actions workflow that automates the creation of daily blog posts using the Blogger API. This workflow allows you to schedule and publish blog posts at a specified time every day.

GitHub Actions Workflow

Let's dive into the GitHub Actions workflow defined in the .github/workflows/daily-automation.yml file.

name: Daily Automated Actions
on:
  schedule:
    - cron: '0 14 * * *'
  push:
    branches:
      - main
jobs:
  daily-run:
    name: 'Runs daily'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
      - run: git checkout HEAD^2
        if: ${{ github.event_name == 'pull_request' }}
      - name: Setup Python version 3.9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Automate blog posts
        run: |
          python run.py --credentials '${{ secrets.credentials }}' --json '${{ secrets.json }}' --repo '${{ secrets.repo }}'

Workflow Steps:

  1. Checkout Repository: The action checks out the repository code.
  2. Python Setup: It sets up the Python environment with version 3.9.
  3. Install Dependencies: Installs the required dependencies defined in requirements.txt.
  4. Automate Blog Posts: Runs the run.py script to automate the creation of blog posts using the specified credentials, JSON file, and GitHub repository.

Python Script (run.py)

Now, let's explore the Python script responsible for interacting with the Blogger API and creating blog posts.

# The content of run.py
# ...
# Example usage
if __name__ == '__main__':
    args = parse_args()
    # Use the contents of the credentials file as a string
    credentials_str = args.credentials
    # Initialize the Blogger API client
    blogger_service = get_blogger_service(credentials_str)
    # Get the Blog ID
    blog_id = get_blog_id(blogger_service)
    if blog_id is None:
        print('Unable to retrieve Blog ID. Exiting.')
        exit(1)
    # Read blog post information from JSON file on GitHub
    github_repo = args.github_repo
    json_file_path = args.json_file
    blog_posts = read_json_file_from_github(github_repo, json_file_path)
    if blog_posts is not None and 'posts' in blog_posts:
        for post in blog_posts['posts']:
            # ...
            # Process and create blog posts based on the script logic
            # ...

Key Script Components:

  • Parsing Arguments: The script parses command-line arguments, including credentials, GitHub repository URL, and JSON file path.
  • Blogger API Initialization: It initializes the Blogger API client using the provided credentials.
  • Reading Blog Post Information: The script reads blog post information from a JSON file in the specified GitHub repository.
  • Automating Post Creation: Based on the scheduled date and time, the script automates the creation of blog posts using the Blogger API.

Conclusion

With this GitHub Actions workflow and Python script, you can effortlessly schedule and automate your daily blog posts, streamlining your content creation process. Customize the workflow and script to fit your specific requirements and enjoy a more efficient and consistent blogging experience.

Comments