|
| 1 | +name: Lint CSS |
| 2 | + |
| 3 | +on: |
| 4 | + # Run on direct pushes to integration branches and on all pull requests. |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - develop |
| 8 | + - main |
| 9 | + - master |
| 10 | + - trunk |
| 11 | + pull_request: |
| 12 | + # Allow manually triggering the workflow. |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +# Cancels all previous workflow runs for the same branch that have not yet completed. |
| 16 | +concurrency: |
| 17 | + # The concurrency group contains the workflow name and the branch name. |
| 18 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + lint: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + name: "Lint: CSS" |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout code |
| 29 | + uses: actions/checkout@v3 |
| 30 | + |
| 31 | + - name: Cache Node.js modules |
| 32 | + id: cache-node-modules |
| 33 | + uses: actions/cache@v4 |
| 34 | + with: |
| 35 | + path: node_modules |
| 36 | + key: ${{ runner.os }}-node_modules-${{ hashFiles('**/package-lock.json') }} |
| 37 | + restore-keys: | |
| 38 | + ${{ runner.os }}-node_modules- |
| 39 | + # The lint stage doesn't run the unit tests or use code style, so no need for PHPUnit, WPCS or phpcompatibility. |
| 40 | + - name: 'Install NPM packages' |
| 41 | + if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 42 | + run: npm ci --prefer-offline --no-audit --ignore-scripts |
| 43 | + |
| 44 | + - name: Get only files changed in this PR |
| 45 | + id: changed-files |
| 46 | + uses: actions/github-script@v6 |
| 47 | + with: |
| 48 | + script: | |
| 49 | + if (!context.payload.pull_request) { |
| 50 | + core.warning("No pull request context available. Skipping changed files retrieval."); |
| 51 | + core.setOutput('files', ''); |
| 52 | + return ''; |
| 53 | + } |
| 54 | +
|
| 55 | + const changedFiles = await github.paginate( |
| 56 | + github.rest.pulls.listFiles, |
| 57 | + { |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + pull_number: context.payload.pull_request.number, |
| 61 | + } |
| 62 | + ); |
| 63 | + const cssFiles = changedFiles |
| 64 | + .filter(file => file.status !== 'removed') |
| 65 | + .map(file => file.filename) |
| 66 | + .filter(filename => filename.endsWith('.scss') || filename.endsWith('.css')) |
| 67 | + .join(' '); |
| 68 | +
|
| 69 | + core.setOutput('files', cssFiles); |
| 70 | + return cssFiles; |
| 71 | +
|
| 72 | + # If this is a PR then lint only css/scss changed in the PR, if not a PR then lint them all. |
| 73 | + - name: Run stylelint |
| 74 | + run: npx wp-scripts lint-style ${{ steps.changed-files.outputs.files || '' }} |
0 commit comments