🤖 Automate Posts to Mastodon with GitHub Actions

Science & Design
3 min readNov 2, 2023

There’s a lot to do when you want to build and maintain software — and one of the most essential aspects is sharing what you’re up to with your community. But with so much going on, from working on code, writing blog posts, thinking about user acquisition strategies, or improving our app’s accessibility, social media can often go dormant for long periods.

The automation can solve problems for so many use cases, and here we’ll show you how to configure GitHub Actions to automatically post to Mastodon whenever a PR gets merged! This helps us keep people informed in real-time about progress on our software, making our community more informed and, in turn, safer.

Step 1: Mastodon

From your Mastodon account, go to Preferences > Development and click “New application.” Give it any name, for example, “Github PR Notifier.”

Modify the Scopes only to select write:statuses and crypto and click “Save Changes.”

The Development preferences screen

Once saved, click back into the application you just created. At the top of the screen, you’ll see “Your access token” — we’ll use this in the next step.

Step 2: GitHub

If you don’t have a GitHub account, you’ll need to create one. Go to or create the repository that you want to automate.

Store Your Secret

Go to Settings > Secrets and variables > Actions and click “New repository secret.” Name it MASTODON_ACCESS_TOKEN and paste your access token from step 1 into the Secret field.

The Actions secrets and variables home screen

Create an Action

Click “Add File” and create the file “.github/workflows

/post_to_mastodon.yml . Add the following content, and be sure to replace your-mastodon-instance-url with the URL of your Mastodon instance.

name: Post to Mastodon on PR Merge

on:
pull_request:
types:
- closed

jobs:
post_to_mastodon:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Post to Mastodon
run: |
curl -X POST -H "Authorization: Bearer ${{ secrets.MASTODON_ACCESS_TOKEN }}" \
-F "status=Pull request #${{ github.event.pull_request.number }} merged: ${{ github.event.pull_request.title }}" \
https://your-mastodon-instance-url/api/v1/statuses

Step 3: Test it!

From your repository, create a new branch. Make any change — a slight modification to your README, or a new file, for example. Next, click on the tab labeled “Pull requests.” You should see a “Compare & pull request” button. Click that, and then merge your PR.

Once merged, the PR will be closed, and your action should automatically post an update to your Mastodon account!

Hush Line’s test Mastodon account with an automated post from the GitHub PR

🎉 Congratulations! You’ve just gained time back to your life.

--

--

Science & Design

👋 We’re a non-profit design and software development organization. Let’s make something great together! https://scidsg.org