New fields added to .value
in to_entries[] | (.value changes) | from_entries
are lost if I combine the result with another array
#2052
-
I'm trying to add matrices into jobs in a GitHub Actions manifest, but only to those which have The input looks like: name: CI
on:
pull_request:
jobs:
no_setup_go:
runs-on: [ubuntu-stable]
steps:
- run: |
exit 0
has_setup_go:
runs-on: [ubuntu-stable]
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.20.1"
- run: |
exit 0
also_has_setup_go:
runs-on: [ubuntu-stable]
strategy:
fail-fast: false
matrix:
go_version:
- "1.19"
- "1.20"
- "1.21"
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}
- run: |
exit 0 Since GO_VERSIONS='["1.20.x", "1.21.x", "1.22.x"]'
yq --exit-status eval '
.jobs
| to_entries[]
| select(.value.steps[] | select(.uses | contains("/setup-go@")) | . += { "with": { "go-version": "${{ matrix.go_version }}" } })
| .value.strategy.fail-fast = false | .value.strategy.matrix.go_version = '"$GO_VERSIONS"'
' .github/workflows/ci.yaml ...gave me key: has_setup_go
value:
runs-on: [ubuntu-stable]
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}
- run: |
exit 0
strategy:
fail-fast: false
matrix:
go_version:
- 1.20.x
- 1.21.x
- 1.22.x
key: also_has_setup_go
value:
runs-on: [ubuntu-stable]
strategy:
fail-fast: false
matrix:
go_version:
- 1.20.x
- 1.21.x
- 1.22.x
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}
- run: |
exit 0 I thought it'd be a simple matter of merging back the other jobs: GO_VERSIONS='["1.20.x", "1.21.x", "1.22.x"]'
yq --exit-status eval '
.jobs = (
[
.jobs
| to_entries[]
| select(.value.steps[] | select(.uses | contains("/setup-go@")) | . += { "with": { "go-version": "${{ matrix.go_version }}" } })
| .value.strategy.fail-fast = false | .value.strategy.matrix.go_version = '"$GO_VERSIONS"'
] + [
.jobs
| to_entries[]
| select([.value.steps[] | select(.uses | contains("/setup-go@"))] | length == 0)
]
| from_entries
)' .github/workflows/ci.yaml However this actually gave me: name: CI
on:
pull_request:
jobs:
has_setup_go:
runs-on: [ubuntu-stable]
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}
- run: |
exit 0
also_has_setup_go:
runs-on: [ubuntu-stable]
strategy:
fail-fast: false
matrix:
go_version:
- 1.20.x
- 1.21.x
- 1.22.x
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}
- run: |
exit 0
no_setup_go:
runs-on: [ubuntu-stable]
steps:
- run: |
exit 0 The new Any idea what I did wrong? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The workaround I had later was GO_VERSIONS='["1.20.x", "1.21.x", "1.22.x"]'
# shellcheck disable=SC2016
# shellcheck disable=SC2016
yq --exit-status '
.jobs = (
[
.jobs
| to_entries[]
| select(.value.steps[] | select(.uses | contains("/setup-go@")) | . += { "with": { "go-version": "${{ matrix.go_version }}" } })
| .value = { "strategy": { "fail-fast": false, "matrix": { "go_version": '"$GO_VERSIONS"' } } } + (.value | with_entries(select(.key != "strategy"))) # Add strategy to the top
] + [
.jobs
| to_entries[]
| select([.value.steps[] | select(.uses | contains("/setup-go@"))] | length == 0)
]
| from_entries
)' -i .github/workflows/ci.yaml It unfortunately doesn't preserve the original key order (I'd like to avoid unnecessary changes as I want to Git-commit them.) |
Beta Was this translation helpful? Give feedback.
-
You can do it like this:
Explanation:
*formatting |
Beta Was this translation helpful? Give feedback.
You can do it like this:
Explanation:
with
to make multiple updates to a deep path https://mikefarah.gitbook.io/yq/operators/with*formatting