<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vasily Loginov</title>
    <description>The latest articles on Forem by Vasily Loginov (@karatheodory).</description>
    <link>https://forem.com/karatheodory</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F192668%2Fe1c75ec4-97bb-4a54-89de-92660b87c299.jpeg</url>
      <title>Forem: Vasily Loginov</title>
      <link>https://forem.com/karatheodory</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/karatheodory"/>
    <language>en</language>
    <item>
      <title>Passing dynamic number of parameters to a reusable Github Actions workflow</title>
      <dc:creator>Vasily Loginov</dc:creator>
      <pubDate>Sun, 16 Nov 2025 14:22:23 +0000</pubDate>
      <link>https://forem.com/karatheodory/passing-dynamic-number-of-parameters-to-a-reusable-github-actions-workflow-2bcl</link>
      <guid>https://forem.com/karatheodory/passing-dynamic-number-of-parameters-to-a-reusable-github-actions-workflow-2bcl</guid>
      <description>&lt;h2&gt;
  
  
  Source code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://github.com/karatheodory/gh-action-template-file/blob/main/.github/workflows/template-file.yaml" rel="noopener noreferrer"&gt;&lt;code&gt;template-file&lt;/code&gt; action&lt;/a&gt; - contains the actual implementation of the templating.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/karatheodory/gh-action-template-file/blob/main/.github/workflows/manual-run.yaml" rel="noopener noreferrer"&gt;An example of how to call the action&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Github Actions and reusable workflows
&lt;/h2&gt;

