You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Task

  • Users frequently find the situation that jobs in a workflow pass variables to subsequent jobs.
  • This can easily be achieved by appending a pair of name/value for the variable to a temporary file that is offered by the JS7_RETURN_VALUES environment variable like this:

    Example how to pass a variable to subsequent jobs with Unix
    MY_VAR="some text"
    echo "my_variable=$MY_VAR" >> $JS7_RETURN_VALUES
    Example how to pass a variable to subsequent jobs with Windows
    set MY_VAR=some text
    echo my_variable=%MY_VAR% >> %JS7_RETURN_VALUES%


    Explanation:

    • A variable my_variable is passed as an order variable to subsequent jobs and instructions in a workflow.
    • The variable can be assigned a constant value or a value from some environment variable as e.g. MY_VAR that holds some arbitrary value.
  • There are two limitations about this approach:
    • The length of values for environment variables is limited, the precise max. size of environment variables is OS dependent.
    • If the value of the variable includes special characters such as carriage return, newline, linefeed then this might break depending on capabilities of the echo command in the OS.

Solution

  • To address the problem of special characters use base64 encoding for variable values as this encoding results in a single printable string.
  • To address the limitation concerning the size of environment variables do not use them and think about ways how to directly append lines to the temporary file indicated by JS7_RETURN_VALUES

Example

  • Let's assume two jobs to be executed in sequence:
    • the first job creates a lengthy e-email body in HTML format,
    • the second job sends e-mail and makes use of the HTML body.


  • Job: create-mail-body

    Example for Shell version on Unix
    #!/user/bin/env bash
    
    mailBody="<html><body><b>hello</b> <i>world</i></body></html>"
    mailBodyEncoded=$(echo $mailBody | base64 )
    echo "body=base64:$mailBodyEncoded" >> $JS7_RETURN_VALUES
    Example for PowerShell version on Unix
    #!/usr/bin/env pwsh
    
    $mailBody = "<html><body><b>hello</b> <i>world</i></body></html>"
    $mailBodyEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( $mailBody ))
    "body=base64:$mailBodyEncoded" | Out-File $env:JS7_RETURN_VALUES -Append


    Explanation:
    • In fact the e-mail body is not too lengthy, however, it suggests that an e-mail body would be created by some template file as with $mailBody = Get-Content "mail-body.html"

    • Base64 encoding is available from a number of sources such as the base64 Unix utility or .Net Core class.
    • The value of the e-mail body variable is prefixed with base64: to indicate the encoding.
    • The subsequent JS7 - JITL MailJob accepts the body variable and identifies the encoding and automatically decodes the argument value.



  • No labels