Coder Social home page Coder Social logo

jinja2-action's Introduction

release marketplace

Jinja2 is a fast and straightforward templating engine. You can use this action to easily run it in your GitHub workflows.

Using input variables

- name: Setup nginx
  uses: cuchi/[email protected]
  with:
    template: infra/nginx.conf.j2
    output_file: infra/nginx.conf
    strict: true
    variables: |
      server_host=staging.example.com
      timeout=30s

Using data files

- name: Setup nginx
  uses: cuchi/[email protected]
  with:
    template: infra/nginx.conf.j2
    output_file: infra/nginx.conf
    data_file: staging_config.json
    data_format: json # Will try to guess from the extension instead (unnecessary in this case)

Using environment variables

- name: Setup nginx
  uses: cuchi/[email protected]
  with:
    template: infra/nginx.conf.j2
    output_file: infra/nginx.conf
  env:
    SERVER_HOST: staging.example.com

Environment variables are used this way in the template file:

{{ env['SERVER_HOST'] }} <-- This is always strict
{{ env.get('SERVER_HOST') }} <-- This is never strict, and displays `None` if you don't specify a default value

See also

jinja2-action's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jinja2-action's Issues

Refactor & add unit tests

This project is working very well and there is no known bugs that I`m aware of ๐Ÿ˜„

But we can still address some improvements:

  • There are no unit tests (only integration ones), it would be nice to have some of them, so we can make better assumptions about our code.
  • Since we need to implement unit tests, we also need to make the code testable. In order to do this, we need to have isolated functions to have them tested in a unitary way.

See: https://docs.python.org/3/library/unittest.html

How to use jinja loop variable as env variable name?

Hi guys, this could be more a question to Github actions than to your project, but maybe you have some handy answer...

I am trying to understand how to use sensitive data together with Jinja and Actions so I'm gonna hide the hashed passwords in github env variables:

{% for project in projects %}
{% if project.auth.enabled=true|default(false) %}
       {{ project.dashedname }}-auth.basicauth.users={{ env['HERE-GOES-MY-QUESTION'] }}
{% endif %}
{% endfor %}

So I'm gonna have some yamled structure to fill this template and a list of variables named like 'my-project1-hashed-password' in github env, How can I use loop variable to pick the env variable?

Alternatively - maybe I'm just doing it wrong and I can just put env names into data_file somehow?

Please advise.

[Enhancement] Allow variables to be set from a variable

In one project I wanted to use the action to create the body for the Release, considering information that I store in env variables or bash variables. I have tried a few things to make it work with jinja2-action, but I had no success as the variable was always being evaluated as a text.
It would be very interesting to have the action working with variables from the environment instead of being a hardcoded string in the configuration.

No module named 'imp'

Traceback (most recent call last):
  File "/app/entrypoint.py", line 5, in <module>
    from main import Context
  File "/app/main.py", line 2, in <module>
    from j2cli.context import read_context_data
  File "/usr/local/lib/python3.12/site-packages/j2cli/__init__.py", line 10, in <module>
    from j2cli.cli import main
  File "/usr/local/lib/python3.12/site-packages/j2cli/cli.py", line 8, in <module>
    import imp, inspect
ModuleNotFoundError: No module named 'imp'

If I use Python version as 3.12 then this error is thrown. According to documentation, imp is deprecated in Python version 3.12.

Doesn't create a file if it doesn't exist already

If the target file doesn't already exist, then this Action throws an exception.

Run cuchi/[email protected]
/usr/bin/docker run --name a682585caf9ec008f4fbaaf8ac6c0d38484c3_bfdb60 --label 6a6825 --workdir /github/workspace --rm -e INPUT_TEMPLATE -e INPUT_OUTPUT_FILE -e INPUT_STRICT -e INPUT_VARIABLES -e INPUT_DATA_FILE -e INPUT_DATA_FORMAT -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_RUN_ATTEMPT -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_REF_NAME -e GITHUB_REF_PROTECTED -e GITHUB_REF_TYPE -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e RUNNER_OS -e RUNNER_ARCH -e RUNNER_NAME -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/intro-github-actions/intro-github-actions":"/github/workspace" 6a6825:85caf9ec008f4fbaaf8ac6c0d38484c3
Traceback (most recent call last):
  File "/entrypoint.py", line 38, in <module>
    with open(os.environ['INPUT_OUTPUT_FILE'], 'w') as file:
FileNotFoundError: [Errno 2] No such file or directory: '$HOME/.rclone.conf'

Add support for templating without the need for files

I want to propose adding support for compiling templates without needing files. An example use case would be something along the lines of this,

- id: pr-message
  name: Create PR Message
  uses: cuchi/[email protected]
  with:
    template_str: |
      Hello! You've created a PR that has {{ num_files_touched }} files changed!

      {% for file in files %}
        - {{ file }}
      {% endfor %}
    strict: true
    variables: |
      files=...
      num_files_touched=30
- name: Create PR
  uses: peter-evans/create-pull-request@v5
  with:
    body: ${{ steps.pr-message.outputs.contents }}

Would this be feasible? A major version bump might be necessary since template and output_file will no longer be required. If output_file is omitted, it saves the contents to the step's outputs. Thoughts on the proposal?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.