Enhance GitHub Actions workflow to support separate deployment jobs f… #2
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
| # Simple workflow for deploying static content to GitHub Pages | |
| name: Deploy static content to Pages | |
| on: | |
| push: | |
| branches: ["main", "demo_app_v1", "demo_app_v2", "demo_app_v3", "demo_app_v4", "demo_app_v5"] | |
| # Allows you to run this workflow manually from the Actions tab | |
| workflow_dispatch: | |
| # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages | |
| permissions: | |
| contents: read | |
| pages: write | |
| id-token: write | |
| # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. | |
| # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. | |
| concurrency: | |
| group: "pages" | |
| cancel-in-progress: false | |
| jobs: | |
| # Deploy for non-main branches (no protected environment) | |
| deploy_unprotected: | |
| if: github.ref != 'refs/heads/main' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| - name: Install dependencies | |
| run: | | |
| if [ -f package-lock.json ]; then npm ci; else npm install; fi | |
| - name: Determine deploy subpath | |
| id: vars | |
| run: | | |
| BRANCH=${GITHUB_REF#refs/heads/} | |
| # Default folder for main is root (no vX prefix). For demo branches map to v1..v5 | |
| case "$BRANCH" in | |
| demo_app_v1) SUBPATH="v1";; | |
| demo_app_v2) SUBPATH="v2";; | |
| demo_app_v3) SUBPATH="v3";; | |
| demo_app_v4) SUBPATH="v4";; | |
| demo_app_v5) SUBPATH="v5";; | |
| main) SUBPATH="";; | |
| *) SUBPATH="";; | |
| esac | |
| echo "subpath=$SUBPATH" >> $GITHUB_OUTPUT | |
| - name: Build | |
| env: | |
| BASE_PATH: ${{ steps.vars.outputs.subpath }} | |
| APP_BASE: browserstack-demo-app | |
| run: | | |
| # Compute a single deploy base that always ends with a trailing slash | |
| if [ -n "$BASE_PATH" ]; then | |
| DEPLOY_BASE="/${APP_BASE}/${BASE_PATH}/" | |
| else | |
| DEPLOY_BASE="/${APP_BASE}/" | |
| fi | |
| # Export to the build-tool env names expected by the project | |
| export PUBLIC_URL="$DEPLOY_BASE" | |
| export VITE_BASE_PATH="$DEPLOY_BASE" | |
| echo "Building with DEPLOY_BASE=$DEPLOY_BASE" | |
| npm run build | |
| - name: Setup Pages | |
| uses: actions/configure-pages@v5 | |
| - name: Upload build artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: ./build | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 | |
| # Deploy for main branch (keeps environment protections) | |
| deploy_protected: | |
| if: github.ref == 'refs/heads/main' | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| - name: Install dependencies | |
| run: | | |
| if [ -f package-lock.json ]; then npm ci; else npm install; fi | |
| - name: Determine deploy subpath | |
| id: vars | |
| run: | | |
| BRANCH=${GITHUB_REF#refs/heads/} | |
| # Default folder for main is root (no vX prefix). For demo branches map to v1..v5 | |
| case "$BRANCH" in | |
| demo_app_v1) SUBPATH="v1";; | |
| demo_app_v2) SUBPATH="v2";; | |
| demo_app_v3) SUBPATH="v3";; | |
| demo_app_v4) SUBPATH="v4";; | |
| demo_app_v5) SUBPATH="v5";; | |
| main) SUBPATH="";; | |
| *) SUBPATH="";; | |
| esac | |
| echo "subpath=$SUBPATH" >> $GITHUB_OUTPUT | |
| - name: Build | |
| env: | |
| BASE_PATH: ${{ steps.vars.outputs.subpath }} | |
| APP_BASE: browserstack-demo-app | |
| run: | | |
| # Compute a single deploy base that always ends with a trailing slash | |
| if [ -n "$BASE_PATH" ]; then | |
| DEPLOY_BASE="/${APP_BASE}/${BASE_PATH}/" | |
| else | |
| DEPLOY_BASE="/${APP_BASE}/" | |
| fi | |
| # Export to the build-tool env names expected by the project | |
| export PUBLIC_URL="$DEPLOY_BASE" | |
| export VITE_BASE_PATH="$DEPLOY_BASE" | |
| echo "Building with DEPLOY_BASE=$DEPLOY_BASE" | |
| npm run build | |
| - name: Setup Pages | |
| uses: actions/configure-pages@v5 | |
| - name: Upload build artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: ./build | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 |