-
Notifications
You must be signed in to change notification settings - Fork 21
136 lines (110 loc) · 4.37 KB
/
auto-publish.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
name: Auto Publish
# Add these permission settings
permissions:
contents: write # For creating releases and pushing code
pull-requests: write # For creating pull requests
packages: write # For publishing packages
on:
schedule:
# Runs every 15 minutes
- cron: "*/15 * * * *"
workflow_dispatch: # Allows manual triggering
jobs:
check-and-publish:
# Add branch protection
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for git history
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
registry-url: "https://registry.npmjs.org"
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
- name: Install dependencies
run: pnpm install
- name: Build and get hash
id: build
run: |
# Run preparation scripts
pnpm run prepare:market-map
pnpm run prepare:token-map
# Commit any changes from preparation scripts
git add .
git commit -m "chore: update generated files" || echo "No changes to commit"
pnpm run build
BUILD_HASH=$(find dist -type f -exec sha256sum {} \; | sort | sha256sum | cut -d' ' -f1)
echo "BUILD_HASH=${BUILD_HASH}" >> $GITHUB_ENV
echo "dist-hash=${BUILD_HASH}" >> dist-hash.txt
- name: Check if publish needed
id: check_version
run: |
# Get the git SHA from the published package
PUBLISHED_SHA=$(npm view royco gitHead 2>/dev/null || echo "none")
if [ "$PUBLISHED_SHA" = "none" ]; then
echo "Package not published yet"
echo "NEEDS_PUBLISH=true" >> $GITHUB_ENV
else
# Get hash of sdk directory in current state
CURRENT_SDK_HASH=$(find sdk -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | cut -d' ' -f1)
# Get hash of sdk directory at published version
git checkout $PUBLISHED_SHA 2>/dev/null
PUBLISHED_SDK_HASH=$(find sdk -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | cut -d' ' -f1)
git checkout - 2>/dev/null
echo "Current SDK hash: $CURRENT_SDK_HASH"
echo "Published SDK hash: $PUBLISHED_SDK_HASH"
if [ "$CURRENT_SDK_HASH" != "$PUBLISHED_SDK_HASH" ]; then
echo "Changes detected in SDK files"
echo "NEEDS_PUBLISH=true" >> $GITHUB_ENV
else
echo "No SDK changes detected"
echo "NEEDS_PUBLISH=false" >> $GITHUB_ENV
echo "No changes detected - skipping publish"
exit 0
fi
fi
- name: Configure Git
if: env.NEEDS_PUBLISH == 'true'
run: |
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
- name: Create Changeset and Publish
if: env.NEEDS_PUBLISH == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# Create changeset
CURRENT_TIME=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
mkdir -p .changeset
echo "---
\"royco\": patch
---
New SDK version @ ${CURRENT_TIME}" > .changeset/automated-patch-release.md
# Add and commit changeset
git add .changeset/*.md
git commit -m "feat(npm): add changeset"
# Create release
pnpm changeset version
# Update package.json with new dist-hash
node -e "const pkg=require('./package.json'); pkg['dist-hash']='${BUILD_HASH}'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n')"
# Update package.json files
git add package.json
git commit -m "feat(npm): update versions and dist-hash"
# Build again
pnpm run build
# Publish to npm
pnpm changeset publish
# Push changes and tags
git push --follow-tags
# Create GitHub release
LATEST_TAG=$(git describe --tags --abbrev=0)
gh release create "$LATEST_TAG" --generate-notes