GitHub Action: yaml-config-query
Define YAML document, filter it with JSON query and get result as outputs
Introduction
Utility action allow to declare YAML structured document as an input and get it's part as the action outputs referenced using JQ.
This action is useful in simplifing complext GitHub action workflows in different ways. For examples follow usage section.
Migration v0 to v1
There is an issue The query contains true or false fails with an error.
A workaround is to use a quote around "true" and "false" in a query.
To migrate from v0 to v1, quote in your queries all true/false and Github actions substitutions resovled to the values.
Example
query: .truereplace withquery: ."true"query: .${{ inputs.from == '' }}replace withquery: ."${{ inputs.from == '' }}"
Usage
Define constants
name: Pull Request
on:
pull_request:
branches: [ 'main' ]
types: [opened, synchronize, reopened, closed, labeled, unlabeled]
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Context
id: context
uses: cloudposse/github-action-yaml-config-query@main
with:
config: |
image: acme/example
tag: sha-${{ github.sha }}
- run: |
docker run ${{ steps.context.outputs.image }}:${{ steps.context.outputs.tag }}
Implement if/else
name: Promote
on:
workflow_call:
inputs:
from:
required: false
type: string
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Context
id: from
uses: cloudposse/github-action-yaml-config-query@main
with:
query: ."${{ inputs.from == '' }}"
config: |-
true:
tag: ${{ github.sha }}
false:
tag: ${{ inputs.from }}
- run: |
docker tag acme/example:${{ steps.context.outputs.tag }}
Implement switch
name: Build
on:
pull_request:
branches: [ 'main' ]
types: [opened, synchronize, reopened]
push:
branches: [ main ]
release:
types: [published]
jobs:
context:
runs-on: ubuntu-latest
steps:
- name: Context
id: controller
uses: cloudposse/github-action-yaml-config-query@main
with:
query: .${{ github.event_name }}
config: |-
pull_request:
build: true
promote: false
test: true
deploy: ["preview"]
push:
build: true
promote: false
test: true
deploy: ["dev"]
release:
build: false
promote: true
test: false
deploy: ["staging", "production"]
outputs:
build: ${{ steps.controlle.outputs.build }}
promote: ${{ steps.controlle.outputs.promote }}
test: ${{ steps.controlle.outputs.test }}
deploy: ${{ steps.controlle.outputs.deploy }}
build:
needs: [context]
if: ${{ needs.context.outputs.build }}
uses: ./.github/workflows/reusable-build.yaml
test:
needs: [context, test]
if: ${{ needs.context.outputs.test }}
uses: ./.github/workflows/reusable-test.yaml
promote:
needs: [context]
if: ${{ needs.context.outputs.promote }}
uses: ./.github/workflows/reusable-promote.yaml
deploy:
needs: [context]
if: ${{ needs.context.outputs.deploy != '[]' }}
strategy:
matrix:
environment: ${{ fromJson(needs.context.outputs.deploy) }}
uses: ./.github/workflows/reusable-deploy.yaml
with:
environment: ${{ matrix.environment }}