Home > database >  Escape issue in GITHUB_ENV with quote and ampersand
Escape issue in GITHUB_ENV with quote and ampersand

Time:01-12

I am getting an error when attempting to print github environment value which contains quote and ampersand. Here's how the workflow looks like:

name: "Repro"
...

jobs:
  repro:
    runs-on: ubuntu-20.04
    steps:
    - name: "Define multi line variables"
      run: | 
        msg=$(echo 'Terraform used the selected providers to generate the following execution plan.
        Resource actions are indicated with the following symbols:
        ~ update in-place

        Terraform will perform the following actions:
        
        # module.app.helm_release.repro will be updated in-place
        ~ resource helm_release repro {
              id                         = repro
              name                       = repro
            ~ values                     = [
                  <<-EOT
                      ok: contains&character
                      ok2: "something-quoted"
                      problem: "aloha&barnie"
                      # ISSUE: anything after aloha is truncated when put to GITHUB_ENV, including this comment!
        ... (should not be truncated)
        ')
        echo "this is still good, not truncated: $msg"
        echo "SOME_ENV_VAR<<EOF" >> $GITHUB_ENV
        echo "$msg" >> $GITHUB_ENV
        echo "EOF" >> $GITHUB_ENV

    - name: "Try to print SOME_ENV_VAR with CAT - OK"
      run: | 
        echo `cat <<EOF
          ${{ env.SOME_ENV_VAR }}
        EOF`

    - name: "Try to print SOME_ENV_VAR without CAT/EOF - NOT OK"
      run: | 
        echo "${{ env.SOME_ENV_VAR }}"

github_error_screenshot

line 17: $'barnie\n              # ISSUE: anything after aloha is truncated when put to GITHUB_ENV, including this comment!\n... (should not be truncated)': command not found

So this issue happens with the echo "${{ env.SOME_ENV_VAR }}" command. Why is that when I have a quote on its own that's OK, ampersand on its own that's OK, but seemingly not both in the same line?

Possible solution...

I am able to print the value using CAT EOF like the following:

echo `cat <<EOF
  ${{ env.SOME_ENV_VAR }}
EOF`

The ask

I am interested to understand why the combination of quote and ampersand leads to this issue. Furthermore I cannot use CAT EOF solution above as I need to include this ENV variable in subsequent step that cannot execute shell script but can read ENV variable. Any ideas?

- name: Inform Terraform Plan Results
  uses: mshick/add-pr-comment@v2
  with:
    repo-token: ${{ secrets.GITHUB_TOKEN }}
    message-id: tf_plan_${{ matrix.environment }}
    message: |
      **Terraform plan for ${{ matrix.environment }}**
      <details>
        <summary>app/repro</summary>

        ```diff
        ${{ env.SOME_ENV_VAR }}
        ```
      </details>

CodePudding user response:

Given the breakpoint behaviour, it almost looks like a missing double-quote somewhere ... OR the program calling this is wrapping it with double-quotes, thereby exposing the "@" sign to the OS and interpreting it as a kind of buffer flush.

Looked more closely, that is what is happening at ... echo "$msg" >> $GITHUB_ENV ... which means that you need to escape the double-quotes to ensure portions of code are not "protruding" out of the echo command's double-quotes. I would recommend using temporary external file, massaging that as required with sed, then using it to define ENV.

  • Related