Skip to content

Commit a5c0cf2

Browse files
authored
docs: add an example about fix mode on gh actions (#6060)
1 parent 75aedaa commit a5c0cf2

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,104 @@ When fix mode is enabled, some linters and formatters don't maintain the
463463
original file or directory ownership, and use the user that Super-linter uses
464464
to run the linter or formatter.
465465

466+
### Fix mode examples and workflows
467+
468+
You can configure Super-linter to automatically deliver linting and formatting
469+
fixes. For example, you can configure a workflow that automatically commits the
470+
fixes, and pushes them to a branch.
471+
472+
To ensure that Super-linter analyzes your codebase as expected, we recommend
473+
that you configure your fix mode workflows in addition to your existing
474+
Super-linter workflows.
475+
476+
#### GitHub Actions workflow example: pull request
477+
478+
The following example shows a GitHub Actions workflow that uses Super-linter to
479+
automatically fix linting and formatting issues, commit changes in the current
480+
branch, and push commits to the remote branch tracking the current branch
481+
whenever a pull request is created or updated:
482+
483+
```yaml
484+
---
485+
name: Lint
486+
487+
on: # yamllint disable-line rule:truthy
488+
push: null
489+
pull_request: null
490+
491+
permissions:
492+
contents: read
493+
494+
jobs:
495+
lint:
496+
# Super-linter workflow running in check only mode
497+
# See https://github.com/super-linter/super-linter#get-started
498+
499+
fix-lint-issues:
500+
permissions:
501+
# To write linting fixes
502+
contents: write
503+
# To write Super-linter status checks
504+
statuses: write
505+
runs-on: ubuntu-latest
506+
steps:
507+
- uses: actions/checkout@v4
508+
with:
509+
fetch-depth: 0
510+
- name: Super-Linter
511+
uses: super-linter/[email protected] # x-release-please-version
512+
env:
513+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
514+
# Set your fix mode variables to true
515+
FIX_SHELL_SHFMT: true
516+
FIX_YAML_PRETTIER: true
517+
# To reuse the same Super-linter configuration that you use in the
518+
# lint job without duplicating it, see
519+
# https://github.com/super-linter/super-linter/blob/main/docs/run-linter-locally.md#share-environment-variables-between-environments
520+
- name: Commit and push linting fixes
521+
# Run only on:
522+
# - Pull requests
523+
# - Not on the default branch
524+
if: >
525+
github.event_name == 'pull_request' &&
526+
github.ref_name != github.event.repository.default_branch
527+
uses: stefanzweifel/git-auto-commit-action@v5
528+
with:
529+
branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
530+
commit_message: "chore: fix linting issues"
531+
commit_user_name: super-linter
532+
commit_user_email: [email protected]
533+
```
534+
535+
This example uses
536+
[GitHub Actions automatic token authentication](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication)
537+
that automatically greates a unique `GITHUB_TOKEN` secret for the workflow.
538+
GitHub Actions imposes the following limitations on workflows:
539+
540+
- To avoid accidentally creating recursive workflow runs, the commit that
541+
contains linting and formatting fixes
542+
[doesn't create new workflow runs](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow).
543+
- It restrict edits to GitHub Actions workflows files (in `.github/workflows`).
544+
- It may fail pushing commits to protected branches.
545+
546+
To work around these limitations, you do the following:
547+
548+
1. [Create an authentication token with additional permissions](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#granting-additional-permissions).
549+
1. Grant the authentication token the
550+
[`repo` and `workflow` permissions](https://docs.github.com/en/rest/authentication/permissions-required-for-fine-grained-personal-access-tokens).
551+
1. Use the authentication token in the `actions/checkout` step:
552+
553+
```yaml
554+
- uses: actions/checkout@v4
555+
with:
556+
fetch-depth: 0
557+
token: ${{ secrets.SUPER_LINTER_TOKEN }}
558+
```
559+
560+
This example assumes that you saved the authentication token in a secret
561+
called `SUPER_LINTER_TOKEN`, but you can choose whatever name you prefer for
562+
the secret.
563+
466564
## Configure linters
467565

468566
Super-linter provides default configurations for some linters in the [`TEMPLATES/`](./TEMPLATES/)
@@ -610,6 +708,21 @@ previous paragraph, but only supports emitting results to standard output or
610708
standard error streams, you can
611709
[enable Super-linter outputs](#super-linter-outputs) and parse them.
612710

711+
### Ignore output that Super-linter generates
712+
713+
Super-linter generates output reports and logs. To avoid that these outputs end
714+
up in your repository, we recommend that you add the following lines to your
715+
`.gitignore` file:
716+
717+
```text
718+
# Super-linter outputs
719+
super-linter-output
720+
super-linter.log
721+
722+
# GitHub Actions leftovers
723+
github_conf
724+
```
725+
613726
## How to contribute
614727

615728
If you would like to help contribute to super-linter, see

0 commit comments

Comments
 (0)