diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 58467c78ec..a954284367 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,6 +4,8 @@ on: push: branches: - master + tags: + - latest-stable workflow_dispatch: inputs: web: @@ -59,8 +61,6 @@ jobs: RUSTC_WRAPPER: /usr/bin/sccache CARGO_INCREMENTAL: 0 SCCACHE_DIR: /var/lib/github-actions/.cache - INDEX_HTML_HEAD_REPLACEMENT: - steps: - name: ๐Ÿ“ฅ Clone repository uses: actions/checkout@v6 @@ -90,9 +90,20 @@ jobs: rustflags: "" target: wasm32-unknown-unknown - - name: โœ‚ Replace template in of index.html + - name: ๐Ÿ”€ Choose production deployment environment and insert template + id: production-env if: github.event_name == 'push' - run: sed -i "s||$INDEX_HTML_HEAD_REPLACEMENT|" frontend/index.html + run: | + if [[ "${{ github.ref }}" == "refs/tags/latest-stable" ]]; then + echo "cf_project=graphite-editor" >> $GITHUB_OUTPUT + DOMAIN="editor.graphite.art" + else + echo "cf_project=graphite-dev" >> $GITHUB_OUTPUT + DOMAIN="dev.graphite.art" + fi + TEMPLATE="" + echo "template=$TEMPLATE" >> $GITHUB_OUTPUT + sed -i "s||$TEMPLATE|" frontend/index.html - name: ๐ŸŒ Build Graphite web code env: @@ -101,32 +112,75 @@ jobs: - name: ๐Ÿ“ค Publish to Cloudflare Pages id: cloudflare + continue-on-error: ${{ github.event_name != 'push' }} env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} run: | - MAX_ATTEMPTS=5 - DELAY=30 + if [ -z "$CLOUDFLARE_API_TOKEN" ]; then + echo "No Cloudflare API token available (fork PR), skipping deploy." + exit 0 + fi + MAX_ATTEMPTS=8 + DELAY=15 for ATTEMPT in $(seq 1 $MAX_ATTEMPTS); do echo "Attempt $ATTEMPT of $MAX_ATTEMPTS..." - if OUTPUT=$(npx wrangler@3 pages deploy "frontend/dist" --project-name="graphite-dev" --commit-dirty=true 2>&1); then - URL=$(echo "$OUTPUT" | grep -oP 'https://[^\s]+\.pages\.dev' | head -1) + npx wrangler@3 pages deploy "frontend/dist" --project-name="${{ steps.production-env.outputs.cf_project || 'graphite-dev' }}" --commit-dirty=true 2>&1 | tee /tmp/wrangler_output + if [ ${PIPESTATUS[0]} -eq 0 ]; then + URL=$(grep -oP 'https://[^\s]+\.pages\.dev' /tmp/wrangler_output | head -1) echo "url=$URL" >> "$GITHUB_OUTPUT" echo "Published successfully: $URL" exit 0 fi - echo "Attempt $ATTEMPT failed:" - echo "$OUTPUT" + echo "Attempt $ATTEMPT failed." if [ "$ATTEMPT" -lt "$MAX_ATTEMPTS" ]; then echo "Retrying in ${DELAY}s..." sleep $DELAY - DELAY=$((DELAY * 3)) + DELAY=$((DELAY * 2)) fi done echo "All $MAX_ATTEMPTS Cloudflare Pages publish attempts failed." exit 1 + - name: ๐Ÿš€ Create a GitHub environment deployment + if: inputs.checkout_repo == '' || inputs.checkout_repo == github.repository + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CF_URL: ${{ steps.cloudflare.outputs.url }} + run: | + if [ -z "$CF_URL" ]; then + echo "No Cloudflare URL available, skipping deployment." + exit 0 + fi + if [ "${{ github.ref }}" = "refs/tags/latest-stable" ]; then + REF="latest-stable" + ENVIRONMENT="graphite-editor (Production)" + elif [ "${{ github.event_name }}" = "push" ]; then + REF="master" + ENVIRONMENT="graphite-dev (Production)" + else + REF="${{ inputs.checkout_ref || github.head_ref || github.ref_name }}" + ENVIRONMENT="graphite-dev (Preview)" + fi + DEPLOY_ID=$(gh api \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + repos/${{ github.repository }}/deployments \ + --input - \ + --jq '.id' < - - steps: - - name: ๐Ÿ“ฅ Clone repository - uses: actions/checkout@v6 - - - name: ๐Ÿ—‘ Clear wasm-bindgen cache - run: rm -r ~/.cache/.wasm-pack - - - name: ๐ŸŸข Install Node.js - uses: actions/setup-node@v6 - with: - node-version-file: .nvmrc - - - name: ๐Ÿšง Install build dependencies - run: | - cd frontend - npm run setup - - - name: ๐Ÿฆ€ Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - toolchain: stable - override: true - cache: false - rustflags: "" - target: wasm32-unknown-unknown - - - name: โœ‚ Replace template in of index.html - run: sed -i "s||$INDEX_HTML_HEAD_REPLACEMENT|" frontend/index.html - - - name: ๐ŸŒ Build Graphite web code - env: - NODE_ENV: production - run: mold -run cargo run build web - - - name: ๐Ÿ“ค Publish to Cloudflare Pages - env: - CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - run: npx wrangler@3 pages deploy "frontend/dist" --project-name="graphite-editor" --branch="master" --commit-dirty=true - - - name: ๐Ÿ“ฆ Upload assets to GitHub release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - DATE=$(git log -1 --format=%cd --date=format:%Y-%m-%d) - cd frontend - sed -i "s|$INDEX_HTML_HEAD_REPLACEMENT||" dist/index.html - mv dist "graphite-$DATE" - zip -r "graphite-self-hosted-build.zip" "graphite-$DATE" - gh release upload latest-stable "graphite-self-hosted-build.zip" --clobber