Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Action: Invalid environment variable format

I have a problem with Github action. After I configured README.md over web browser after that GitHub action throw error:

Run export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
  export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
  echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV
  shell: /usr/bin/bash -e {0}
Error: Unable to process file command 'env' successfully.
Error: Invalid environment variable format 'Indicate that a variable can have the value `null`. Null-Safety is default from version **2.12.0** in the Dart language.'

The variable format is a text from the README.md and now no matter how I edit it always throws an error with the old error text. I tried to google it but not found any similar solution.

  • Current mine variable format

Null-Safety is enabled as default and it will indicate that a variable may have the value null. Required in the new Dart language from version 2.12.0.

My workflow file:

name: release master branch

on:
  push:
    branches:
      - master

jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: install dependencies
        run: npm install
      - name: install vsce
        run: sudo npm install -g vsce
      - name: extract version number
        run: |
          export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
          echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV
      - name: package the extension
        run: vsce package
      - name: release package to github repo
        uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: '${{ secrets.GITHUB_TOKEN }}'
          automatic_release_tag: master-v${{ env.BUILD_VERSION }}
          prerelease: true
          title: 'Json to Dart Extension master-v${{ env.BUILD_VERSION }}'
          files: |
            ./json-to-dart-${{ env.BUILD_VERSION }}.vsix

By adding paths ignore do not resolve it.

on:
  push:
    branches:
      - master
    paths-ignore:
      - '**/README.md'

The full code is public; you can find it at https://github.com/hiranthaR/Json-to-Dart-Model

Partial log:

...
2021-08-02T19:38:38.3085331Z ##[group]Run sudo npm install -g vsce
2021-08-02T19:38:38.3086141Z [36;1msudo npm install -g vsce[0m
2021-08-02T19:38:38.3129504Z shell: /usr/bin/bash -e {0}
2021-08-02T19:38:38.3129978Z ##[endgroup]
2021-08-02T19:38:42.3974877Z /usr/local/bin/vsce -> /usr/local/lib/node_modules/vsce/out/vsce
2021-08-02T19:38:42.4134841Z + [email protected]
2021-08-02T19:38:42.4135550Z added 73 packages from 42 contributors in 3.582s
2021-08-02T19:38:42.4443780Z ##[group]Run export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
2021-08-02T19:38:42.4444834Z [36;1mexport BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')[0m
2021-08-02T19:38:42.4445782Z [36;1mecho "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV[0m
2021-08-02T19:38:42.4489050Z shell: /usr/bin/bash -e {0}
2021-08-02T19:38:42.4489590Z ##[endgroup]
2021-08-02T19:38:42.4690016Z ##[error]Unable to process file command 'env' successfully.
2021-08-02T19:38:42.4703378Z ##[error]Invalid environment variable format 'Indicate that a variable can have the value `null`. Null-Safety is default from version **2.12.0** in the Dart language.'
2021-08-02T19:38:42.4890985Z Post job cleanup.
...
like image 568
Arnas Avatar asked Aug 06 '26 10:08

Arnas


2 Answers

Another way to deal with this is documented in the Github Actions documentation here: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings

Instead of doing

  export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
  echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV

You use the multiline string format as described in the documentation with

{name}<<{delimiter}
{value}
{delimiter}
  export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
  echo "BUILD_VERSION<<EOFMARKER" >> ${GITHUB_ENV}
  echo "${BUILD_VERSION}" >> ${GITHUB_ENV}
  echo "EOFMARKER" >> ${GITHUB_ENV}

Note: you should take care to read the security warning about using a constant like EOF as a delimiter here: https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections, so if you take input that you consider "from the user" you can use something to generate random data as a delimiter like this:

  export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}')
  delimiter="$(openssl rand -hex 8)"
  echo "BUILD_VERSION<<${delimiter}" >> ${GITHUB_ENV}
  echo "${BUILD_VERSION}" >> ${GITHUB_ENV}
  echo "${delimiter}" >> ${GITHUB_ENV}
like image 152
PeterT Avatar answered Aug 08 '26 22:08

PeterT


The problem is unrelated to your README.md in any way. Instead, the issue is that your package.json contains these other strings which include the literal string version. You are adding all these lines to your $GITHUB_ENV file, and all except the first are syntax errors.

BUILD_VERSION=3.2.8
Indicate that a variable can have the value `null`. Null-Safety is default from version **2.12.0** in the Dart language.
Disable ask for confirmation to start the conversion from the file `models.jsonc`.
Default target directory when conversion is from the file `models.jsonc`.

Try a more specific regular expression, or, as already suggested in a comment, use Awk to do the filtering, which allows for more precision. This also gets rid of the useless use of grep.

      - name: extract version number
        run: |
          BUILD_VERSION=$(awk -F \" '$2 == "version" {print $4}' package.json)
          echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV

In fact, you can do this in a single line; clearly, the indirection via a temporary variable wasn't helping you debug this anyway (though adding set -x to the shell script snippet probably would have).

      - name: extract version number
        run:
          awk -F \" '$2 == "version" {print "BUILD_VERSION=" $4}' package.json >> $GITHUB_ENV

Properly speaking, you should probably use a JSON-aware tool like jq to extract the version number and nothing else.

      - name: extract version number
        run:
          jq -r '"BUILD_VERSION=\(.version)"' package.json >> $GITHUB_ENV
like image 36
tripleee Avatar answered Aug 08 '26 22:08

tripleee