|
| 1 | +# Vendored from City-of-Helsinki/.github with Node-before-pnpm setup order |
| 2 | +# (pnpm 11.5+ requires Node >=22.13 at install time; CI uses Node 24). |
| 3 | +name: Common CI for Node (pnpm) |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + node-version: |
| 9 | + description: Node version to use |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + pnpm-version: |
| 13 | + description: pnpm version to use |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + default: '11.5.1' |
| 17 | + extra-commands: |
| 18 | + description: Extra setup commands to run before lint/test/build |
| 19 | + required: false |
| 20 | + type: string |
| 21 | + app-directory: |
| 22 | + description: Application subdirectory for lint/test/build |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + default: '.' |
| 26 | + typecheck: |
| 27 | + description: Run type checking |
| 28 | + required: false |
| 29 | + type: boolean |
| 30 | + default: false |
| 31 | + upload-artifacts: |
| 32 | + description: Upload build artifacts |
| 33 | + required: false |
| 34 | + type: boolean |
| 35 | + default: false |
| 36 | + skip-sonar: |
| 37 | + description: Skip SonarQube |
| 38 | + required: false |
| 39 | + type: boolean |
| 40 | + default: false |
| 41 | + skip-build: |
| 42 | + description: Skip build phase |
| 43 | + required: false |
| 44 | + type: boolean |
| 45 | + default: false |
| 46 | + working-directory: |
| 47 | + description: Working directory for install/build/test jobs |
| 48 | + required: false |
| 49 | + type: string |
| 50 | + default: '.' |
| 51 | + ignore-scripts: |
| 52 | + description: Pass --ignore-scripts to pnpm install |
| 53 | + required: false |
| 54 | + type: boolean |
| 55 | + default: true |
| 56 | + secrets: |
| 57 | + SONAR_TOKEN: |
| 58 | + description: 'SonarQube Cloud token' |
| 59 | + required: true |
| 60 | + workflow_dispatch: |
| 61 | + |
| 62 | +jobs: |
| 63 | + commitlint: |
| 64 | + name: Check commit messages |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@v4 |
| 69 | + - name: Run commitlint |
| 70 | + uses: wagoid/commitlint-github-action@b948419dd99f3fd78a6548d48f94e3df7f6bf3ed # v6.2.1 |
| 71 | + |
| 72 | + build: |
| 73 | + name: Lint and build |
| 74 | + if: ${{ !inputs.skip-build }} |
| 75 | + runs-on: ubuntu-latest |
| 76 | + defaults: |
| 77 | + run: |
| 78 | + working-directory: ${{ inputs.working-directory }} |
| 79 | + steps: |
| 80 | + - uses: actions/checkout@v4 |
| 81 | + |
| 82 | + - name: Setup Node.js |
| 83 | + uses: actions/setup-node@v4 |
| 84 | + with: |
| 85 | + node-version: ${{ inputs.node-version }} |
| 86 | + |
| 87 | + - name: Setup pnpm |
| 88 | + uses: pnpm/action-setup@v4 |
| 89 | + with: |
| 90 | + version: ${{ inputs.pnpm-version }} |
| 91 | + |
| 92 | + - name: Setup Node.js with dependency path |
| 93 | + if: ${{ inputs.working-directory != '.' }} |
| 94 | + uses: actions/setup-node@v4 |
| 95 | + with: |
| 96 | + node-version: ${{ inputs.node-version }} |
| 97 | + cache: pnpm |
| 98 | + cache-dependency-path: ${{ inputs.working-directory }}/pnpm-lock.yaml |
| 99 | + |
| 100 | + - name: Setup Node.js without dependency path |
| 101 | + if: ${{ inputs.working-directory == '.' }} |
| 102 | + uses: actions/setup-node@v4 |
| 103 | + with: |
| 104 | + node-version: ${{ inputs.node-version }} |
| 105 | + cache: pnpm |
| 106 | + |
| 107 | + - name: Install dependencies |
| 108 | + run: | |
| 109 | + IGNORE_SCRIPTS="${{ inputs.ignore-scripts == true && '--ignore-scripts' || '' }}" |
| 110 | + pnpm install --frozen-lockfile $IGNORE_SCRIPTS |
| 111 | +
|
| 112 | + - name: Run extra commands |
| 113 | + if: ${{ inputs.extra-commands != '' }} |
| 114 | + run: ${{ inputs.extra-commands }} |
| 115 | + |
| 116 | + - name: Typecheck application |
| 117 | + if: ${{ inputs.typecheck }} |
| 118 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 119 | + run: pnpm typecheck |
| 120 | + |
| 121 | + - name: Lint application |
| 122 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 123 | + run: pnpm lint |
| 124 | + |
| 125 | + - name: Build application |
| 126 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 127 | + run: pnpm build |
| 128 | + |
| 129 | + - name: Upload build |
| 130 | + if: ${{ inputs.upload-artifacts }} |
| 131 | + uses: actions/upload-artifact@v4 |
| 132 | + with: |
| 133 | + name: build-artifacts-${{ inputs.node-version }} |
| 134 | + path: | |
| 135 | + ${{ inputs.app-directory }}/dist |
| 136 | + ${{ inputs.app-directory }}/build |
| 137 | + ${{ inputs.app-directory }}/lib |
| 138 | + retention-days: 1 |
| 139 | + |
| 140 | + - name: Check browser bundle size limits |
| 141 | + if: ${{ hashFiles(format('{0}/.size-limit.js', inputs.app-directory)) != '' }} |
| 142 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 143 | + run: pnpm check-size |
| 144 | + |
| 145 | + - name: Check ecmascript checks for build files |
| 146 | + if: ${{ hashFiles(format('{0}/.escheckrc', inputs.app-directory)) != '' }} |
| 147 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 148 | + run: pnpm check-dist |
| 149 | + |
| 150 | + tests: |
| 151 | + name: Test |
| 152 | + runs-on: ubuntu-latest |
| 153 | + defaults: |
| 154 | + run: |
| 155 | + working-directory: ${{ inputs.working-directory }} |
| 156 | + steps: |
| 157 | + - uses: actions/checkout@v4 |
| 158 | + |
| 159 | + - name: Setup Node.js |
| 160 | + uses: actions/setup-node@v4 |
| 161 | + with: |
| 162 | + node-version: ${{ inputs.node-version }} |
| 163 | + |
| 164 | + - name: Setup pnpm |
| 165 | + uses: pnpm/action-setup@v4 |
| 166 | + with: |
| 167 | + version: ${{ inputs.pnpm-version }} |
| 168 | + |
| 169 | + - name: Setup Node.js with dependency path |
| 170 | + if: ${{ inputs.working-directory != '.' }} |
| 171 | + uses: actions/setup-node@v4 |
| 172 | + with: |
| 173 | + node-version: ${{ inputs.node-version }} |
| 174 | + cache: pnpm |
| 175 | + cache-dependency-path: ${{ inputs.working-directory }}/pnpm-lock.yaml |
| 176 | + |
| 177 | + - name: Setup Node.js without dependency path |
| 178 | + if: ${{ inputs.working-directory == '.' }} |
| 179 | + uses: actions/setup-node@v4 |
| 180 | + with: |
| 181 | + node-version: ${{ inputs.node-version }} |
| 182 | + cache: pnpm |
| 183 | + |
| 184 | + - name: Install dependencies |
| 185 | + run: | |
| 186 | + IGNORE_SCRIPTS="${{ inputs.ignore-scripts == true && '--ignore-scripts' || '' }}" |
| 187 | + pnpm install --frozen-lockfile $IGNORE_SCRIPTS |
| 188 | +
|
| 189 | + - name: Run extra commands |
| 190 | + if: ${{ inputs.extra-commands != '' }} |
| 191 | + run: ${{ inputs.extra-commands }} |
| 192 | + |
| 193 | + - name: Run tests |
| 194 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 195 | + run: pnpm test:coverage |
| 196 | + env: |
| 197 | + CI: true |
| 198 | + |
| 199 | + - name: Store coverage report |
| 200 | + uses: actions/cache/save@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 |
| 201 | + with: |
| 202 | + path: ${{ (inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory) && format('{0}/', inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory) || ''}}coverage |
| 203 | + key: cache-${{ github.run_id }}-${{ github.run_attempt }} |
| 204 | + |
| 205 | + sonarcloud: |
| 206 | + name: Run SonarQube Cloud Scan |
| 207 | + if: ${{ !inputs.skip-sonar }} |
| 208 | + runs-on: ubuntu-latest |
| 209 | + needs: tests |
| 210 | + defaults: |
| 211 | + run: |
| 212 | + working-directory: ${{ inputs.working-directory }} |
| 213 | + steps: |
| 214 | + - uses: actions/checkout@v4 |
| 215 | + with: |
| 216 | + fetch-depth: 0 |
| 217 | + |
| 218 | + - name: Restore coverage report |
| 219 | + uses: actions/cache/restore@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 |
| 220 | + with: |
| 221 | + path: ${{ (inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory) && format('{0}/', inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory) || ''}}coverage |
| 222 | + key: cache-${{ github.run_id }}-${{ github.run_attempt }} |
| 223 | + fail-on-cache-miss: true |
| 224 | + |
| 225 | + - name: Override coverage report source path for SonarQube Cloud |
| 226 | + working-directory: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 227 | + run: | |
| 228 | + find coverage -type f \( -name '*.info' -o -name 'lcov.info' \) 2>/dev/null | xargs -r sed -i 's@SF:'$GITHUB_WORKSPACE'@SF:/github/workspace/@g' || true |
| 229 | + find coverage -type f -name '*.json' 2>/dev/null | xargs -r sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' || true |
| 230 | +
|
| 231 | + - name: SonarQube Cloud Scan |
| 232 | + uses: SonarSource/sonarqube-scan-action@59db25f34e16620e48ab4bb9e4a5dce155cb5432 # v8.0.0 |
| 233 | + with: |
| 234 | + projectBaseDir: ${{ inputs.app-directory == '.' && inputs.working-directory || inputs.app-directory }} |
| 235 | + env: |
| 236 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
0 commit comments