Skip to content

Commit 4b3434b

Browse files
committed
Create workflow to post to X
1 parent 3c16ffa commit 4b3434b

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

.github/workflows/post-to-x.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Post landed commits to X
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
post:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Refresh X OAuth token
14+
id: x
15+
run: |
16+
RESPONSE=$(curl -s -X POST "https://api.twitter.com/2/oauth2/token" \
17+
-H "Content-Type: application/x-www-form-urlencoded" \
18+
-u "${{ secrets.X_CLIENT_ID }}:${{ secrets.X_CLIENT_SECRET }}" \
19+
--data-urlencode "grant_type=refresh_token" \
20+
--data-urlencode "refresh_token=${{ secrets.X_REFRESH_TOKEN }}")
21+
22+
ACCESS_TOKEN=$(echo "$RESPONSE" | jq -r '.access_token')
23+
24+
if [ "$ACCESS_TOKEN" = "null" ] || [ -z "$ACCESS_TOKEN" ]; then
25+
echo "$RESPONSE"
26+
exit 1
27+
fi
28+
29+
echo "::add-mask::$ACCESS_TOKEN"
30+
echo "access_token=$ACCESS_TOKEN" >> "$GITHUB_OUTPUT"
31+
32+
- name: Post to X
33+
run: |
34+
TEXT="New commit: ${{ github.event.head_commit.message }} ${{ github.event.head_commit.url }}"
35+
36+
jq -n --arg text "$TEXT" '{text: $text}' > body.json
37+
38+
curl -s -f -X POST "https://api.twitter.com/2/tweets" \
39+
-H "Authorization: Bearer ${{ steps.x.outputs.access_token }}" \
40+
-H "Content-Type: application/json" \
41+
-d @body.json | jq .

0 commit comments

Comments
 (0)