v0.2.7 #20
Workflow file for this run
This file contains hidden or 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: Publish to NPM | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to publish (leave empty to use Cargo.toml version)' | |
| required: false | |
| type: string | |
| tag: | |
| description: 'NPM tag (latest=production, test/beta/alpha=auto-versioned for testing)' | |
| required: false | |
| default: 'test' | |
| type: choice | |
| options: | |
| - test | |
| - beta | |
| - alpha | |
| - latest | |
| # Allow the workflow to create deployments and comment on releases | |
| permissions: | |
| contents: read | |
| deployments: write | |
| issues: write | |
| pull-requests: write | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Rust toolchain | |
| uses: dtolnay/rust-toolchain@stable | |
| with: | |
| targets: wasm32-unknown-unknown | |
| - name: Cache Rust dependencies and tools | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.cargo/registry | |
| ~/.cargo/git | |
| ~/.cargo/bin | |
| target | |
| key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-tools-v1 | |
| restore-keys: | | |
| ${{ runner.os }}-cargo- | |
| - name: Install wasm-pack and cargo-edit | |
| run: | | |
| # Only install if not already cached | |
| if [ ! -f ~/.cargo/bin/wasm-pack ]; then | |
| echo "Installing wasm-pack..." | |
| curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh | |
| else | |
| echo "wasm-pack already installed (cached)" | |
| fi | |
| if [ ! -f ~/.cargo/bin/cargo-edit ]; then | |
| echo "Installing cargo-edit..." | |
| cargo install cargo-edit | |
| else | |
| echo "cargo-edit already installed (cached)" | |
| fi | |
| echo "$HOME/.cargo/bin" >> $GITHUB_PATH | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Handle version management | |
| run: | | |
| INPUT_VERSION="${{ github.event.inputs.version }}" | |
| INPUT_TAG="${{ github.event.inputs.tag }}" | |
| # Use cargo's built-in metadata extraction (best practice) | |
| CURRENT_VERSION=$(cargo pkgid | cut -d# -f2 | cut -d: -f2) | |
| if [ -n "$INPUT_VERSION" ]; then | |
| echo "Updating version to: $INPUT_VERSION" | |
| NEW_VERSION="$INPUT_VERSION" | |
| elif [ "$INPUT_TAG" != "latest" ] && [ -n "$INPUT_TAG" ]; then | |
| TIMESTAMP=$(date +%Y%m%d%H%M%S) | |
| # For test versions, increment patch version first to avoid semver "downgrade" | |
| # e.g., 0.2.0 -> 0.2.1-test.timestamp (not 0.2.0-test.timestamp) | |
| NEXT_VERSION=$(echo "$CURRENT_VERSION" | awk -F. '{$NF = $NF + 1; print}' OFS=.) | |
| NEW_VERSION="${NEXT_VERSION}-${INPUT_TAG}.${TIMESTAMP}" | |
| echo "Auto-generating test version: $NEW_VERSION (incremented from $CURRENT_VERSION)" | |
| else | |
| echo "Using version from Cargo.toml: $CURRENT_VERSION" | |
| NEW_VERSION="$CURRENT_VERSION" | |
| fi | |
| if [ -n "$NEW_VERSION" ] && [ "$NEW_VERSION" != "$CURRENT_VERSION" ]; then | |
| echo "Updating Cargo.toml with version: $NEW_VERSION" | |
| cargo set-version "$NEW_VERSION" | |
| echo "Updated to version: $NEW_VERSION" | |
| fi | |
| - name: Build WebAssembly package | |
| run: | | |
| echo "🔨 Building WebAssembly package for npm..." | |
| wasm-pack build \ | |
| --target bundler \ | |
| --out-dir pkg \ | |
| --release | |
| # Add memory export to the generated JS and TypeScript definitions | |
| echo "📝 Adding memory export..." | |
| sed -i '1a export const memory = wasm.memory;' pkg/bresenham_lighting_engine.js | |
| sed -i '2a /**\n * Export the memory of the module so we can access it directly\n */\nexport const memory: WebAssembly.Memory;' pkg/bresenham_lighting_engine.d.ts | |
| echo "📊 Build verification:" | |
| ls -la pkg/ | |
| echo "WASM size: $(du -h pkg/bresenham_lighting_engine_bg.wasm | cut -f1)" | |
| # Show the generated package.json | |
| echo "📦 Generated package.json:" | |
| cat pkg/package.json | |
| - name: Update package metadata | |
| working-directory: pkg | |
| run: | | |
| # Add repository and homepage information | |
| npm pkg set repository.type="git" | |
| npm pkg set repository.url="git+https://github.com/${{ github.repository }}.git" | |
| npm pkg set homepage="https://github.com/${{ github.repository }}#readme" | |
| npm pkg set bugs.url="https://github.com/${{ github.repository }}/issues" | |
| # Add keywords if not present | |
| npm pkg set keywords='["lighting", "game-engine", "webassembly", "bresenham", "2d", "wasm", "rust"]' | |
| echo "📝 Updated package.json:" | |
| cat package.json | |
| - name: Check if version exists and dry run | |
| working-directory: pkg | |
| run: | | |
| PACKAGE_NAME=$(npm pkg get name | tr -d '"') | |
| PACKAGE_VERSION=$(npm pkg get version | tr -d '"') | |
| echo "🔍 Checking if ${PACKAGE_NAME}@${PACKAGE_VERSION} already exists..." | |
| # Check if version exists on npm | |
| if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version 2>/dev/null; then | |
| echo "⚠️ Version ${PACKAGE_VERSION} already exists on npm" | |
| if [ "${{ github.event.inputs.tag || 'latest' }}" = "latest" ]; then | |
| echo "❌ Cannot republish existing version to 'latest' tag" | |
| echo "💡 Suggestion: Increment the version in Cargo.toml or use a different tag for testing" | |
| exit 1 | |
| else | |
| echo "⚠️ Version exists but using tag '${{ github.event.inputs.tag || 'latest' }}' - this might still fail" | |
| fi | |
| else | |
| echo "✅ Version ${PACKAGE_VERSION} is available for publishing" | |
| fi | |
| echo "🧪 Running dry run publish..." | |
| npm publish --dry-run --tag ${{ github.event.inputs.tag || 'latest' }} | |
| - name: Publish to NPM | |
| working-directory: pkg | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| echo "🚀 Publishing to NPM..." | |
| npm publish --tag ${{ github.event.inputs.tag || 'latest' }} --access public | |
| echo "✅ Package published successfully!" | |
| echo "📦 Package: $(npm pkg get name | tr -d '"')" | |
| echo "🏷️ Version: $(npm pkg get version | tr -d '"')" | |
| echo "🔖 Tag: ${{ github.event.inputs.tag || 'latest' }}" | |
| - name: Create GitHub deployment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const packageJson = require('./pkg/package.json'); | |
| await github.rest.repos.createDeployment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: context.sha, | |
| environment: 'npm', | |
| description: `Published ${packageJson.name}@${packageJson.version} to NPM`, | |
| auto_merge: false, | |
| required_contexts: [] | |
| }); | |