Skip to content

fix: Fix environment variable handling in build workflow #2

fix: Fix environment variable handling in build workflow

fix: Fix environment variable handling in build workflow #2

# build-draft-release.yml
#
# This workflow is triggered when a version tag (e.g., 1.3.17) is pushed.
# It builds the Obsidian plugin, minifies it with Terser, and creates a
# draft GitHub release with the three required files:
# - main.js (minified)
# - manifest.json (with updated version)
# - styles.css
#
# The draft release must be manually published, which will trigger
# the sync-manifest workflow to update the repository's manifest.json
#
# Improvements:
# - Uses latest GitHub Actions versions (v4)
# - Better concurrency control to prevent duplicate drafts
# - Improved error handling and verification
# - Cleaner release creation with softprops/action-gh-release
name: Build Draft Release
on:
push:
tags:
- '*.*.*' # Trigger on version tags like 1.2.3
- 'v*.*.*' # Also supports v1.2.3 format if needed
permissions:
contents: write
concurrency:
group: draft-release-${{ github.ref_name }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for potential changelog generation
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # Simplified caching
- name: Install dependencies
run: npm ci
- name: Update manifest version for release
run: |
# Strip 'v' prefix if present (supports both 1.2.3 and v1.2.3)
TAG="${{ github.ref_name }}"
VERSION="${TAG#v}"
echo "VERSION=$VERSION" >> $GITHUB_ENV
node -e "
const fs = require('fs');
const version = '${VERSION}';
const manifest = JSON.parse(fs.readFileSync('manifest.json', 'utf8'));
manifest.version = version;
fs.writeFileSync('manifest.json', JSON.stringify(manifest, null, 2) + '\n');
console.log('✅ Updated manifest.json to version ' + version + ' for release');
"
- name: Build plugin
run: npm run build
- name: Minify main.js with Terser
run: |
echo "📦 Original main.js size:"
ls -lh main.js
# Use npx to run terser without global installation
npx terser@latest main.js --compress --mangle --output main.min.js
mv main.min.js main.js
echo "✨ Minified main.js size:"
ls -lh main.js
- name: Verify release files exist
run: |
echo "🔍 Verifying release files..."
# Check each required file
for file in main.js manifest.json styles.css; do
if [ ! -f "$file" ]; then
echo "❌ Missing required file: $file"
exit 1
fi
echo "✅ Found: $file ($(ls -lh $file | awk '{print $5}'))"
done
# Verify manifest version matches tag
MANIFEST_VERSION=$(node -p "require('./manifest.json').version")
if [ "$MANIFEST_VERSION" != "$VERSION" ]; then
echo "❌ Manifest version ($MANIFEST_VERSION) doesn't match tag ($VERSION)"
exit 1
fi
echo "✅ Manifest version verified: $VERSION"
- name: Create draft release with assets
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
name: ${{ env.VERSION }}
tag_name: ${{ github.ref_name }}
files: |
main.js
manifest.json
styles.css