Skip to content

Commit

Permalink
Correct use of working directory with ansible-lint github action (#4340)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Sep 19, 2024
1 parent 571711f commit 4e3058f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Self test for ansible-lint@${{ github.action_ref || 'main' }}
- name: Self test 1
uses: ./
with:
# basically we only lint linter own configuration, which should be passing.
args: .ansible-lint
- name: Self test 2
uses: ./
with:
# basically we only lint linter own configuration, which should be passing.
args: .ansible-lint
working_directory: .
- name: Self test 3
uses: ./
with:
# basically we only lint linter own configuration, which should be passing.
args: .ansible-lint
working_directory: docs
expected_return_code: "2" # expected to fail because the given argument does not exist in this folder
build:
name: ${{ matrix.name }}
runs-on: ${{ matrix.os || 'ubuntu-22.04' }}
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,25 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run ansible-lint
uses: ansible/ansible-lint@main # or version tag instead of 'main'
uses: ansible/ansible-lint@main
# optional (see below):
with:
args: ""
setup_python: "true"
working_directory: ""
requirements_file: ""
```
All the arguments are optional and most users should not need them:
- `args`: Arguments to be passed to ansible-lint command.
- `setup_python`: If python should be installed. Default is `true`.
- `working_directory`: The directory where to run ansible-lint from. Default is
`github.workspace`. That might be needed if you want to lint only a subset of
your repository.
- `requirements_file`: Path to the requirements.yml file to install role and
collection dependencies.

For more details, see [ansible-lint-action].

# Communication
Expand Down Expand Up @@ -63,8 +79,8 @@ original [MIT] license.

# Authors

ansible-lint was created by [Will Thames] and is now maintained as part of the
[Ansible] by [Red Hat] project.
ansible-lint was created by [Will Thames] and is now maintained as part of the [Ansible]
by [Red Hat] project.

[ansible]: https://ansible.com
[contribution guidelines]:
Expand Down
17 changes: 14 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
description: Path to the requirements YAML file to install role and collection dependencies.
required: false
default: ""
expected_return_code:
description: Expected return code from ansible-lint. Default is 0. Used for self-testing purposes.
required: false
default: "0"
runs:
using: composite
steps:
Expand All @@ -41,19 +45,22 @@ runs:
# https://github.com/actions/toolkit/issues/1035
# https://github.com/actions/setup-python/issues/361
- name: Generate .git/ansible-lint-requirements.txt
id: get_reqs
shell: bash
env:
GH_ACTION_REF: ${{ github.action_ref || 'main' }}
working-directory: ${{ steps.inputs.outputs.working_directory }}
run: |
wget --output-document=${{ steps.inputs.outputs.working_directory }}/.git/ansible-lint-requirements.txt https://raw.githubusercontent.com/ansible/ansible-lint/$GH_ACTION_REF/.config/requirements-lock.txt
reqs_file=$(git rev-parse --show-toplevel)/.git/ansible-lint-requirements.txt
echo "reqs_file=$reqs_file\n" >> $GITHUB_OUTPUT
wget --output-document=$reqs_file https://raw.githubusercontent.com/ansible/ansible-lint/$GH_ACTION_REF/.config/requirements-lock.txt
- name: Set up Python
if: inputs.setup_python == 'true'
uses: actions/setup-python@v5
with:
cache: pip
cache-dependency-path: ${{ steps.inputs.outputs.working_directory }}/.git/ansible-lint-requirements.txt
cache-dependency-path: ${{ steps.get_reqs.outputs.reqs_file }}
python-version: "3.11"

- name: Install ansible-lint
Expand All @@ -78,4 +85,8 @@ runs:
- name: Run ansible-lint
shell: bash
working-directory: ${{ steps.inputs.outputs.working_directory }}
run: ansible-lint ${{ inputs.args }}
run: |
exit_code=0
expected_exit_code=${{ inputs.expected_return_code }}
ansible-lint ${{ inputs.args }} || exit_code=$?
if [ "$exit_code" != "$expected_exit_code" ]; then echo "Command failed: got '$exit_code', expected '$expected_exit_code'"; exit 1; fi
16 changes: 16 additions & 0 deletions docs/installing.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,24 @@ jobs:
- uses: actions/checkout@v4
- name: Run ansible-lint
uses: ansible/ansible-lint@main
# optional (see below):
with:
args: ""
setup_python: "true"
working_directory: ""
requirements_file: ""
```
All the arguments are optional and most users should not need them:
- `args`: Arguments to be passed to ansible-lint command.
- `setup_python`: If python should be installed. Default is `true`.
- `working_directory`: The directory where to run ansible-lint from. Default is
`github.workspace`. That might be needed if you want to lint only a subset of
your repository.
- `requirements_file`: Path to the requirements.yml file to install role and
collection dependencies.

Due to limitations on how GitHub Actions are processing arguments, we do not
plan to provide extra options. You will have to make use of
[ansible-lint own configuration file](https://ansible.readthedocs.io/projects/lint/configuring/)
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
},
"meta": {
"etag": "fdff861b226b13b711dd7f94301ed5becd6dc5d8d4e872f909d4a3d8133d600a",
"etag": "57fd2147c4c70dfd7dea6b3960bb8ed910c01c693de168cc7ac2b8cf527a1166",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
},
"meta-runtime": {
Expand Down

0 comments on commit 4e3058f

Please sign in to comment.