Skip to content

catalogue-pr-updated #205

catalogue-pr-updated

catalogue-pr-updated #205

Workflow file for this run

name: Deploy PR preview
on:
repository_dispatch:
types: [catalogue-pr-updated, catalogue-pr-closed]
env:
NODE_VERSION: 20.17.0
PREVIEW_BASE_URL: https://esa-apex.github.io/apex-algorithms-catalogue-web
PR_NUMBER: ${{ github.event.client_payload.pr_number }}
ALGO_REF: ${{ github.event.client_payload.head_ref }}
ALGO_REPO: apex_algorithms
permissions:
contents: read
pages: write
id-token: write
actions: read
pull-requests: write
concurrency:
group: preview-deployment
cancel-in-progress: false
jobs:
build-homepage:
if: ${{ github.event.action == 'catalogue-pr-updated' }}
runs-on: ubuntu-latest
outputs:
homepage-artifact: homepage-build-${{ github.run_id }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
run: npm ci
- name: Build preview homepage only
run: |
BUILD_TARGET=preview-homepage npm run build
mkdir -p homepage-build
cp -r dist/client/* homepage-build/
# Keep only the homepage and its assets
find homepage-build -type d -name "pr-preview" -prune -o -name "apps" -type d -exec rm -rf {} + 2>/dev/null || true
- name: Upload homepage artifact
uses: actions/upload-artifact@v4
with:
name: homepage-build-${{ github.run_id }}
path: homepage-build
retention-days: 1
build:
if: ${{ github.event.action == 'catalogue-pr-updated' }}
needs: build-homepage
runs-on: ubuntu-latest
outputs:
pr-message: ${{ steps.pr-message.outputs.message }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
run: npm ci
- name: Build new preview site
run: BUILD_TARGET=preview npm run build
env:
ALGO_REF: ${{ env.ALGO_REF }}
PR_NUMBER: ${{ env.PR_NUMBER }}
- name: Setup new preview site
run: |
rm -rf preview/new
npx make-dir preview/new
mv dist/client preview/new
mv preview/new/client "preview/new/pr-${PR_NUMBER}"
- name: Download homepage build
uses: actions/download-artifact@v4
with:
name: homepage-build-${{ github.run_id }}
path: homepage-build
- name: Get latest successful workflow run ID
id: get-latest-run
run: |
WORKFLOW_ID=$(gh api \
-H "Accept: application/vnd.github+json" \
repos/${{ github.repository }}/actions/workflows/preview.yml/runs \
--jq '.workflow_runs[] | select(.conclusion == "success") | .id' \
| head -n 1)
echo "run_id=$WORKFLOW_ID" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download current preview site
uses: actions/download-artifact@v5
with:
name: github-pages
run-id: ${{ steps.get-latest-run.outputs.run_id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Extract current preview site
run: |
cp artifact.tar preview
cd preview && tar xf artifact.tar
rm artifact.tar
- name: Merge new preview site with current preview site
run: |
# Copy the PR-specific content
cp -r preview/new/* preview/pr-preview
# Copy the homepage build to replace the main index.html
cp homepage-build/pr-preview/index.html preview/index.html
# Copy homepage assets to the root for the main homepage
cp -r homepage-build/_astro preview/ 2>/dev/null || true
cp -r homepage-build/fonts preview/ 2>/dev/null || true
cp -r homepage-build/icons preview/ 2>/dev/null || true
cp -r homepage-build/images preview/ 2>/dev/null || true
# Clean up
rm -rf preview/new
- name: Update versions.csv
run: |
rm preview/versions.csv || true
chmod +x scripts/generate-preview-versions.sh
./scripts/generate-preview-versions.sh
- name: Set PR message
id: pr-message
run: |
MESSAGE=$(cat <<EOF
## 🔍 Catalogue's Preview Site Deployed
Your changes have been deployed to the preview site:
**🔗 Preview URL:** ${{ env.PREVIEW_BASE_URL }}/pr-preview/pr-${{ env.PR_NUMBER }}/
This preview will be updated automatically when you push new changes to your PR.
EOF
)
echo "message<<EOF" >> "$GITHUB_OUTPUT"
echo "$MESSAGE" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
- name: Upload pages artifact
uses: actions/upload-pages-artifact@v4
with:
name: github-pages
path: preview
retention-days: 90
cleanup:
if: ${{ github.event.action == 'catalogue-pr-closed' }}
runs-on: ubuntu-latest
outputs:
pr-message: ${{ steps.pr-message.outputs.message }}
steps:
- uses: actions/checkout@v4
- name: Get latest successful workflow run ID
id: get-latest-run
run: |
WORKFLOW_ID=$(gh api \
-H "Accept: application/vnd.github+json" \
repos/${{ github.repository }}/actions/workflows/preview.yml/runs \
--jq '.workflow_runs[] | select(.conclusion == "success") | .id' \
| head -n 1)
echo "run_id=$WORKFLOW_ID" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download current preview site
uses: actions/download-artifact@v5
with:
name: github-pages
run-id: ${{ steps.get-latest-run.outputs.run_id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Extract current preview site
run: |
mkdir preview
cp artifact.tar preview
cd preview && tar xf artifact.tar
rm artifact.tar
- name: Remove preview for closed PR
run: |
rm -rf "preview/pr-preview/pr-${PR_NUMBER}"
- name: Update versions.csv
run: |
rm preview/versions.csv || true
chmod +x scripts/generate-preview-versions.sh
./scripts/generate-preview-versions.sh
- name: Set PR message
id: pr-message
run: |
MESSAGE=$(cat <<EOF
## 🧹 Catalogue's Preview Site Cleanup
The preview site for this pull request has been cleaned up.
If you need a new preview, please reopen the PR or create a new one.
EOF
)
echo "message<<EOF" >> "$GITHUB_OUTPUT"
echo "$MESSAGE" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
- name: Upload pages artifact
uses: actions/upload-pages-artifact@v4
with:
name: github-pages
path: preview
retention-days: 90
deploy:
if: always() && (needs.build.result == 'success' || needs.cleanup.result == 'success')
needs: [build, cleanup]
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Generate a token for gh cli
id: generate-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.GH_APP_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: ${{ env.ALGO_REPO }}
- name: Send comment to PR
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
repo: ${{ env.ALGO_REPO }}
number: ${{ env.PR_NUMBER }}
header: preview-link
message: ${{ needs.build.outputs.pr-message || needs.cleanup.outputs.pr-message }}