Auto Publish #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
- cron: "0 * * * *" # Runs every hour | |
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 NPM version hash | |
id: check_version | |
run: | | |
CURRENT_VERSION=$(npm view royco version 2>/dev/null || echo "none") | |
CURRENT_HASH=$(npm view royco dist-hash 2>/dev/null || echo "none") | |
echo "Current published hash: $CURRENT_HASH" | |
echo "New build hash: $BUILD_HASH" | |
if [ "$CURRENT_HASH" != "$BUILD_HASH" ]; then | |
echo "New build detected, needs publishing" | |
echo "NEEDS_PUBLISH=true" >> $GITHUB_ENV | |
else | |
echo "No changes detected, skipping publish" | |
echo "NEEDS_PUBLISH=false" >> $GITHUB_ENV | |
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 |