Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Update the action and inherit ansible-lint arguments #7

Merged
merged 7 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Ansible Lint
description: Run Ansible Lint
author: Ansible by Red Hat <info@ansible.com>

inputs:
args:
description: |
Arguments to be passed to the ansible-lint

Options:
-q quieter, although not silent output
-p parseable output in the format of pep8
--parseable-severity parseable output including severity of rule
-r RULESDIR specify one or more rules directories using one or
more -r arguments. Any -r flags override the default
rules in ansiblelint/rules, unless -R is also used.
-R Use default rules in ansiblelint/rules in addition to
any extra
rules directories specified with -r. There is no need
to specify this if no -r flags are used
-t TAGS only check rules whose id/tags match these values
-x SKIP_LIST only check rules whose id/tags do not match these
values
--nocolor disable colored output
--exclude=EXCLUDE_PATHS
path to directories or files to skip. This option is
repeatable.
-c C Specify configuration file to use. Defaults to ".ansible-lint"

required: false
targets:
description: |
Paths to ansible files (i.e., playbooks, tasks, handlers etc..)
or valid Ansible directories according to the Ansible role
directory structure.
required: true
runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.args }}
env:
TARGETS: ${{ inputs.targets }}
103 changes: 84 additions & 19 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,95 @@
#! /usr/bin/env bash

set -eo pipefail
set -Eeuo pipefail
set -x

ACTION_PLAYBOOK_NAME="${ACTION_PLAYBOOK_NAME:-playbook.yml}"
# Filter out arguments that are not available to this action
# args:
# $@: Arguments to be filtered
parse_args() {
local opts=""
while (( "$#" )); do
case "$1" in
-q|--quiet)
opts="$opts -q"
shift
;;
-c)
opts="$opts -c $2"
shift 2
;;
-p)
opts="$opts -p"
shift
;;
-r)
opts="$opts -r $2"
shift 2
;;
-R)
opts="$opts -R"
shift
;;
-t)
opts="$opts -t $2"
shift 2
;;
-x)
opts="$opts -x $2"
shift 2
;;
--exclude)
opts="$opts --exclude=$2"
shift 2
;;
--no-color)
opts="$opts --no-color"
shift
;;
--parseable-severity)
opts="$opts --parseable-severity"
shift
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
>&2 echo "ERROR: Unsupported flag: '$1'"
exit 1
;;
*) # positional arguments
shift # ignore
;;
esac
done

set -u
# set remaining positional arguments (if any) in their proper place
eval set -- "$opts"

cd "${GITHUB_WORKSPACE}"
echo "${opts/ /}"
return 0
}

ACTION_PLAYBOOK_PATH="${GITHUB_WORKSPACE}/${ACTION_PLAYBOOK_NAME}"
# Generates client.
# args:
# $@: additional options
# env:
# [required] TARGETS : Files or directories (i.e., playbooks, tasks, handlers etc..) to be linted
ansible::lint() {
: "${TARGETS?No targets to check. Nothing to do.}"
: "${GITHUB_WORKSPACE?GITHUB_WORKSPACE has to be set. Did you use the actions/checkout action?}"
pushd ${GITHUB_WORKSPACE}

if [ ! -f "${ACTION_PLAYBOOK_PATH}" -a ! -d "${ACTION_PLAYBOOK_PATH}" ]; then
>&2 echo "==> Can't find '${ACTION_PLAYBOOK_PATH}'.
Please ensure to set up ACTION_PLAYBOOK_NAME env var
relative to the root of your project."
exit 1
fi
local opts=$(parse_args "$@" || exit 1)

>&2 echo
>&2 echo "==> Linting ${ACTION_PLAYBOOK_PATH}…"
ansible-lint -v --force-color $opts ${TARGETS}
}

if [ -d "${ACTION_PLAYBOOK_PATH}" ]; then
ansible-lint `find "${ACTION_PLAYBOOK_PATH}" -type f -name playbook.yml`
else
ansible-lint "${ACTION_PLAYBOOK_PATH}"
fi

>&2 echo
args=("$@")

if [ "$0" = "$BASH_SOURCE" ] ; then
>&2 echo -E "\nRunning Ansible Lint...\n"
ansible::lint ${args[@]}
fi
75 changes: 66 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,78 @@ This action allows you to run `ansible-lint` with no additional options.


## Usage
To use the action simply add the following lines to your `.github/main.workflow`.
To use the action simply create an `ansible-lint.yml` (or choose custom `*.yml` name) in the `.github/workflows/` directory.

For example:

```yaml
name: Ansible Lint # feel free to pick your own name

on: push

jobs:
build:

runs-on: ubuntu-latest

steps:
# Important: This sets up your GITHUB_WORKSPACE environment variable
- uses: actions/checkout@v1
- name: Lint Ansible Playbook
# replace "master" with any valid ref
uses: ansible/ansible-lint-action@master
with:
# [required]
# Paths to ansible files (i.e., playbooks, tasks, handlers etc..)
# or valid Ansible directories according to the Ansible role
# directory structure.
targets: ""
# [optional]
# Arguments to be passed to the ansible-lint

# Options:
# -q quieter, although not silent output
# -p parseable output in the format of pep8
# --parseable-severity parseable output including severity of rule
# -r RULESDIR specify one or more rules directories using one or
# more -r arguments. Any -r flags override the default
# rules in ansiblelint/rules, unless -R is also used.
# -R Use default rules in ansiblelint/rules in addition to
# any extra
# rules directories specified with -r. There is no need
# to specify this if no -r flags are used
# -t TAGS only check rules whose id/tags match these values
# -x SKIP_LIST only check rules whose id/tags do not match these
# values
# --nocolor disable colored output
# --exclude=EXCLUDE_PATHS
# path to directories or files to skip. This option is
# repeatable.
# -c C Specify configuration file to use. Defaults to ".ansible-lint"
args: ""

```hcl
action "Lint Ansible Playbook" {
uses = "ansible/ansible-lint-action@master"
}
```

N.B. Use `v4.1.0` or any other valid tag, or branch, or commit SHA instead
of `master` to pin the action to use a specific version.
> TIP: N.B. Use `ansible/ansible-lint-action@v4.1.0` or any other valid tag, or branch, or commit SHA instead of `v4.1.0` to pin the action to use a specific version.

Alternatively, you can run the ansible lint only on certain branches:

```yaml

on:
push:
branches:
- stable
- release/v*
```

or on various [events](https://help.github.com/en/articles/events-that-trigger-workflows):

### Environment Variables
- **ACTION_PLAYBOOK_NAME**: (optional) defaults to `playbook.yml`
```yaml
on: [push, pull_request]
```

<br>

## License
The Dockerfile and associated scripts and documentation in this project are released under the [MIT](license).
Expand Down