Skip to content

Commit 67cb4eb

Browse files
andrewimesonadrienverge
authored andcommitted
Auto-change output format if GitHub Actions detected
1 parent 50c7453 commit 67cb4eb

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

docs/integration.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,37 @@ Here is an example, to add in your .pre-commit-config.yaml
1717
hooks:
1818
- id: yamllint
1919
args: [-c=/path/to/.yamllint]
20+
21+
Integration with GitHub Actions
22+
-------------------------------
23+
24+
yamllint auto-detects when it's running inside of `GitHub
25+
Actions<https://github.com/features/actions>` and automatically uses the suited
26+
output format to decorate code with linting errors automatically. You can also
27+
force the GitHub Actions output with ``yamllint --format github``.
28+
29+
An example workflow using GitHub Actions:
30+
31+
.. code:: yaml
32+
33+
---
34+
name: yamllint test
35+
36+
on: push
37+
38+
jobs:
39+
test:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v2
43+
44+
- name: Set up Python
45+
uses: actions/setup-python@v2
46+
with:
47+
python-version: 3.8
48+
49+
- name: Install yamllint
50+
run: pip install yamllint
51+
52+
- name: Lint YAML files
53+
run: yamllint .

tests/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,24 @@ def test_run_format_github(self):
563563
self.assertEqual(
564564
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
565565

566+
def test_github_actions_detection(self):
567+
path = os.path.join(self.wd, 'a.yaml')
568+
self.addCleanup(os.environ.__delitem__, 'GITHUB_ACTIONS')
569+
self.addCleanup(os.environ.__delitem__, 'GITHUB_WORKFLOW')
570+
571+
with RunContext(self) as ctx:
572+
os.environ['GITHUB_ACTIONS'] = 'something'
573+
os.environ['GITHUB_WORKFLOW'] = 'something'
574+
cli.run((path, ))
575+
expected_out = (
576+
'::error file=%s,line=2,col=4::[trailing-spaces] trailing'
577+
' spaces\n'
578+
'::error file=%s,line=3,col=4::[new-line-at-end-of-file] no'
579+
' new line character at the end of file\n'
580+
% (path, path))
581+
self.assertEqual(
582+
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
583+
566584
def test_run_read_from_stdin(self):
567585
# prepares stdin with an invalid yaml string so that we can check
568586
# for its specific error, and be assured that stdin was read

yamllint/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ def show_problems(problems, file, args_format, no_warn):
109109
continue
110110
if args_format == 'parsable':
111111
print(Format.parsable(problem, file))
112-
elif args_format == 'github':
112+
elif args_format == 'github' or (args_format == 'auto' and
113+
'GITHUB_ACTIONS' in os.environ and
114+
'GITHUB_WORKFLOW' in os.environ):
113115
print(Format.github(problem, file))
114116
elif args_format == 'colored' or \
115117
(args_format == 'auto' and supports_color()):

0 commit comments

Comments
 (0)