aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/pr-test.yml
blob: 017a060423947b76b2539f3c4e96a8c132fadd71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# This is more or less a copy of
# https://github.com/mdn/content/blob/main/.github/workflows/pr-test.yml
# but done in a way that it first checks out mdn/translated-content (or
# fork of) and _then_ checks out mdn/content which has the relevant
# CI related tooling.

name: PR Test

on:
  pull_request:
    branches:
      - main

jobs:
  tests:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        # Needed otherswise it will check out a merge commit which means
        # you can't get a `git diff` for this PR compared to its base.
        # with:
        #   ref: ${{ github.event.pull_request.head.sha }}

      - uses: actions/checkout@v2
        with:
          repository: mdn/content
          path: mdn/content

      - name: Setup Node.js environment
        uses: actions/setup-node@v3
        with:
          node-version: "12"

      - name: Cache node_modules
        uses: actions/cache@v2.1.6
        id: cached-node_modules
        with:
          path: |
            ${{ github.workspace }}/mdn/content/node_modules
          key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Install all yarn packages
        if: steps.cached-node_modules.outputs.cache-hit != 'true'
        working-directory: ${{ github.workspace }}/mdn/content
        run: |
          yarn --frozen-lockfile

      - name: Config git core.quotePath
        run: |
          # If you don't do this, the get-diff-action won't be able to
          # notice files that contain non-ascii characters.
          # I.e.
          #
          #    ▶ git status
          #    Changes not staged for commit:
          #    ...
          #   modified:   "files/en-us/glossary/b\303\251zier_curve/index.html"
          #
          # But after you set `core.quotePath` you get:
          #
          #    ▶ git status
          #    Changes not staged for commit:
          #    ...
          #   modified:   "files/en-us/glossary/bézier_curve/index.html"
          #
          # Now, this gets used by the `git diff ...` inside get-diff-action.
          git config --global core.quotePath false

      - uses: technote-space/get-diff-action@v6.0.1
        id: git_diff_content
        with:
          PATTERNS: files/**/*.+(html|md)
          SET_ENV_NAME: GIT_DIFF_CONTENT

      - name: Build changed content
        if: ${{ env.GIT_DIFF_CONTENT }}
        env:
          CONTENT_ROOT: ${{ github.workspace }}/mdn/content/files
          CONTENT_TRANSLATED_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"

          # Because we build these pages in a way that you get a toolbar,
          # so the flaws can be displayed, but we don't want any of the
          # other toolbar features like "Fix fixable flaws" or "Quick-edit"
          # we set this to disable that stuff.
          REACT_APP_CRUD_MODE_READONLY: true

          # Setting this to an empty string effectively means that the
          # <iframe> src will end up being the relative URL of the current
          # document as a base.
          # I.e. like this, if the current document is '/en-US/docs/Foo':
          #  <iframe src="/en-US/docs/Foo/_samples_/index.html">
          # ...for example.
          # Yes, it's potentially "insecure" because the iframe will execute
          # whatever code is inserted into the code example. But since the
          # whole (possible) domain for PR builds will never be somewhere
          # where there are interesting cookies, it's a safe choice.
          BUILD_LIVE_SAMPLES_BASE_URL: ""

          # In these builds we never care for or need the ability to sign in.
          # This environment variable will disable that functionality entirely.
          REACT_APP_DISABLE_AUTH: true

          # TODO: This should be implicit when `CI=true`
          BUILD_NO_PROGRESSBAR: true

          # If we don't do this the built files will end up in
          # `node_modules/@mdn/yari/client/build/` and we don't want that
          # to get pushed into the cache.
          BUILD_OUT_ROOT: /tmp/build

        working-directory: ${{ github.workspace }}/mdn/content
        run: |
          mkdir -p $BUILD_OUT_ROOT

          # Don't use `yarn build` (from mdn/content) because that one hardcodes
          # the BUILD_OUT_ROOT and CONTENT_ROOT env vars.
          node node_modules/@mdn/yari/build/cli.js ${{ env.GIT_DIFF_CONTENT }}

          echo "Disk usage size of build/"
          du -sh $BUILD_OUT_ROOT

          # Save the PR number into the build
          echo ${{ github.event.number }} > $BUILD_OUT_ROOT/NR

      - name: Generate the diff file
        if: ${{ env.GIT_DIFF_CONTENT }}
        env:
          # This must match what we set in the "Build changed content" step above
          BUILD_OUT_ROOT: /tmp/build
        run: |
          # Save 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.
          git diff --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} > $BUILD_OUT_ROOT/DIFF

      - name: Merge static assets with built documents
        if: ${{ env.GIT_DIFF_CONTENT }}
        env:
          # This must match what we set in the "Build changed content" step above
          BUILD_OUT_ROOT: /tmp/build
        run: |
          rsync -a ${{ github.workspace }}/mdn/content/node_modules/@mdn/yari/client/build/ $BUILD_OUT_ROOT/

          # Now that build/ directory contains everything you need to deploy
          # that as a site. HTML, static assets, images, etc.
          # However, that Yari static files is very heavy and it's in large
          # part due to the .map files.
          # In fact, as of March 2021, the client/build/static directory
          # is 2.3MB but only 864KB without all the .map files.
          # Let's delete those this time because this isn't the right time
          # to debug JS or CSS.
          find $BUILD_OUT_ROOT/static -type f -name "*.map" | xargs rm

      - uses: actions/upload-artifact@v2
        if: ${{ env.GIT_DIFF_CONTENT }}
        env:
          # This must match what we set in the "Build changed content" step above
          BUILD_OUT_ROOT: /tmp/build
        with:
          name: build
          path: ${{ env.BUILD_OUT_ROOT }}

      - uses: technote-space/get-diff-action@v6.0.1
        with:
          PATTERNS: files/**/*.+(png|jpeg|jpg|gif|svg|webp)
          ABSOLUTE: true
          SET_ENV_NAME: GIT_DIFF_FILES

      - name: Check changed files
        if: ${{ env.GIT_DIFF_FILES }}
        env:
          CONTENT_ROOT: ${{ github.workspace }}/mdn/content/files
          CONTENT_TRANSLATED_ROOT: ${{ github.workspace }}/files
        working-directory: ${{ github.workspace }}/mdn/content
        run: |
          echo ${{ env.GIT_DIFF_FILES }}

          yarn filecheck ${{ env.GIT_DIFF_FILES }}