&lt;p&gt;Github Actions are one of the best tools for building and publishing your systems (especially when your company already uses Github, and especially when you don't even have a choice, welcome to enterprise development).&lt;/p&gt;

&lt;p&gt;Github Actions have &lt;a href="https://resources.github.com/learn/pathways/automation/intermediate/create-reusable-workflows-in-github-actions/" rel="noopener noreferrer"&gt;a concept of reusable workflows&lt;/a&gt;. Those allow you to expose / reuse parts of your actions to other parties, thus giving a possibility of creating a standardized flow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Github itself provides several reusable workflows, and you've probably already used them many times. One of them is a &lt;a href="https://github.com/actions/checkout" rel="noopener noreferrer"&gt;checkout action&lt;/a&gt; which allows you to check out a repo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Passing dynamic number of parameters
&lt;/h2&gt;

&lt;p&gt;Reusable workflows allow defining a set of input and output params. The above &lt;code&gt;checkout&lt;/code&gt; action can be a relatively comprehensive example (&lt;a href="https://github.com/actions/checkout/blob/main/action.yml" rel="noopener noreferrer"&gt;see here&lt;/a&gt;). And while in most of the cases it it is convenient to specify all the inputs as static variables, in some cases you don't know all the parameters in advance.&lt;/p&gt;

&lt;p&gt;One of the examples is config templating. The templating action itself depends on the variables the config file contents, which are not known in advance. In this article I will go through one of the approaches to pass dynamic number of parameters using JSON and create a &lt;code&gt;template-file&lt;/code&gt; action as an example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assumptions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inputs are considered trusted.&lt;/strong&gt; The approach below allows someone to introduce a list of env vars into your workflow. While in most of the cases it doesn't matter, because the context of the action is defined by the caller, please take a second to see what an attacker can do with the action's environment in your particular case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make sure that you need it.&lt;/strong&gt; Please note it is always better to be explicit if you can, and specify all the parameters as inputs. If you are not sure - don't use this approach, and go with the standard static inputs instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  General approach
&lt;/h2&gt;

&lt;p&gt;To do that we will use the following approach&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduce a JSON object with all the variables we need as one of the inputs;&lt;/li&gt;
&lt;li&gt;Convert the variables from the object into the environment in the action using &lt;code&gt;jq&lt;/code&gt; which is included in the actions by default;&lt;/li&gt;
&lt;li&gt;Introduce them into the environment using &lt;code&gt;export&lt;/code&gt; and &lt;code&gt;eval&lt;/code&gt; statements;&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;envsubst&lt;/code&gt; to do the actual substitution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 0: Passing multiline strings
&lt;/h2&gt;

&lt;p&gt;We will be getting our dynamic params as a JSON object. To pass a JSON string to an action, you can use &lt;a href="https://yaml-multiline.info/" rel="noopener noreferrer"&gt;one of the YAML miltiline strings&lt;/a&gt;, for example here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;      &lt;span class="na"&gt;TEMPLATE_VARS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;{&lt;/span&gt;
          &lt;span class="s"&gt;"APP_ENVIRONMENT": "dev",&lt;/span&gt;
          &lt;span class="s"&gt;"LOG_LEVEL": "DEBUG"&lt;/span&gt;
        &lt;span class="s"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Checking out the repository
&lt;/h2&gt;

&lt;p&gt;In order to access the repo code in the action, we'd need to checkout it. We can use the above &lt;code&gt;checkout&lt;/code&gt; action provided by Github:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout&lt;/span&gt;
    &lt;span class="s"&gt;uses&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Converting JSON variables into the environment variables
&lt;/h2&gt;

&lt;p&gt;Here is where the (standard Linux command) magic happens!&lt;/p&gt;

&lt;p&gt;We can define our action step as the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Template File&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="c1"&gt;# Expose as an env var to handle double quotes correctly in the JSON&lt;/span&gt;
          &lt;span class="na"&gt;TEMPLATE_VARS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ inputs.TEMPLATE_VARS }}&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;set -ex&lt;/span&gt;

          &lt;span class="s"&gt;# Export vars from the input JSON&lt;/span&gt;
          &lt;span class="s"&gt;PARSED_VARS=$(echo "${TEMPLATE_VARS}" | jq 'to_entries[] | "\(.key)=\(.value)"' | xargs -I '{}' echo "export {}")&lt;/span&gt;

          &lt;span class="s"&gt;# Load the vars into the environment&lt;/span&gt;
          &lt;span class="s"&gt;eval "${PARSED_VARS}"&lt;/span&gt;

          &lt;span class="s"&gt;# Create envsubst template&lt;/span&gt;
          &lt;span class="s"&gt;ENVSUBST_VAR_LIST=$(echo "${TEMPLATE_VARS}" | jq 'to_entries[] | "${\(.key)}"' | xargs)&lt;/span&gt;

          &lt;span class="s"&gt;# Template the config file&lt;/span&gt;
          &lt;span class="s"&gt;envsubst "${ENVSUBST_VAR_LIST}" &amp;lt; ${{ inputs.TEMPLATE_FILE_PATH }} &amp;gt; ${{ inputs.TARGET_FILE_PATH }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following things are happening in the above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We define our input variable as an environment. This makes sure the quotes are not messed up in the output.&lt;/li&gt;
&lt;li&gt;Then we introduce a &lt;code&gt;PARSED_VARS&lt;/code&gt; variable which contains a list of &lt;code&gt;export $VARIABLE_1=value&lt;/code&gt; statements. We do that using &lt;code&gt;jq&lt;/code&gt; utility to extract keys and values from the input JSON.&lt;/li&gt;
&lt;li&gt;We introduce the parsed variables into the environment using &lt;code&gt;eval&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We create a list of variables &lt;code&gt;envsubst&lt;/code&gt; should substitute. This makes sure that standard variables like &lt;code&gt;$PATH&lt;/code&gt; are not substituted, unless they are specified in the input JSON.&lt;/li&gt;
&lt;li&gt;We run the &lt;code&gt;envsubst&lt;/code&gt; command to template our input file and store it in the target.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it! The output file should contain a result of substitution of the variables.&lt;/p&gt;

</description>
      <category>githubactions</category>
      <category>github</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
