Skip to content

cloudposse/github-action-yaml-config-query

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Project Banner

Latest ReleaseSlack Community

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: .true replace with query: ."true"
  • query: .${{ inputs.from == '' }} replace with query: ."${{ 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 }}

Inputs

Name Description Default Required
config YAML config N/A true
query JQ Query . true

Related Projects

Check out these related projects.

References

For additional context, refer to some of these links.

✨ Contributing

This project is under active development, and we encourage contributions from our community.

Many thanks to our outstanding contributors:

For πŸ› bug reports & feature requests, please use the issue tracker.

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

  1. Review our Code of Conduct and Contributor Guidelines.
  2. Fork the repo on GitHub
  3. Clone the project to your own machine
  4. Commit changes to your own branch
  5. Push your work back up to your fork
  6. Submit a Pull Request so that we can review your changes

NOTE: Be sure to merge the latest changes from "upstream" before making a pull request!

🌎 Slack Community

Join our Open Source Community on Slack. It's FREE for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally sweet infrastructure.

πŸ“° Newsletter

Sign up for our newsletter and join 3,000+ DevOps engineers, CTOs, and founders who get insider access to the latest DevOps trends, so you can always stay in the know. Dropped straight into your Inbox every week β€” and usually a 5-minute read.

πŸ“† Office Hours

Join us every Wednesday via Zoom for your weekly dose of insider DevOps trends, AWS news and Terraform insights, all sourced from our SweetOps community, plus a live Q&A that you can’t find anywhere else. It's FREE for everyone!

License

License

Preamble to the Apache License, Version 2.0

Complete license is available in the LICENSE file.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Trademarks

All other trademarks referenced herein are the property of their respective owners.


Copyright Β© 2017-2024 Cloud Posse, LLC

README footer

Beacon