我想读取一个 JSON 文件,并在 Github Actions YAML 文件中使用一个字符串属性。我该怎么做?(我需要的是
package.json
的版本号)package.json
的版本号)使用内置的fromJson(value)
(请参见此处:https://docs.github.com/en/actions/learn-github-actions/expressions#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}}"
set-env
/ set-output
多行处理的 GitHub 问题:https://github.com/actions/toolkit/issues/403。cat package.json | tr '\n\r' ' '
的事情,尽管这并不能正确解决编码百分号的问题。 - Kimball Robinsonjq
快速获取 JSON 数据:cat package.json | jq '.version'
。 - Kimball Robinson使用多行环境变量:
- run: |
echo 'PACKAGE_JSON<<EOF' >> $GITHUB_ENV
cat ./package.json >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- run: |
echo '${{ fromJson(env.PACKAGE_JSON).version }}'
隐式键需要在单行上
。 - Reinsbrain./your.json
)加载 json。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
受 @dastrobu 的回答启发,可以将键/值添加到 $GITHUB_ENV,并使用 jq
将 package.json 转换/压缩为一行:
- run: echo "PACKAGE_JSON=$(jq -c . < package.json)" >> $GITHUB_ENV
- run: echo '${{ fromJson(env.PACKAGE_JSON).version }}'
您可以轻松使用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)
}
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 }}
我曾经使用过这个方法从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")`
简而言之
如果您:
代码片段在结尾处。
--
我有一个相对复杂的情况,我们使用了几个自托管的 GitHub runner。我需要从一个大型配置 JSON 文件中读取一个字段,以决定其余作业应在哪个平台上运行。
因此,下面的代码从名为 configure
的作业开始,该作业从存储库中的 config.json
文件中读取并将其设置为输出,依赖于此的下一个作业可以从输出中读取。
我尝试了这里和this post中的大多数答案,并结合了有效的答案,所以感谢所有这些人的评论。
jobs:
configure:
runs-on: ubuntu-latest # at first we still don't know which self-hosted server, so we just use ubuntu latest and read the JSON file
outputs: # here we use the outputs from steps, and set outputs for the job `configure`
config: ${{ steps.read-json.outputs.config }}
platform: ${{ steps.get-attribute.outputs.platform }}
steps:
- name: Checkout to repository
uses: actions/checkout@v3
- name: Read JSON file # this puts the whole JSON file in the read-json step output
id: read-json
run: | # to read multi-line JSON file and not bother with escaping
echo "config<<EOF" >> $GITHUB_OUTPUT
cat ./config.json >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Add platform in output # for simpler use, you can add a single field in the output as well.
id: read-attribute
run: echo "platform=${{fromJson(steps.read-json.outputs.config).platform}}" >> "$GITHUB_OUTPUT"
job2:
needs: configure
runs-on: ${{needs.configure.outputs.platform}}
steps:
- name: Log JSON file
run: echo "${{needs.configure.outputs.config}}"
- name: Log an attribute in JSON file
run: echo "${{fromJson(needs.configure.outputs.config).attribute}}"
使用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}}
set-output 和 save-state 已被弃用。从2023年6月1日开始,通过stdout使用 save-state 或 set-output 命令的工作流将会失败并显示错误。阅读更多