pushbag
Developer blog

Send push notifications from GitHub Actions and cron with curl

6 min read

Published · Updated

POST a JSON object to https://ping.pushbag.dev/ with curl and a Notifications key to publish to your personal channel. A successful request returns HTTP 202. It means pushbag accepted the message, which is enough for a CI job to stop retrying. The phone may still be offline or the channel muted. Test from the machine that runs the job first:

curl --fail-with-body -sS https://ping.pushbag.dev/ \
  -H "Authorization: Bearer $PUSHBAG_API_KEY" \
  -H 'Content-Type: application/json' \
  --data-binary '{"title":"Build","message":"Ready for review","priority":3}'

The response contains id, channelId, and receipt. Keep the message ID with the build log if you need to match a publish to an entry in the channel. To observe the status along with the body during setup, add --write-out '\nHTTP %{http_code}\n' to the command. Remove that option before piping the body to a JSON parser. curl command reference

Give the workflow a reproducible test

Save the following as .github/workflows/notify.yml and configure the Actions secret PUSHBAG_API_KEY for the repository. This complete workflow sends a test notification when you run it manually from Actions. It doesn’t assume any particular build system, require a checkout, or grant the repository token permissions. The URL points to the workflow run, giving the person who receives it a way back to the evidence.

name: Test phone notification
on: workflow_dispatch
permissions: {}
jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - name: Publish run link
        env:
          PUSHBAG_API_KEY: ${{ secrets.PUSHBAG_API_KEY }}
        run: |
          set -eu
          : "${PUSHBAG_API_KEY:?Configure the PUSHBAG_API_KEY secret}"
          jq -n --arg link "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" \
            '{title:"CI", message:"Notification test completed", click:$link, priority:3}' > message.json
          curl --fail-with-body -sS https://ping.pushbag.dev/ \
            -H "Authorization: Bearer $PUSHBAG_API_KEY" \
            -H 'Content-Type: application/json' \
            -H "Idempotency-Key: test-$GITHUB_REPOSITORY_ID-$GITHUB_RUN_ID" \
            --data-binary @message.json

The step-scoped environment exposes the secret to the publishing step. jq encodes the URL as a JSON string, so you can later substitute other text fields without hand-escaping quotes. Once the test succeeds, move the step after the operation you want to report and change its wording to describe that operation. Put a failure notification in a step with an appropriate failure condition and a separate key suffix. Otherwise two different outcomes can collide under the same key. Workflow syntax · Actions secrets · Runner variables

Choose the fields the receiver needs

Field Value and use
message Required nonempty text. Say what finished and which run needs attention.
title Optional short label such as Backup or CI.
priority Integer from 1 through 5. The default is 3. A receiver may filter lower priorities.
tags, markdown Optional array of text labels and a boolean selecting Markdown formatting.
click, icon Destination opened on tap and an HTTP or HTTPS icon URL. A click destination may also be a supported app link.
actions Array of view or copy actions. Supply a label and either the destination URL or the value to copy.
data JSON object for structured details about the job.
artifactVersionId ID of an accessible artifact version to associate with this notification.

For a dedicated operations channel, post to https://ping.pushbag.dev/CHANNEL_ID with the real channel ID. Omitting the channel selects the authenticated publisher’s personal channel. A key scoped to a shared channel still needs that explicit destination. Use priority 3 for a routine completion, and agree with the people receiving the channel on which failures merit 4 or 5. Priority alone cannot ensure that a particular phone will sound.

Reuse the request in a scheduled job

Save this function in /opt/jobs/pushbag.sh for scripts run by the backup account. It accepts a stable event ID, message text, and an optional priority. Set PUSHBAG_PING_URL for a channel other than the default. The function constructs the payload before calling curl, so a JSON encoding failure cannot send an empty body.

publish_backup() (
  set -eu
  payload=$(jq -n --arg message "$2" --argjson priority "${3:-3}" \
    '{title:"Backup", message:$message, priority:$priority}')
  curl --fail-with-body -sS "${PUSHBAG_PING_URL:-https://ping.pushbag.dev/}" \
    -H "Authorization: Bearer $PUSHBAG_API_KEY" \
    -H 'Content-Type: application/json' \
    -H "Idempotency-Key: $1" --data-binary "$payload"
)

A user crontab entry for an executable wrapper might read:

15 2 * * * /opt/jobs/backup-and-notify

Have that wrapper set the path for jq and curl, load the key from the backup account’s secret store, and source /opt/jobs/pushbag.sh. After a successful backup, call publish_backup "backup-$BACKUP_ID" "Backup $BACKUP_ID completed", where the ID comes from the backup itself. On failure, preserve the backup command’s exit status when the wrapper exits. Otherwise a successful alert about a failed backup can make the scheduler think the backup succeeded. User crontab format

Verify a replay without sending a second alert

Run the manual workflow once, record the message ID from the response body, then use GitHub Actions to rerun the same workflow run. Keep its message and URL unchanged. Compare the two response IDs and check how many entries appeared in the channel. Starting another manual workflow dispatch creates a different run ID and intentionally publishes another entry. This distinction is worth testing before wiring the command into a noisy build matrix.

For a cron integration, do the same exercise with a saved backup identifier in a test channel. Reserve one identifier for that backup through all delivery attempts, including attempts made by an operator after the scheduler exits. If the script generates an identifier from the current time each time it starts, an otherwise identical retry looks like a new event.

Decide whether a retry is safe

A connection failure after sending the request leaves the caller uncertain. Retry the same logical event with its original payload and idempotency key. A matching replay on the channel returns the original receipt. Changing the message under the same key produces HTTP 409 with idempotency_conflict. Keep keys at or below 128 characters. For matrix jobs, include the matrix entry in the identity if each result deserves its own alert.

If the service returns HTTP 400 with The body is not valid JSON., inspect the generated payload before retrying. HTTP 415 with Send text/plain or application/json. points to the content type. HTTP 401 with Invalid or revoked credential. points to the secret. For HTTP 403, verify the key can publish to the named channel. HTTP 429 covers both throttling and exhausted quota. Check the returned error code before deciding when another attempt makes sense. HTTP API · CI and script notifications · Comparing notification services

Frequently asked questions

Does HTTP 202 mean someone saw the notification?

It means the service accepted the message. Inspect the channel and receiver separately to diagnose delivery.

Can two matrix jobs share an idempotency key?

Give jobs separate keys if both should publish. Reusing a key on the same channel can suppress a duplicate or produce a conflict.

Where should cron get the publish credential?

Load it in the job from a file readable only by the job account or from the job's secret store. Keep it out of the crontab line.