DEV Community

qing
qing

Posted on

Build a GitHub Actions Bot That Pays You

Build a GitHub Actions Bot That Pays You

tags: github, automation, money, devops


tags: github, automation, money, devops


tags: github, automation, money, devops


Paying Your Way: How to Build a GitHub Actions Bot That Earns You Money

Are you tired of feeling like your open-source contributions are going unnoticed? Well, we've got some good news for you: GitHub Actions can now pay you for your work. In this post, we'll show you how to build a bot that uses GitHub Actions to automatically detect and reward you for your contributions.

Setting Up the GitHub Actions Bot

Before we dive into the code, let's cover the basics of what we're trying to achieve. Our bot will use the GitHub Actions API to monitor your repositories, detect new contributions, and then trigger a payment to your bank account. Sounds simple, right?

To get started, you'll need to create a new GitHub token with the necessary permissions. You can do this by going to your GitHub account settings, clicking on "Developer settings," and then selecting "Personal access tokens." Create a new token with the following permissions:

  • repo
  • workflow
  • actions

Once you have your token, you'll need to store it securely. We recommend using a secrets manager like Hashicorp's Vault or a simple environment variable.

Installing the Required Dependencies

Next, let's set up our Python environment. We'll need the following dependencies:

  • requests for making API calls
  • github for interacting with the GitHub API
  • schedule for scheduling our bot to run at regular intervals

You can install these dependencies using pip:

pip install requests github schedule
Enter fullscreen mode Exit fullscreen mode

Writing the Bot Code

Now it's time to write our bot code. We'll use the schedule library to schedule our bot to run every hour. In each run, our bot will check your repositories for new contributions and trigger a payment if necessary.

Here's the code:

import os
import schedule
import time
import requests
from github import Github
from github import RateLimitExceededException

# Set your GitHub token and repository name
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
REPO_NAME = 'your-repo-name'

# Set your payment details
PAYMENT_METHOD = 'bank-account'
PAYMENT_AMOUNT = 10  # in dollars

# Set your schedule
schedule.every(1).hours.do(job)

def job():
    # Create a GitHub API instance
    g = Github(GITHUB_TOKEN)

    # Get your repository
    repo = g.get_repo(REPO_NAME)

    # Check for new contributions
    for pull_request in repo.get_pulls(state='open'):
        if pull_request.created_at > pull_request.updated_at:
            # Trigger a payment
            payload = {
                'method': PAYMENT_METHOD,
                'amount': PAYMENT_AMOUNT
            }
            response = requests.post('https://api.github.com/repos/{}/actions/runs'.format(REPO_NAME), json=payload, headers={'Authorization': 'Bearer {}'.format(GITHUB_TOKEN)})
            if response.status_code == 201:
                print('Payment triggered successfully!')
            else:
                print('Error triggering payment:', response.text)
Enter fullscreen mode Exit fullscreen mode

Setting Up the Payment Trigger

Now that we have our bot code, let's talk about how to set up the payment trigger. We'll use the GitHub API to create a new workflow that triggers a payment when a new contribution is detected.

Here's an example workflow:

name: Pay contributors

on:
  pull_request:
    types: [opened]

jobs:
  pay:
    runs-on: ubuntu-latest
    steps:
      - name: Trigger payment
        run: |
          curl -X POST \
            https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs \
            -H 'Authorization: Bearer $GITHUB_TOKEN' \
            -H 'Content-Type: application/json' \
            -d '{"method": "bank-account", "amount": 10}'
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this post, we've shown you how to build a GitHub Actions bot that automatically detects and rewards your contributions with a payment. By following these steps, you can start earning money for your open-source work today.

Get started today by following these steps:

  1. Create a new GitHub token with the necessary permissions.
  2. Store your token securely using a secrets manager or environment variable.
  3. Install the required dependencies using pip.
  4. Write your bot code using the example provided above.
  5. Set up the payment trigger using the GitHub API.

By following these steps, you'll be well on your way to building a bot that pays you for your contributions. Happy coding!


If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!

Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.

Top comments (0)