Skip to content

Fix X publish workflow (rotate refresh token) #5

Fix X publish workflow (rotate refresh token)

Fix X publish workflow (rotate refresh token) #5

Workflow file for this run

name: Post landed commits to X
on:
push:
branches:
- main
permissions:
actions: write
contents: read
jobs:
post:
runs-on: ubuntu-latest
steps:
- name: Refresh X OAuth token
id: x
env:
GH_TOKEN: ${{ github.token }}
run: |
RESPONSE=$(curl -s -X POST "https://api.twitter.com/2/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "${{ secrets.X_CLIENT_ID }}:${{ secrets.X_CLIENT_SECRET }}" \
--data-urlencode "grant_type=refresh_token" \
--data-urlencode "refresh_token=${{ vars.X_REFRESH_TOKEN }}")
ACCESS_TOKEN=$(echo "$RESPONSE" | jq -r '.access_token')
NEW_REFRESH_TOKEN=$(echo "$RESPONSE" | jq -r '.refresh_token // empty')
if [ "$ACCESS_TOKEN" = "null" ] || [ -z "$ACCESS_TOKEN" ]; then
echo "$RESPONSE"
exit 1
fi
if [ -n "$NEW_REFRESH_TOKEN" ]; then
echo "::add-mask::$NEW_REFRESH_TOKEN"
gh variable set X_REFRESH_TOKEN --body "$NEW_REFRESH_TOKEN"
fi
echo "::add-mask::$ACCESS_TOKEN"
echo "access_token=$ACCESS_TOKEN" >> "$GITHUB_OUTPUT"
- name: Post to X
run: |
TEXT="New commit: ${{ github.event.head_commit.message }} ${{ github.event.head_commit.url }}"
jq -n --arg text "$TEXT" '{text: $text}' > body.json
curl -s -f -X POST "https://api.twitter.com/2/tweets" \
-H "Authorization: Bearer ${{ steps.x.outputs.access_token }}" \
-H "Content-Type: application/json" \
-d @body.json | jq .