DEV Community

John  Ajera
John Ajera

Posted on • Edited on

Automating Mastodon Posts with GitHub Actions

Configure GitHub for Mastodon Publishing

1. Prepare Your Mastodon Account

Generate a Mastodon API Key

  • Go to your Mastodon account.
  • Navigate to Preferences > Development.
  • Create a new application:
    • Application name: GitHub-Integration
    • Scopes: profile (default), write:statuses
  • Click Submit.
  • Click the newly created GitHub-Integration application.
  • Navigate and note Your access token.

2. Create a GitHub Repository

Create a New Repository

  • Go to GitHub and create a new repository.
  • Give it a meaningful name (e.g., blog).

Set Up the Repository Structure

  • Clone the repository to your local machine:
  git clone https://github.com/<your-username>/<repository-name>.git
  cd <repository-name>
Enter fullscreen mode Exit fullscreen mode

It's always a best practice to create a branch and work from it.

  git branch <branch-name> initial-build
  git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

After creating the folder, the repository structure should look like this:

  .
  ├── articles
  ├── LICENSE
  └── README.md
Enter fullscreen mode Exit fullscreen mode

3. Add Your Mastodon API Key to GitHub Secrets

Go to Repository Settings

  • Navigate to "Settings" > "Secrets and variables" > "Actions" > "New repository secret".

Add a Secret

  • Name the secret "MASTODON_TOKEN".
  • Paste the "Mastodon" API key from the previous step.

  • Set up the folder structure for GitHub Actions workflows:

  mkdir -p .github/workflows
  touch .github/workflows/mastodon-publisher.yml
Enter fullscreen mode Exit fullscreen mode
  .
  ├── .github
  │   └── workflows
  │       └── mastodon-publisher.yml
  ├── articles
  ├── LICENSE
  └── README.md
Enter fullscreen mode Exit fullscreen mode

Configure mastodon-publisher.yml

  • Update the "mastodon-publisher.yml" file with the following contents:
  name: Mastodon Publisher

  on:
    push:
      branches:
        - "**"

  jobs:
    publish:
      runs-on: ubuntu-latest

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

        - name: Mastodon Post on GitHub Actions
          id: mastodon_toot
          uses: cbrgm/mastodon-github-action@v2
          with:
            access-token: ${{ secrets.MASTODON_TOKEN }}
            url: 'https://mastodon.social'
            message: "Hello from GitHub Actions!"

        - name: Get toot information
          run: |
            echo "Toot ID: ${{ steps.mastodon_toot.outputs.id }}"
            echo "Toot URL: ${{ steps.mastodon_toot.outputs.url }}"
            echo "Scheduled at: ${{ steps.mastodon_toot.outputs.scheduled_at }}"
Enter fullscreen mode Exit fullscreen mode

Summary

  • The "mastodon-publisher.yml" workflow automates publishing Markdown articles located in the articles directory to your Mastodon account.

5. Push the branch to GitHub

  • Execute
git add --all
git commit -m "feat: add GitHub Actions workflow for publishing to Mastodon" \
  -m "Configure automated publishing workflow."
git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

6. Create a Pull Request (PR)

Go to Your Repository in GitHub

  • Visit your repository on GitHub.

Navigate to the "Pull Requests" Tab

  • Click on the "Pull Requests" tab at the top of the repository page.

Click "New Pull Request"

  • Select the "New Pull Request" button.

Select Branches

  • Base branch: Choose the branch you want to merge into (e.g., main or production).
  • Compare branch: Select the <branch-name> branch.

Review Changes

  • Review the list of commits and the files changed to ensure they are correct.

Add a Title and Description

  • Provide a concise and descriptive title (e.g., feat: Add GitHub Actions for Mastodon publishing).
  • Add any necessary details in the description box (e.g., reasons for changes, references to issues, etc.).

Review and Approve the PR

  • Self-Review or Request Reviews.

Merge the Pull Request

  • Navigate to the PR you just created.
  • Click "Merge Pull Request".
  • Click "Confirm merge".

7. Test and Validate the Workflow

  • The workflow will trigger automatically once a push or merge is made. You can apply conditional logic to refine when it should run, ensuring it only triggers for specific events or branches.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs