Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multifile yaml #570

Merged
merged 10 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,36 @@ jobs:
}
```

### Advaned Example with an separate target repository
### Change a YAML Multifile

Yaml supports multiple documents in a single file separated by `---`.
To update such a file, start the property path with the index of the document to be changed.

```yaml
jobs:
test-multifile-changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: fjogeleit/yaml-update-action@main
with:
valueFile: 'deployment/helm/values.yaml'
branch: deployment/v1.0.1
targetBranch: main
createPR: 'true'
description: Test GitHub Action
message: 'Update Images'
title: 'Version Updates '
changes: |
{
"__tests__/fixtures/multivalue.yaml": {
"[0].backend.version": "v1.1.0",
"[1].containers[1].image": "node:alpine"
}
}
```

### Advanced Example with an separate target repository

```yaml
env:
Expand Down
75 changes: 75 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,50 @@ test('multiple changes in one file', async () => {
console.info(content)
})

test('change in multi file', async () => {
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
process.env['WORK_DIR'] = '__tests__'
process.env['VALUE_PATH'] = '[0].backend.version'
process.env['VALUE'] = 'v1.1.0'
process.env['BRANCH'] = 'deployment/v1.1.0'
process.env['QUOTING_TYPE'] = '"'

type Result = {
backend: { version: string }
frontend: ContentNode
}

const [{ json, content }] = await runTest<Result>(new EnvOptions())

const jsonArray = json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual(process.env['VALUE'])
expect(jsonArray[1].backend.version).not.toEqual(process.env['VALUE'])
console.info(content)
console.info(json)
})

test('multiple changes in a multifile', async () => {
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
process.env['CHANGES'] =
'{"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"}'

type Result = {
backend: { version: string }
containers: { name: string; image: string }[]
}

const [{ json, content }] = await runTest<Result>(new EnvOptions())

const jsonArray = json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(content)
})

test('multiple changes in multiple files', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
Expand All @@ -201,6 +245,37 @@ test('multiple changes in multiple files', async () => {
console.info(results[1].content)
})

test('multiple changes in multiple files, including multifiles', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
"fixtures/multivalue.yaml": {"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"},
"fixtures/values.prod.yaml": {"backend.version": "v1.3.0", "frontend": true}
}`

type Result = {
backend: { version: string }
fronted: boolean
containers: { name: string; image: string }[]
}

const results = await runTest<Result>(new EnvOptions())

expect(results[0].json.backend.version).toEqual('v1.1.0')
expect(results[0].json.containers[1].image).toEqual('node:alpine')
console.info(results[0].content)

const jsonArray = results[1].json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(results[1].content)

expect(results[2].json.backend.version).toEqual('v1.3.0')
expect(results[2].json.frontend).toEqual(true)
console.info(results[2].content)
})
test('append array node', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {
Expand Down
26 changes: 26 additions & 0 deletions __tests__/fixtures/multivalue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
backend:
version: v1.2.0
frontend: {}
containers:
- name: nginx
image: nginx:latest
- name: node
image: node:latest
config:
prod: false,
version: 0
boolString: 'true'

---
backend:
version: v1.2.0
frontend: {}
containers:
- name: nginx
image: nginx:latest
- name: node
image: node:latest
config:
prod: false,
version: 0
boolString: 'true'
Loading
Loading