json GitHub操作:环境变量格式无效

xjreopfe  于 2022-11-19  发布在  Git
关注(0)|答案(2)|浏览(224)

我有一个Github操作的问题。在我配置README.md通过Web浏览器后,GitHub操作抛出错误:

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.'

变量格式是README.md中的一个文本,现在无论我如何编辑它,它总是会抛出一个旧错误文本的错误。我试着谷歌它,但没有找到任何类似的解决方案。

  • 当前矿变量格式

默认情况下启用空值安全,它将指示变量可能具有空值。在2.12.0版的新Dart语言中是必需的。
我的工作流文件:

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

通过添加路径忽略不解决它。

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

完整的代码是公开的;您可以在https://github.com/hiranthaR/Json-to-Dart-Model中找到它
部分日志:

...
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 + vsce@1.96.1
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.
...
lyr7nygr

lyr7nygr1#

这个问题与您的README.md无关。相反,问题是您的package.json包含其他字符串,其中包括文字字符串version。您将所有这些行添加到您的$GITHUB_ENV文件中,除了第一行之外,所有行都是语法错误。

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`.

尝试一个更具体的正则表达式,或者像在注解中建议的那样,使用Awk来进行过滤,这样可以获得更高的精度。

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

实际上,您可以在一行中完成此操作;很明显,通过临时变量进行间接寻址并不能帮助您调试(尽管将set -x添加到shell脚本片段中可能会有帮助)。

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

正确地说,您可能应该使用诸如jq之类的支持JSON的工具来提取版本号,而不提取其他内容。

- name: extract version number
        run:
          jq -r '"BUILD_VERSION=\(.version)"' package.json >> $GITHUB_ENV
ax6ht2ek

ax6ht2ek2#

Github Actions文档中记录了另一种处理方法:https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
而不是做

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

可以使用文档中所述的多行字符串格式
第一次
注意:您应该仔细阅读有关使用EOF之类的常量作为分隔符的安全警告:https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#了解脚本注入的风险,所以如果你接受你认为“来自用户”的输入,你可以使用一些东西来生成随机数据作为分隔符,如下所示:

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}

相关问题