Skip to content

Latest commit

 

History

History
236 lines (195 loc) · 8.77 KB

README.md

File metadata and controls

236 lines (195 loc) · 8.77 KB

JSON to Variables Setter

GitHub Marketplace

Overview

JSON to Variables Setter (json2vars-setter) is a GitHub Action designed to parse a JSON file and set the resulting variables (such as operating systems, programming language versions, and GitHub Pages branch) as outputs in a GitHub Actions workflow.

Supported GitHub Actions Matrix Components

Languages Test Status
Python Python Test
Node.js Node.js Test
Ruby Ruby Test
Go Go Test
Rust Rust Test

Table of contents

Usage

This action reads a JSON file (default path: .github/workflows/matrix.json) and sets GitHub Actions outputs based on the parsed data.

Note

  • Please create the JSON file by referring to the Example JSON File.
  • By default, the JSON file path is .github/workflows/matrix.json. If you create a custom file, specify it in the 7rikazhexde/json2vars-setter action.
  • In the workflow, only the variables specified in the Outputs section are available.
  • Language versions are optional. If a language is not defined in the JSON, its corresponding output will be empty.

Inputs

Input Description Required
json-file Path to the JSON file.
Default: .github/workflows/matrix.json
No

Outputs

Important

Please check Example Workflow.
(*1): In order to reference them in both steps and jobs, outputs must be specified.
(*2): For lists, explicitly enclose the list in "" to make it a string. (Note that it is not '').

Output Description
os List of operating systems
versions_python List of Python versions
versions_ruby List of Ruby versions
versions_nodejs List of Node.js versions
versions_go List of Go versions
versions_rust List of Rust versions
ghpages_branch GitHub Pages branch name

Example JSON File

Here is an example of a JSON file that can be used with this action.

{
    "os": [
        "ubuntu-latest",
        "windows-latest",
        "macos-latest"
    ],
    "versions": {
        "python": [
            "3.10",
            "3.11",
            "3.12",
            "3.13"
        ],
        "ruby": [
            "3.0.6",
            "3.1.6",
            "3.2.6"
        ],
        "nodejs": [
            "16",
            "18",
            "20",
            "22"
        ],
        "go": [
            "1.23.0",
            "1.23.1",
            "1.23.2"
        ],
        "rust": [
            "1.79.0",
            "1.80.0",
            "1.81.0",
            "1.82.0",
            "stable"
        ]
    },
    "ghpages_branch": "ghgapes"
}

You can also define only specific languages.
Undefined language versions will result in empty outputs(*3).

{
    "os": [
        "ubuntu-latest",
        "windows-latest",
        "macos-latest"
    ],
    "versions": {
        "python": [
            "3.10",
            "3.11",
            "3.12",
            "3.13"
        ]
        //(*3)
    },
    "ghpages_branch": "ghgapes"
}

Example Workflow

Below is a Python Example of how to use the name: Set variables from JSON in a GitHub Actions workflow.

See Language-specific Workflows for workflow examples in other languages(Node.js, Ruby, Go, Rust).

name: Test on PR by matrix.json (Except Dependabot)

on:
  pull_request:
    branches: ["main"]

jobs:
  set_variables:
    if: github.actor != 'dependabot[bot]' && !startsWith(github.event.pull_request.title, 'Bump version')
    runs-on: ubuntu-latest
    outputs: # (*1)
      os: ${{ steps.json2vars.outputs.os }}
      versions_python: ${{ steps.json2vars.outputs.versions_python }}
      ghpages_branch: ${{ steps.json2vars.outputs.ghpages_branch }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.2.1
        with:
          fetch-depth: 0

      - name: Set variables from JSON
        id: json2vars
        uses: 7rikazhexde/json2vars-setter@main
        # Not required if using .github/workflows/matrix.json
        with:
          json-file: .github/workflows/python_project_matrix.json

      - name: Debug output values
        run: |
          echo "os: ${{ steps.json2vars.outputs.os }}"
          echo "versions_python: ${{ steps.json2vars.outputs.versions_python }}"
          echo "ghpages_branch: ${{ steps.json2vars.outputs.ghpages_branch }}"

  run_tests:
    needs: set_variables
    strategy:
      matrix:
        os: ${{ fromJson(needs.set_variables.outputs.os) }}
        python-version: ${{ fromJson(needs.set_variables.outputs.versions_python) }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.2.1
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5.2.0
        with:
          python-version: ${{ matrix.python-version }}

      - name: Show variables
        shell: bash
        run: |
          # For non-list case
          ghpages_branch="${{ needs.set_variables.outputs.ghpages_branch }}"

          # For list case, explicitly enclose the list in “” to make it a string. (Note that it is not ''.)(*2)
          os='${{ needs.set_variables.outputs.os }}'
          versions_python='${{ needs.set_variables.outputs.versions_python }}'

          echo "os: $os"
          echo "versions_python: ${versions_python}"
          echo "ghpages_branch: ${ghpages_branch}"

          # For loop case
          os_list=$(echo "${os}" | jq -r '.[]' | tr '\n' ' ' | sed 's/ $//')
          python_versions_list=$(echo "${versions_python}" | jq -r '.[]' | tr '\n' ' ' | sed 's/ $//')

          for current_os in ${os_list}; do
            for version in ${python_versions_list}; do
              echo "Current OS: ${current_os}, Current Python Version: ${version}"
            done
          done

      - name: Run pytest
        id: pytest
        shell: bash
        run: |
          output="$(poetry run pytest)"
          echo "${output}"

Language-specific Workflows

For language-specific workflow examples, please refer to

License

This project is licensed under the MIT License - see the LICENSE file for details.