1
0
Fork 0
mirror of https://github.com/karancode/yamllint-github-action.git synced 2024-08-16 10:19:48 +02:00

Action v1 (#1)

* initial

* test -x

* bugfix

* bash -> sh

* fix

* fix condition & install jq,curl

* fix message

* fix

* readme

* icon
This commit is contained in:
Karan Thanvi 2019-12-24 01:22:54 +09:00 committed by GitHub
parent 565a77661f
commit c350bd84d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 201 additions and 1 deletions

11
Dockerfile Normal file
View file

@ -0,0 +1,11 @@
FROM python:3.7-alpine
RUN apk add --update --no-cache bash ca-certificates curl git jq openssh
RUN pip install yamllint
RUN ["bin/sh", "-c", "mkdir -p /src"]
COPY ["src", "/src/"]
ENTRYPOINT ["/src/entrypoint.sh"]

View file

@ -1,2 +1,58 @@
# yamllint-github-action
Github Action for linting yaml files using yamllint
Yamllint GitHub Actions allow you to execute `yamllint` command within GitHub Actions.
The output of the actions can be viewed from the Actions tab in the main repository view. If the actions are executed on a pull request event, a comment may be posted on the pull request.
Yamllint GitHub Actions is a single GitHub Action that can be executed on different directories depending on the content of the GitHub Actions YAML file.
## Success Criteria
An exit code of `0` is considered a successful execution.
## Usage
The most common usage is to run `yamllint` on a file/directory. A comment will be posted to the pull request depending on the output of the Yamllint command being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file.
```yaml
name: 'Yamllint GitHub Actions'
on:
- pull_request
jobs:
yamllint:
name: 'Yamllint'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@master
- name: 'Yamllint'
uses: karancode/yamllint-github-action@master
with:
yamllint_file_or_dir: '<yaml_file_or_dir>'
yamllint_strict: false
yamllint_comment: true
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
```
This was a simplified example showing the basic features of this Yamllint GitHub Actions.
# Inputs
Inputs configure Yamllint GitHub Actions to perform lint action.
* `yamllint_file_or_dir` - (Optional) The file or directory to run `yamllint` on (assumes that the directory contains *.yaml file). Defaults to `.`.
* `yamllint_strict` - (Optional) Yamllint strict option. Defaults to `false`.
* `yamllint_config_filepath` - (Optional) Path to a custom config file. Defaults to default configs.
* `yamllint_config_datapath` - (Optional) Custom configuration (as YAML source). Defaults to default configs.
* `yamllint_format` - (Optional) Format for parsing output. Defaults to `auto`.
* `yamllint_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `false`.
## Outputs
Outputs are used to pass information to subsequent GitHub Actions steps.
* `yamllint_output` - The Yamllint build outputs.
## Secrets
Secrets are similar to inputs except that they are encrypted and only used by GitHub Actions. It's a convenient way to keep sensitive data out of the GitHub Actions workflow YAML file.
* `GITHUB_ACCESS_TOKEN` - (Optional) The GitHub API token used to post comments to pull requests. Not required if the `yamllint_comment` input is set to `false`.

38
actions.yaml Normal file
View file

@ -0,0 +1,38 @@
# action.yaml
name: 'Yamllint Github Action'
author: 'karancode <karanthanvi0@gmail.com>'
description: 'Github action for Yamllint - manily to liniting yaml files for k8s config'
branding:
icon: 'sliders'
color: 'green'
inputs:
yamllint_file_or_dir:
description: 'File or Directory to do yamllint on'
required: false
default: '.'
yamllint_strict:
description: 'Use Strict'
required: false
default: '0'
yamllint_config_filepath:
description: 'Custom yamllint config path'
required: false
default: '' # using default
yamllint_config_datapath:
description: 'Custom configuration (as YAML source)'
required: false
default: '' # using default
yamllint_format:
description: 'format for parsing output'
required: false
default: '' # using default
yamllint_comment:
description: 'Comment yamllint output'
required: false
default: '0'
outputs:
yamllint_output:
description: 'Output of yamllint'
runs:
using: 'docker'
image: 'Dockerfile'

47
src/entrypoint.sh Executable file
View file

@ -0,0 +1,47 @@
#!/bin/sh
function parse_inputs {
yamllint_file_or_dir=""
if [ "${INPUT_YAMLLINT_FILE_OR_DIR}" != "" ] || [ "${INPUT_YAMLLINT_FILE_OR_DIR}" != "." ]; then
yamllint_file_or_dir="${INPUT_YAMLLINT_FILE_OR_DIR}"
fi
yamllint_strict=''
if [ "${INPUT_YAMLLINT_STRICT}" != "0" ] || [ "${INPUT_YAMLLINT_STRICT}" != "false" ]; then
yamllint_strict="--strict"
fi
yamllint_config_filepath=''
if [ ! -z "${INPUT_YAMLLINT_CONFIG_FILEPATH}" ]; then
yamllint_config_filepath="--config-file ${INPUT_YAMLLINT_CONFIG_FILEPATH}"
fi
yamllint_config_datapath=''
if [ ! -z "${INPUT_YAMLLINT_CONFIG_DATAPATH}" ]; then
yamllint_config_datapath="--config-data ${INPUT_YAMLLINT_CONFIG_DATAPATH}"
fi
yamllint_format=''
if [ ! -z "${INPUT_YAMLLINT_FORMAT}" ]; then
yamllint_format="--format ${INPUT_YAMLLINT_FORMAT}"
fi
yamllint_comment=0
if [ "${INPUT_YAMLLINT_COMMENT}" != "0" ] || [ "${INPUT_YAMLLINT_COMMENT}" != "false" ]; then
yamllint_comment="1"
fi
}
function main {
scriptDir=$(dirname ${0})
source ${scriptDir}/yaml_lint.sh
parse_inputs
yaml_lint
}
main "${*}"

48
src/yaml_lint.sh Executable file
View file

@ -0,0 +1,48 @@
#!/bin/sh
function yaml_lint {
# gather output
echo "lint: info: yamllint on ${yamllint_file_or_dir}."
lint_output=$(yamllint ${yamllint_strict} ${yamllint_config_filepath} ${yamllint_config_datapath} ${yamllint_format} ${yamllint_file_or_dir})
lint_exit_code=${?}
# exit code 0 - success
if [ ${lint_exit_code} -eq 0 ];then
lint_comment_status="Success"
echo "lint: info: successful yamllint on ${yamllint_file_or_dir}."
echo "${lint_output}"
echo
fi
# exit code !0 - failure
if [ ${lint_exit_code} -ne 0 ]; then
lint_comment_status="Failed"
echo "lint: error: failed yamllint on ${yamllint_file_or_dir}."
echo "${lint_output}"
echo
fi
# comment
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ] && [ "${yamllint_comment}" == "1" ]; then
lint_comment_wrapper="#### \`yamllint\` ${lint_comment_status}
<details><summary>Show Output</summary>
\`\`\`
${lint_output}
\`\`\`
</details>
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Lint: \`${yamllint_file_or_dir}\`*"
echo "lint: info: creating json"
lint_payload=$(echo "${lint_comment_wrapper}" | jq -R --slurp '{body: .}')
lint_comment_url=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "lint: info: commenting on the pull request"
echo "${lint_payload}" | curl -s -S -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" --header "Content-Type: application/json" --data @- "${lint_comment_url}" > /dev/null
fi
echo ::set-output name=yamllint_output::${lint_output}
exit ${lint_exit_code}
}