在 Github Actions 中读取 JSON 文件

exo*_*ift 9 json yaml github-actions

我想读取一个 JSON 文件并在 Github Actions YAML 文件中的字符串中使用一个属性。我该怎么做呢?(我想要的版本package.json

das*_*obu 18

使用多行环境变量

- run: |
    echo 'PACKAGE_JSON<<EOF' >> $GITHUB_ENV
    cat ./package.json >> $GITHUB_ENV
    echo 'EOF' >> $GITHUB_ENV
- run: |
    echo '${{ fromJson(env.PACKAGE_JSON).version }}'
Run Code Online (Sandbox Code Playgroud)

这避免了任何转义的需要。


riQ*_*iQQ 17

使用内置的fromJson(value)(见这里:https : //help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson

读取文件取决于您使用的外壳。下面是一个例子sh

name: Test linux job
on:
  push

jobs:
  testJob:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - id: set_var
        run: |
          content=`cat ./path/to/package.json`
          # the following lines are only required for multi line json
          content="${content//'%'/'%25'}"
          content="${content//$'\n'/'%0A'}"
          content="${content//$'\r'/'%0D'}"
          # end of optional handling for multi line json
          echo "::set-output name=packageJson::$content"
      - run: |
          echo "${{fromJson(steps.set_var.outputs.packageJson).version}}"

Run Code Online (Sandbox Code Playgroud)

根据https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870 进行多行 JSON 处理

关于set-env/set-output多行处理的GitHub 问题:https : //github.com/actions/toolkit/issues/403

  • 使用fromJson后如何访问对象的属性?echo result.property 或 $result.property 或 ${ result.property } 或 ${{ result.property }}? (2认同)

Rei*_*ain 12

受到 @dastrobu 的回答的启发,它将 key/val 添加到 $GITHUB_ENV并使用jq将 package.json 转换/缩小为一行:

- run: echo "PACKAGE_JSON=$(jq -c . < package.json)" >> $GITHUB_ENV
- run: echo '${{ fromJson(env.PACKAGE_JSON).version }}'
Run Code Online (Sandbox Code Playgroud)


ycr*_*ycr 12

您可以轻松地使用Script此操作。

  - name: "Read JSON"
    uses: actions/github-script@v6
    id: check-env
    with:
      result-encoding: string
      script: |
        try {
          const fs = require('fs')
          const jsonString = fs.readFileSync('./dir/file.json')
          var apps = JSON.parse(jsonString)
        } catch(err) {
          core.error("Error while reading or parsing the JSON")
          core.setFailed(err)
        }
Run Code Online (Sandbox Code Playgroud)


San*_*ich 11

以下是来自GHA 官方文档的示例版本,其中包括两项更改:

  1. 从文件加载 json ( ./your.json)
  2. 删除换行符(来源
  3. 用于fromJson解析输出并设置矩阵变量。
name: build
on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
    - id: set-matrix
      run: |
        JSON=$(cat ./your.json)
        echo "::set-output name=matrix::${JSON//'%'/'%25'}"

  job2:
    needs: job1
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.job1.outputs.matrix)}}
    steps:
    - run: build
Run Code Online (Sandbox Code Playgroud)


小智 7

on: [push, pull_request] 
name: Build
jobs:
  build:
    name: Example
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with: 
          path: './'      
      - run: |
          echo "`jq '.base_config[0].value="Alpha-21"' config.json `" > config.json
          echo "`jq '.base_config[1].value="1.2.14"' config.json`" > config.json
          echo "`jq '.base_config[2].value="29/12/2020"' config.json `" > config.json
     
      - uses: EndBug/add-and-commit@v6
        with:
          message: 'Add the version and date'
          add: '*.json --force'
          cwd: './' 
          token: ${{ secrets.TOKEN }} 
Run Code Online (Sandbox Code Playgroud)

  • @Macindows是的,根据https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-README.md(搜索“jq”) (8认同)
  • 所以 jq 是默认安装的应用程序的一部分? (4认同)