Skip to content

Auto Publish

Auto Publish #26

Workflow file for this run

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
if ! git checkout $PUBLISHED_SHA >/dev/null 2>&1; then
echo "Failed to checkout published version, assuming changes needed"
echo "NEEDS_PUBLISH=true" >> $GITHUB_ENV
exit 0
fi
PUBLISHED_SDK_HASH=$(find sdk -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | cut -d' ' -f1)
if ! git checkout - >/dev/null 2>&1; then
echo "Failed to return to original branch, but continuing"
fi
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"
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