在 Github Actions 中读取 JSON 文件 [英] Read JSON file in Github Actions
问题描述
我想读取 JSON 文件并使用 Github Actions YAML 文件中字符串中的属性.我该怎么做呢?(我想要package.json
的版本)
I want to read a JSON file and use a property in a string in a Github Actions YAML file. How do I do this?
(I want the version of the package.json
)
推荐答案
使用内置的 fromJson(value)
(请参阅此处:https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson)
Use the built-in fromJson(value)
(see here: https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson)
读取文件取决于您使用的 shell.以下是 sh
的示例:
Reading a file depends on the shell you're using. Here's an example for 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
Multi line JSON handling as per https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870
关于 set-env
/set-output
多行处理的 GitHub 问题:https://github.com/actions/toolkit/issues/403
GitHub issue about set-env
/ set-output
multi line handling: https://github.com/actions/toolkit/issues/403
这篇关于在 Github Actions 中读取 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!