Skip to content

Commit 32c469c

Browse files
authored
Update Yamale to v4.x (#5)
* docker: run as non-root and use venv * chore: upgrade yamale to v4.0.2 BREAKING CHANGE * feat: rewrite entrypoint for v4 support BREAKING CHANGE * docs: update readme and examples for v4 * chore: remove example files from Docker image
1 parent 075382b commit 32c469c

17 files changed

Lines changed: 183 additions & 47 deletions

.github/workflows/tests.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Action tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
pull_request:
9+
branches:
10+
- master
11+
- main
12+
13+
jobs:
14+
schema:
15+
name: Run tests
16+
runs-on: ubuntu-latest
17+
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
test:
22+
# Strict
23+
- suite: nostrict
24+
schema: ./tests/nostrict/schema.yaml
25+
target: ./tests/nostrict/valid-matching-fields.yaml
26+
no-strict: true
27+
error-is-success: false
28+
- suite: nostrict
29+
schema: ./tests/nostrict/schema.yaml
30+
target: ./tests/nostrict/valid-extra-argument.yaml
31+
no-strict: true
32+
error-is-success: false
33+
- suite: nostrict
34+
schema: ./tests/nostrict/schema.yaml
35+
target: ./tests/nostrict/invalid-not-a-boolean.yaml
36+
no-strict: true
37+
error-is-success: true
38+
- suite: nostrict
39+
schema: ./tests/nostrict/schema.yaml
40+
target: ./tests/nostrict/invalid-nums-out-of-range.yaml
41+
no-strict: true
42+
error-is-success: true
43+
44+
# Nostrict
45+
- suite: strict
46+
schema: ./tests/strict/schema.yaml
47+
target: ./tests/strict/valid-matching-fields.yaml
48+
no-strict: false
49+
error-is-success: false
50+
- suite: strict
51+
schema: ./tests/strict/schema.yaml
52+
target: ./tests/strict/invalid-extra-argument.yaml
53+
no-strict: false
54+
error-is-success: true
55+
56+
steps:
57+
- uses: actions/checkout@v2
58+
- uses: ./
59+
name: "Test: ${{ matrix.test.suite }} ${{ matrix.test.target }}"
60+
with:
61+
schema: ${{ matrix.test.schema }}
62+
target: ${{ matrix.test.target }}
63+
no-strict: ${{ matrix.test.no-strict }}
64+
error-is-success: ${{ matrix.test.error-is-success }}

Dockerfile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
FROM python:3.8-alpine
1+
FROM alpine:3
22

3-
RUN apk add --no-cache bash
3+
RUN apk add --no-cache bash python3 py3-pip \
4+
&& mkdir -p /usr/src/app \
5+
&& addgroup -g 10000 app \
6+
&& adduser -s /bin/bash -G app -u 10000 -h /usr/src/app -k /dev/null -D app \
7+
&& python3 -m venv /usr/src/app/venv \
8+
&& chown -R app:app /usr/src/app
49

510
WORKDIR /usr/src/app
11+
USER 10000:10000
12+
ENV VIRTUAL_ENV=/usr/src/app/venv \
13+
PATH=/usr/src/app/venv/bin:$PATH
614

715
COPY requirements.txt ./
816
RUN pip install -r requirements.txt
917

10-
COPY example/ ./example
11-
12-
# Nonexistent
13-
USER 2000:2000
14-
1518
COPY entrypoint.sh /entrypoint.sh
1619
ENTRYPOINT ["/entrypoint.sh"]

README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A GitHub action that uses [Yamale][] for YAML schema validation.
55
## Usage
66

77
- Filenames are relative to the repository root.
8-
- Enable strict checking by setting `strict` to a non-empty string.
8+
- Disable strict checking by setting `no-strict` to `true`, `1` or `yes`.
99
- For help with the schema definitions and reference, see [Yamale][].
1010

1111
The following example sets up a check to validate a YAML file in your
@@ -20,25 +20,27 @@ jobs:
2020
runs-on: ubuntu-latest
2121
steps:
2222
- uses: actions/checkout@v2
23-
- uses: nrkno/yaml-schema-validator-github-action@master
23+
- uses: nrkno/yaml-schema-validator-github-action@v4
2424
with:
25-
schema: 'schemas/schema.yaml'
26-
target: 'target.yaml'
27-
# Uncomment to enable strict checks
28-
# strict: '1'
25+
schema: schemas/schema.yaml
26+
target: target.yaml
27+
# Uncomment to disable strict checks
28+
# no-strict: true
2929
```
3030

31-
### Versioning
31+
## Versioning
3232

33-
To bind the action to a specific release, prefix with `@<tag>`.
34-
E.g. `nrkno/yaml-schema-validator-github-action@v0.1.0`.
33+
This action is meant to be a wrapper around Yamale, so as of version 4.x
34+
of Yamale, this action will follow Yamale's major version scheme.
35+
36+
To bind the action to a specific release, suffix with `@<tag>`.
37+
E.g. `nrkno/yaml-schema-validator-github-action@v4`.
3538

3639
https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses
3740

3841
## Developing
3942

40-
Create and enable a Python virtualenv (not strictly required, but makes testing
41-
more robust)
43+
Create and enable a Python virtualenv
4244

4345
```
4446
$ python -m venv venv
@@ -51,21 +53,12 @@ Install dependencies
5153
$ pip install -r requirements.txt
5254
```
5355

54-
Do a test-run with the provided examples
56+
Do a test-run with one of the provided examples
5557

5658
```
57-
$ ./entrypoint.sh example/schema.yaml example/file.yaml
59+
$ INPUT_SCHEMA=example/schema.yaml \
60+
INPUT_TARGET=example/file-valid-strict.yaml \
61+
./entrypoint.sh
5862
```
5963

60-
### Using Docker
61-
62-
Build the container and reference files within the example/ folder.
63-
64-
```
65-
$ docker build -t yaml-schema-validator .
66-
$ docker run yaml-schema-validator example/schema.yaml example/file.yaml
67-
$ docker run yaml-schema-validator example/schema.yaml example/file-invalid.yaml
68-
```
69-
70-
7164
[Yamale]: https://github.com/23andMe/Yamale

action.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ inputs:
1111
target:
1212
description: 'File to validate'
1313
required: true
14-
strict:
15-
description: 'Enable strict mode. Set to a non-empty string to enable.'
14+
no-strict:
15+
description: 'Disable strict mode'
16+
required: false
17+
error-is-success:
18+
description: 'Flip the validation logic making a failing test pass and a passing test fail. This is used internally for testing the action itself.'
1619
required: false
1720
runs:
1821
using: 'docker'
1922
image: 'Dockerfile'
23+
env:
24+
# Need to convert dashes to underscores in environment variable names for bash
25+
# to be able to read them.
26+
# Github Actions passes them like INPUT_NO-STRICT when they contain dashes.
27+
INPUT_NO_STRICT: ${{ inputs.no-strict }}
28+
INPUT_ERROR_IS_SUCCESS: ${{ inputs.error-is-success }}

entrypoint.sh

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,54 @@
11
#!/bin/bash
22
set -eux
33

4-
strict=''
5-
schema=${INPUT_SCHEMA:-$1}
6-
target=${INPUT_TARGET:-$2}
4+
# Returns a string `true` the string is considered boolean true,
5+
# otherwise `false`. An empty value is considered false.
6+
function str_bool {
7+
local str="${1:-false}"
8+
local pat='^(true|1|yes)$'
9+
if [[ "$str" =~ $pat ]]
10+
then
11+
echo 'true'
12+
else
13+
echo 'false'
14+
fi
15+
}
716

8-
if [ -n "${INPUT_STRICT:-}" ]
9-
then
10-
strict='--strict'
11-
fi
17+
schema="$INPUT_SCHEMA"
18+
target="$INPUT_TARGET"
19+
no_strict=$(str_bool "${INPUT_NO_STRICT:-}")
20+
error_is_success=$(str_bool "${INPUT_ERROR_IS_SUCCESS:-}")
21+
22+
# Must end with a space here
23+
extra_args=' '
1224

13-
if [ ! -e ${schema} ]
25+
if [ ! -e "${schema}" ]
1426
then
1527
>&2 echo "Schema does not exist: $schema"
1628
exit 1
1729
fi
1830

19-
if [ ! -e ${target} ]
31+
# TODO: Allow directories
32+
if [ ! -e "${target}" ]
2033
then
2134
>&2 echo "Target does not exist: $target"
2235
exit 1
2336
fi
2437

25-
yamale --schema=${schema} $target $strict
38+
if [ "$no_strict" = "true" ]
39+
then
40+
extra_args='--no-strict '
41+
fi
42+
43+
if [ "$error_is_success" = "true" ]
44+
then
45+
# Flipped validation logic
46+
echo "--- Flipped validation logic enabled (error-is-success: true)! ---"
47+
# shellcheck disable=SC2086
48+
yamale $extra_args --schema="${schema}" "$target" && exit 1
49+
exit 0
50+
fi
51+
52+
# Normal execution
53+
# shellcheck disable=SC2086
54+
yamale $extra_args --schema="${schema}" "$target"

example/file-invalid.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: Bill
2-
age: 26
3-
height: 6.2
4-
awesome: "pretty"
2+
age: 42000
3+
height: "not a number"
4+
awesome: "not a boolean"

example/file-valid-strict.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: Bill
2+
age: 26
3+
height: 6.2
4+
awesome: True

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
yamale~=2.2.0
1+
yamale~=4.0.2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: Bill
2+
age: 26
3+
height: 6.2
4+
awesome: "not a boolean"

0 commit comments

Comments
 (0)