繁体   English   中英

读取 Github 操作中的 JSON 文件

[英]Read JSON file in Github Actions

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

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

读取文件取决于您使用的 shell。 这是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}}"

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

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

使用多行环境变量

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

这避免了对 escaping 的任何需求。

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

  1. 从文件加载 json ( ./your.json )
  2. 删除换行符( Source
  3. 使用fromJson解析 output 并设置矩阵变量。
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

我曾经用它从 json 数据中获取值。 希望这可以帮助

  - name: fetch the json value
    run: |
         githubjson=`cat $GITHUB_EVENT_PATH`
         echo $githubjson
         number=`echo $(jq -r '.number' <<< "$githubjson")`
         PRTitle=`echo $(jq -r '.pull_request.title' <<< "$githubjson")`
         PRUrl=`echo $(jq -r '.pull_request.html_url' <<< "$githubjson")`
         PRBody=`echo $(jq -r '.pull_request.body' <<< "$githubjson")`
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 }} 

使用 Powershell:

- name: Read json
  id: read-json
  shell: pwsh
  run: |
    $json = Get-Content yourfile.json | ConvertFrom-Json
    echo "::set-output name=prop::$(echo $json.prop)"

- run: echo ${{ steps.read-json.outputs.prop}}

受@dastrobu 回答的启发(将 key/val 添加到 $GITHUB_ENV)[https://stackoverflow.com/questions/57968497/how-do-i-set-an-env-var-with-a-bash-expression -in-github-actions] 并使用jq将 package.json 转换/缩小为一行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM