Ma2*_*340 3 javascript continuous-integration json github github-actions
我有一个像这样的 JSON 存储在执行我的 Github 操作的文件夹/路径中 -
{
"version":"1",
"sampleArray":[
{
"id":"1"
}
],
"secondArray":[
{
"secondId":"2"
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用 Github 操作如何编辑值,id例如:将id值设置为“5”,sampleArray以便我的 JSON 具有更新的值?
您可以使用jq命令行工具来编辑 json 文件,如下所示:
on: [push, pull_request]
name: Build
jobs:
build:
name: Example
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Update config.json
run: echo "`jq '.sampleArray[0].id="5"' config.json`" > config.json
- name: read config.json
run: cat config.json
Run Code Online (Sandbox Code Playgroud)
您还可以将其与 moreutils 包中的海绵一起使用,并在以下示例中传递环境变量:
on: [push, pull_request]
name: Build
jobs:
build:
name: Example
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: install more-utils
run: sudo apt-get install moreutils
- name: Update config.json
env:
ID: 5
run: jq --arg id "$ID" '.sampleArray[0].id=$id' config.json | sponge config.json
- name: read config.json
run: cat config.json
Run Code Online (Sandbox Code Playgroud)
这将输出:
{
"version": "1",
"sampleArray": [{
"id": "5"
}],
"secondArray": [{
"secondId": "2"
}]
}
Run Code Online (Sandbox Code Playgroud)