# This file tests more or less everything related to a pull request. All # in one big job. At the end, if all the testing passes, it proceeds # to upload all the files that were built to our Dev environment. # This way, if the tests passed, you'll be able to review the built # pages on a public URL. name: PR Test on: pull_request: permissions: # Compare two commits. contents: read jobs: tests: if: github.repository == 'mdn/content' runs-on: ubuntu-latest env: BASE_SHA: ${{ github.event.pull_request.base.sha }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} # This is the directory where the built files will be placed. # It's also hardcoded in the `npm run build` command in package.json. # If you change it here, you must also make the same change in # package.json. BUILD_OUT_ROOT: build steps: - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 with: persist-credentials: false - name: Get changed files id: check run: | # Use the GitHub API to get the list of changed files DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \ --jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename') FILTERED_MD_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.md$" || true) FILTERED_IMAGE_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.(png|jpeg|jpg|gif|svg|webp)$" || true) HAS_MD_FILES=false HAS_IMAGE_FILES=false # Check if we actually have content (not just empty strings) if [ -n "${FILTERED_MD_FILES// /}" ]; then # Remove all spaces and check if anything remains HAS_MD_FILES=true EOF_MD="$(openssl rand -hex 8)" echo "GIT_DIFF_CONTENT<<${EOF_MD}" >> "$GITHUB_OUTPUT" echo "${FILTERED_MD_FILES}" >> "$GITHUB_OUTPUT" echo "${EOF_MD}" >> "$GITHUB_OUTPUT" else echo "GIT_DIFF_CONTENT=" >> "$GITHUB_OUTPUT" fi if [ -n "${FILTERED_IMAGE_FILES// /}" ]; then # Remove all spaces and check if anything remains HAS_IMAGE_FILES=true EOF_IMG="$(openssl rand -hex 8)" echo "GIT_DIFF_FILES<<${EOF_IMG}" >> "$GITHUB_OUTPUT" echo "${FILTERED_IMAGE_FILES}" >> "$GITHUB_OUTPUT" echo "${EOF_IMG}" >> "$GITHUB_OUTPUT" else echo "GIT_DIFF_FILES=" >> "$GITHUB_OUTPUT" fi # Set the boolean outputs echo "HAS_MD_FILES=${HAS_MD_FILES}" >> "$GITHUB_OUTPUT" echo "HAS_IMAGE_FILES=${HAS_IMAGE_FILES}" >> "$GITHUB_OUTPUT" if [ "${HAS_MD_FILES}" = "true" ] || [ "${HAS_IMAGE_FILES}" = "true" ]; then echo "HAS_FILES=true" >> "$GITHUB_OUTPUT" else echo "HAS_FILES=false" >> "$GITHUB_OUTPUT" fi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Setup Node.js environment if: steps.check.outputs.HAS_FILES == 'true' uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0 with: node-version-file: ".nvmrc" cache: npm - name: Install if: steps.check.outputs.HAS_FILES == 'true' run: npm ci env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Build changed content id: build-content if: steps.check.outputs.HAS_MD_FILES == 'true' env: GIT_DIFF_CONTENT: ${{ steps.check.outputs.GIT_DIFF_CONTENT }} # Used by the `rari` cli to avoid rate limiting issues # when fetching the latest releases info from the GitHub API. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} CONTENT_ROOT: ${{ github.workspace }}/files # This is so that if there's a single 'unsafe_html' flaw, it # completely fails the build. # But all other flaws should be 'warn', so that we can include # information about the flaws when we analyze the built PR. BUILD_FLAW_LEVELS: "unsafe_html: error, *:warn" BUILD_LIVE_SAMPLES_BASE_URL: https://live.mdnyalp.dev BUILD_LEGACY_LIVE_SAMPLES_BASE_URL: https://live-samples.mdn.allizom.net # TODO: This should be implicit when `CI=true` BUILD_NO_PROGRESSBAR: true FRED_WRITER_MODE: true FRED_PLAYGROUND_BASE_HOST: mdnyalp.dev # rari LIVE_SAMPLES_BASE_URL: https://live.mdnyalp.dev INTERACTIVE_EXAMPLES_BASE_URL: https://interactive-examples.mdn.allizom.net run: | # The reason this script isn't in `package.json` is because # you don't need that script as a writer. It's only used in CI # and it can't use the default CONTENT_ROOT that gets set in # package.json. # Read into array, one filename per line readarray -t files_to_build <<< "$GIT_DIFF_CONTENT" ARGS=() for file in "${files_to_build[@]}"; do ARGS+=("-f" "$PWD/$file") done npx rari build --no-basic --json-issues --data-issues "${ARGS[@]}" # Workaround, as fred-ssr doesn't copy assets. cp -vR node_modules/@mdn/fred/out/. "$BUILD_OUT_ROOT" npx fred-ssr echo "Disk usage size of the build" du -sh $BUILD_OUT_ROOT # Save the PR number into the build echo ${{ github.event.number }} > ${BUILD_OUT_ROOT}/NR # Download the raw diff blob and store that inside the build # directory. # The purpose of this is for the PR Review Companion to later # be able to use this raw diff file for the benefit of analyzing. wget https://github.com/${{ github.repository }}/compare/${BASE_SHA}...${HEAD_SHA}.diff -O ${BUILD_OUT_ROOT}/DIFF - name: Show final disk usage size of build if: steps.check.outputs.HAS_MD_FILES == 'true' run: du -sh $BUILD_OUT_ROOT - uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0 if: steps.check.outputs.HAS_MD_FILES == 'true' with: name: build path: ${{ env.BUILD_OUT_ROOT }} - name: Check changed files if: steps.check.outputs.HAS_IMAGE_FILES == 'true' env: GIT_DIFF_FILES: ${{ steps.check.outputs.GIT_DIFF_FILES }} run: | readarray -t files_to_check <<< "$GIT_DIFF_FILES" export CONTENT_ROOT=$(pwd)/files npm run filecheck "${files_to_check[@]}"