I trying to configure Key-Value Paris using GitHub Actions.
I have two workflow: Bulk-Config-KVM
and Config-KVM
.
Config-KVM
can be used to configure one set that has multiple KVMs.
Bulk-Config-KVM
is used for configuring multiple sets that has multiple KVMs.
Here is my input:
MapIdentifier11, Key11, Value11, Key12, Value12, Key13, Value13, Key14, Value14, true, MapIdentifier22, Key21, Value21, Key22, Value22, Key23, Value23, Key24, Value24, false, MapIdentifier33, Key31, Value31, Key32, Value32, Key33, Value33, Key34, Value34, false
I’m using Config-KVM
as my re-useable workflow from Bulk-Config-KVM
.
Config-KVM
:
#Implementation of Config-KVM using GitHub Action
name: Config-KVM
on:
workflow_dispatch:
inputs:
MapIdentifier:
type: string
description: 'Provide name of KVM'
Key1:
type: string
description: 'Provide Key1'
Value1:
type: string
description: 'Provide Value1'
Key2:
type: string
description: 'Provide Key2'
Value2:
type: string
description: 'Provide Value2'
Key3:
type: string
description: 'Provide Key3'
Value3:
type: string
description: 'Provide Value3'
Key4:
type: string
description: 'Provide Key4'
Value4:
type: string
description: 'Provide Value4'
Encrypted:
type: choice
options:
- true
- false
workflow_call:
inputs:
MapIdentifier:
type: string
description: 'Provide name of KVM'
Key1:
type: string
description: 'Provide Key1'
Value1:
type: string
description: 'Provide Value1'
Key2:
type: string
description: 'Provide Key2'
Value2:
type: string
description: 'Provide Value2'
Key3:
type: string
description: 'Provide Key3'
Value3:
type: string
description: 'Provide Value3'
Key4:
type: string
description: 'Provide Key4'
Value4:
type: string
description: 'Provide Value4'
Encrypted:
type: string
jobs:
Create_Update-KVM:
runs-on: [xxx]
steps:
- name: Execute cURL
run: |
echo "### Key4 = ${{ inputs.Key4 }} ###"
if [ ${{ inputs.Key4 }} != "" ];
then
createresponse=`curl -k --write-out '%{http_code}' --header "authorization: Basic ${{ secrets.USER }}" --header "Content-Type: application/json" --request POST --url 'https://example.com/keyvaluemaps' --data '{"encrypted": "${{ inputs.Encrypted }}","entry": [{"name": "${{ inputs.Key1 }}","value": "${{ inputs.Value1 }}"},{"name": "${{ inputs.Key2 }}","value": "${{ inputs.Value2 }}"},{"name": "${{ inputs.Key3 }}","value": "${{ inputs.Value3 }}"},{"name": "${{ inputs.Key4 }}","value": "${{ inputs.Value4 }}"}],"name": "${{ inputs.MapIdentifier }}"}'`
if [[ $createresponse == *"201"* ]];
then
echo "KVM created successfully: $createresponse"
else
echo "Failed with $createresponse."
exit 1
fi
elif [ ${{ inputs.Key3 }} != "" ];
then
createresponse=`curl -k --write-out '%{http_code}' --header "authorization: Basic ${{ secrets.USER }}" --header "Content-Type: application/json" --request POST --url 'https://example.com/keyvaluemaps' --data '{"encrypted": "${{ inputs.Encrypted }}","entry": [{"name": "${{ inputs.Key1 }}","value": "${{ inputs.Value1 }}"},{"name": "${{ inputs.Key2 }}","value": "${{ inputs.Value2 }}"},{"name": "${{ inputs.Key3 }}","value": "${{ inputs.Value3 }}"}],"name": "${{ inputs.MapIdentifier }}"}'`
if [[ $createresponse == *"201"* ]];
then
echo "KVM created successfully: $createresponse"
else
echo "Failed with $createresponse."
exit 1
fi
elif [ ${{ inputs.Key2 }} != "" ];
then
createresponse=`curl -k --write-out '%{http_code}' --header "authorization: Basic ${{ secrets.USER }}" --header "Content-Type: application/json" --request POST --url 'https://example.com/keyvaluemaps' --data '{"encrypted": "${{ inputs.Encrypted }}","entry": [{"name": "${{ inputs.Key1 }}","value": "${{ inputs.Value1 }}"},{"name": "${{ inputs.Key2 }}","value": "${{ inputs.Value2 }}"}],"name": "${{ inputs.MapIdentifier }}"}'`
if [[ $createresponse == *"201"* ]];
then
echo "KVM created successfully: $createresponse"
else
echo "Failed with $createresponse"
exit 1
fi
else
createresponse=`curl -k --write-out '%{http_code}' --header "authorization: Basic ${{ secrets.USER }}" --header "Content-Type: application/json" --request POST --url 'https://example.com/keyvaluemaps' --data '{"encrypted": "${{ inputs.Encrypted }}","entry": [{"name": "${{ inputs.Key1 }}","value": "${{ inputs.Value1 }}"}],"name": "${{ inputs.MapIdentifier }}"}'`
echo "### createresponse = $createresponse ###"
if [[ $createresponse == *"201"* ]];
then
echo "KVM created successfully."
else
echo "KVM already exists: $createresponse"
exit 1
fi
fi
In the Bulk-Config-KVM
, I’m using Matrix
as my strategy:
name: Bulk-Config-KVM.yml
on:
workflow_dispatch:
inputs:
KVMs:
type: string
description: 'Provide the values in the following order MapIdentifier, Key1, Value1, Key2, Value2, Key3, Value3, Key4, Value4'
jobs:
build:
runs-on: [xxx]
outputs:
iterations: ${{ steps.set-iterations.outputs.values }}
steps:
- name: Start
run: ls
- name: Read JSON and trigger workflows
id: set-iterations
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Print Inputs provided by the user"
echo "### KVMs = ${{ github.event.inputs.KVMs }} ###"
echo "BEFORE"
input="${{ github.event.inputs.KVMs }}"
echo "### input = ${{ github.event.inputs.KVMs }} ###"
echo "AFTER"
# Remove spaces around commas and split the input string into an array
IFS=', ' read -r -a arr <<< "$input"
# Initialize variables
json="["
map=""
encrypted=""
keys=()
values=()
for ((i=0; i<${#arr[@]}; i++)); do
if [[ ${arr[i]} == MapIdentifier* ]]; then
if [ "$map" != "" ]; then
json+="{"MapIdentifier":"$map","
for ((j=0; j<${#keys[@]}; j++)); do
json+=""${keys[j]}":"${values[j]}","
done
json+=""Encrypted":"$encrypted"},"
fi
map=${arr[i]}
encrypted=""
keys=()
values=()
elif [[ ${arr[i]} == true || ${arr[i]} == false ]]; then
encrypted=${arr[i]}
else
keys+=(${arr[i]})
i=$((i+1))
values+=(${arr[i]})
fi
done
# Add the last map
json+="{"MapIdentifier":"$map","
for ((j=0; j<${#keys[@]}; j++)); do
json+=""${keys[j]}":"${values[j]}","
done
json+=""Encrypted":"$encrypted"}"
json+="]"
# Format the JSON
echo $json | jq '.'
- name: Check list
run: |
echo ${{ steps.set-iterations.outputs.values }}
call-ASGW-Config-KVM:
needs: [build]
strategy:
matrix:
iteration: ${{fromJson(needs.build.outputs.iterations)}}
uses: ./.github/workflows/Config-KVM.yml
secrets: inherit
permissions:
contents: read
with:
MapIdentifier: ${{ matrix.iteration.MapIdentifier }}
Key1: ${{ matrix.iteration.Key1 }}
Value1: ${{ matrix.iteration.Value1 }}
Key2: ${{ matrix.iteration.Key2 }}
Value2: ${{ matrix.iteration.Value2 }}
Key3: ${{ matrix.iteration.Key3 }}
Value3: ${{ matrix.iteration.Value3 }}
Key4: ${{ matrix.iteration.Key4 }}
Value4: ${{ matrix.iteration.Value4 }}
check:
needs: [build]
runs-on: [xxx]
strategy:
matrix:
iteration: ${{fromJson(needs.build.outputs.iterations)}}
steps:
- name: Do something
run: |
echo Iteration
echo ${{ matrix.iteration }}
echo ${{ matrix.iteration.MapIdentifier }}
echo ${{ matrix.iteration.Key1 }}
echo ${{ matrix.iteration.Value1 }}
echo ${{ matrix.iteration.Key2 }}
echo ${{ matrix.iteration.Value2 }}
echo ${{ matrix.iteration.Key3 }}
echo ${{ matrix.iteration.Value3 }}
echo ${{ matrix.iteration.Key4 }}
echo ${{ matrix.iteration.Value4 }}
Output of echo JSON
is:
[
{
"MapIdentifier": "MapIdentifier11",
"Key11": "Value11",
"Key12": "Value12",
"Key13": "Value13",
"Key14": "Value14",
"Encrypted": "true"
},
{
"MapIdentifier": "MapIdentifier22",
"Key21": "Value21",
"Key22": "Value22",
"Key23": "Value23",
"Key24": "Value24",
"Encrypted": "false"
},
{
"MapIdentifier": "MapIdentifier33",
"Key31": "Value31",
"Key32": "Value32",
"Key33": "Value33",
"Key34": "Value34",
"Encrypted": "false"
}
]
Error: Error when evaluating 'strategy' for job 'check'. .github/workflows/Bulk-Config-KVM.yml (Line: 105, Col: 20): Error parsing fromJson,.github/workflows/Bulk-Config-KVM.yml (Line: 105, Col: 20): Error reading JToken from JsonReader. Path '', line 0, position 0.,.github/workflows/Bulk-Config-KVM.yml (Line: 105, Col: 20): Unexpected value ''
How to fix this and how to identify the right strategy for the different use cases?