|
| 1 | +name: Build, test, and deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['**'] |
| 6 | + delete: |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + deployments: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: deploy-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + build-and-test: |
| 19 | + if: github.event_name != 'delete' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + branch: ${{ steps.meta.outputs.branch }} |
| 23 | + base_path: ${{ steps.meta.outputs.base_path }} |
| 24 | + is_production: ${{ steps.meta.outputs.is_production }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - uses: oven-sh/setup-bun@v2 |
| 29 | + with: |
| 30 | + bun-version: latest |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: bun install --frozen-lockfile || bun install |
| 34 | + |
| 35 | + - name: Determine deploy metadata |
| 36 | + id: meta |
| 37 | + run: | |
| 38 | + BRANCH="${GITHUB_REF_NAME}" |
| 39 | + SANITIZED="$(echo "$BRANCH" | tr '/' '-' | tr -cd 'a-zA-Z0-9._-')" |
| 40 | + if [ "$BRANCH" = "main" ]; then |
| 41 | + echo "base_path=/" >> "$GITHUB_OUTPUT" |
| 42 | + echo "branch=main" >> "$GITHUB_OUTPUT" |
| 43 | + echo "is_production=true" >> "$GITHUB_OUTPUT" |
| 44 | + else |
| 45 | + echo "base_path=/preview/$SANITIZED/" >> "$GITHUB_OUTPUT" |
| 46 | + echo "branch=$SANITIZED" >> "$GITHUB_OUTPUT" |
| 47 | + echo "is_production=false" >> "$GITHUB_OUTPUT" |
| 48 | + fi |
| 49 | +
|
| 50 | + - name: Run unit and component tests |
| 51 | + run: bun test tests/catalog tests/standards tests/views tests/enhancements tests/build |
| 52 | + |
| 53 | + - name: Build site |
| 54 | + env: |
| 55 | + SITE_BASE_URL: ${{ steps.meta.outputs.base_path }} |
| 56 | + run: bun run build |
| 57 | + |
| 58 | + - name: Run a11y tests against built site |
| 59 | + run: bun test tests/a11y |
| 60 | + |
| 61 | + - name: Upload dist artifact |
| 62 | + uses: actions/upload-artifact@v4 |
| 63 | + with: |
| 64 | + name: dist |
| 65 | + path: dist/ |
| 66 | + retention-days: 3 |
| 67 | + |
| 68 | + publish: |
| 69 | + if: github.event_name == 'push' |
| 70 | + needs: build-and-test |
| 71 | + runs-on: ubuntu-latest |
| 72 | + environment: |
| 73 | + name: ${{ needs.build-and-test.outputs.is_production == 'true' && 'production' || 'preview' }} |
| 74 | + url: ${{ needs.build-and-test.outputs.is_production == 'true' && 'https://labs.flexion.us/' || format('https://labs.flexion.us/preview/{0}/', needs.build-and-test.outputs.branch) }} |
| 75 | + steps: |
| 76 | + - name: Check out or bootstrap gh-pages |
| 77 | + env: |
| 78 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | + git init gh-pages-work |
| 82 | + cd gh-pages-work |
| 83 | + git config user.name 'github-actions[bot]' |
| 84 | + git config user.email 'github-actions[bot]@users.noreply.github.com' |
| 85 | + git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 86 | + if git ls-remote --exit-code --heads origin gh-pages >/dev/null 2>&1; then |
| 87 | + git fetch --depth=1 origin gh-pages |
| 88 | + git checkout -B gh-pages FETCH_HEAD |
| 89 | + else |
| 90 | + echo 'gh-pages branch missing — creating orphan.' |
| 91 | + git checkout --orphan gh-pages |
| 92 | + git reset --hard |
| 93 | + fi |
| 94 | +
|
| 95 | + - uses: actions/download-artifact@v4 |
| 96 | + with: |
| 97 | + name: dist |
| 98 | + path: dist |
| 99 | + |
| 100 | + - name: Sync dist into gh-pages |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + BASE_PATH="${{ needs.build-and-test.outputs.base_path }}" |
| 104 | + cd gh-pages-work |
| 105 | + if [ "$BASE_PATH" = "/" ]; then |
| 106 | + find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name 'preview' -exec rm -rf {} + |
| 107 | + cp -r ../dist/. ./ |
| 108 | + else |
| 109 | + rel="${BASE_PATH#/}" |
| 110 | + rel="${rel%/}" |
| 111 | + rm -rf "$rel" |
| 112 | + mkdir -p "$(dirname "$rel")" |
| 113 | + cp -r ../dist "$rel" |
| 114 | + fi |
| 115 | + if [ ! -f CNAME ]; then |
| 116 | + cp ../CNAME . 2>/dev/null || true |
| 117 | + fi |
| 118 | +
|
| 119 | + - name: Commit and push gh-pages |
| 120 | + working-directory: gh-pages-work |
| 121 | + run: | |
| 122 | + set -euo pipefail |
| 123 | + git add -A |
| 124 | + if git diff --cached --quiet; then |
| 125 | + echo 'No changes to publish.' |
| 126 | + exit 0 |
| 127 | + fi |
| 128 | + git commit -m "Deploy ${GITHUB_SHA::7} to ${{ needs.build-and-test.outputs.base_path }}" |
| 129 | + git push origin gh-pages |
| 130 | +
|
| 131 | + cleanup-preview: |
| 132 | + if: github.event_name == 'delete' && github.event.ref_type == 'branch' |
| 133 | + runs-on: ubuntu-latest |
| 134 | + permissions: |
| 135 | + contents: write |
| 136 | + steps: |
| 137 | + - name: Check out gh-pages (skip if absent) |
| 138 | + id: checkout |
| 139 | + env: |
| 140 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 141 | + run: | |
| 142 | + set -euo pipefail |
| 143 | + git init . |
| 144 | + git config user.name 'github-actions[bot]' |
| 145 | + git config user.email 'github-actions[bot]@users.noreply.github.com' |
| 146 | + git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 147 | + if git ls-remote --exit-code --heads origin gh-pages >/dev/null 2>&1; then |
| 148 | + git fetch --depth=1 origin gh-pages |
| 149 | + git checkout -B gh-pages FETCH_HEAD |
| 150 | + echo 'exists=true' >> "$GITHUB_OUTPUT" |
| 151 | + else |
| 152 | + echo 'gh-pages branch does not exist — nothing to clean up.' |
| 153 | + echo 'exists=false' >> "$GITHUB_OUTPUT" |
| 154 | + fi |
| 155 | +
|
| 156 | + - name: Remove preview directory |
| 157 | + if: steps.checkout.outputs.exists == 'true' |
| 158 | + run: | |
| 159 | + set -euo pipefail |
| 160 | + SANITIZED="$(echo "${{ github.event.ref }}" | tr '/' '-' | tr -cd 'a-zA-Z0-9._-')" |
| 161 | + DIR="preview/$SANITIZED" |
| 162 | + if [ -d "$DIR" ]; then |
| 163 | + rm -rf "$DIR" |
| 164 | + git add -A |
| 165 | + git commit -m "Remove preview for deleted branch $SANITIZED" |
| 166 | + git push origin gh-pages |
| 167 | + fi |
0 commit comments