From 1e8050d959bcbff6cdd7318d6f59e0a472425c8a Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Thu, 20 Apr 2023 12:25:19 -0500 Subject: [PATCH] Converts the action to a composite action (#9) --- .python-version | 1 + Dockerfile | 15 ------------- README.md | 4 ++++ action.yml | 58 +++++++++++++++++++++++++++++++++++++++---------- src/render.sh | 13 +++++++---- 5 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 .python-version delete mode 100644 Dockerfile diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..36435ac --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.10.8 diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 03b52aa..0000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM python:3.10.1-alpine3.15 - -RUN apk add --update-cache \ - bash \ - && rm -rf /var/cache/apk/* - -WORKDIR /opt/app - -COPY requirements.txt . -RUN pip install -r requirements.txt - -COPY src/render.sh . -RUN chmod +x render.sh - -CMD /opt/app/render.sh diff --git a/README.md b/README.md index 1a64933..9bc3910 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,9 @@ Renders Jinja2 Templates via the [J2 cli](https://github.com/kolypto/j2cli). # The name of the output file to write rendered contents to # Default: output output: '' + + # If true, sets -x in the shell command + # Default: false + debug: '' ``` diff --git a/action.yml b/action.yml index 0fb068b..002830e 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,8 @@ +--- + name: 'Render J2 Template' description: 'Renders a J2 Template' + inputs: template: description: 'The path to the template file to render' @@ -43,19 +46,50 @@ inputs: description: 'The name of the output file to write rendered contents to' required: false default: output + debug: + description: 'If true, sets -x in the shell command' + required: false + default: false + outputs: file: description: 'The file the rendered content was written to' + value: ${{ steps.render.outputs.file }} + content: + description: 'The rendered content' + value: ${{ steps.render.outputs.content }} + runs: - using: 'docker' - image: 'Dockerfile' - env: - TEMPLATE: ${{ inputs.template }} - DATA: ${{ inputs.data }} - FORMAT: ${{ inputs.format }} - ENV_VARS: ${{ inputs.env_vars }} - FILTERS: ${{ inputs.filters }} - TESTS: ${{ inputs.tests }} - CUSTOMIZE: ${{ inputs.customize }} - UNDEFINED: ${{ inputs.undefined }} - OUTPUT: ${{ inputs.output }} + using: 'composite' + steps: + - name: 'Setup python' + uses: actions/setup-python@v4 + with: + python-version-file: ${{ github.action_path }}/.python-version + + - name: 'Cache python dependencies' + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles(join(fromJSON('["${{ github.action_path }}", "requirements.txt"]'), '/')) }} + restore-keys: ${{ runner.os }}-pip- + + - name: 'Install dependencies' + shell: bash + run: pip install -r "${{ github.action_path }}/requirements.txt" + + - name: 'Render' + id: render + shell: bash + run: "${{ github.action_path }}/src/render.sh" + env: + TEMPLATE: ${{ inputs.template }} + DATA: ${{ inputs.data }} + FORMAT: ${{ inputs.format }} + ENV_VARS: ${{ inputs.env_vars }} + FILTERS: ${{ inputs.filters }} + TESTS: ${{ inputs.tests }} + CUSTOMIZE: ${{ inputs.customize }} + UNDEFINED: ${{ inputs.undefined }} + OUTPUT: ${{ inputs.output }} + DEBUG: ${{ inputs.debug }} diff --git a/src/render.sh b/src/render.sh index 6d42148..4adf369 100755 --- a/src/render.sh +++ b/src/render.sh @@ -19,6 +19,10 @@ function set-output() { function render() { set -eo pipefail + if [ "${DEBUG:-false}" = 'true' ]; then + set -x + fi + # Validation [ -n "$TEMPLATE" ] || { @@ -29,10 +33,13 @@ function render() { for var in Template Data Filters Tests Customize; do VAR="${var^^}" val="${!VAR}" - [ -z "$val" ] || [ -f "$val" ] || { + if [ -n "$val" ] && ! [ -f "$val" ]; then + whoami + ls -al "$(dirname "$val")" + ls -al "$val" echo "[ERROR] $var file not found: $val" >&2 return 2 - } + fi done [ -z "$UNDEFINED" ] || [ "$UNDEFINED" = true ] || [ "$UNDEFINED" = false ] || { @@ -70,8 +77,6 @@ function render() { COMMAND+=("$TEMPLATE") [ -z "$DATA" ] || COMMAND+=("$DATA") - set -x - which j2 echo "[DEBUG] ${COMMAND[*]}" >&2 "${COMMAND[@]}"