I am trying to configure Key-Value Paris using GitHub Actions.
I have two workflows: Bulk-Config-KVM
and Config-KVM
.
Config-KVM
can be used to configure only one set that has multiple KVMs per execution.
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.finalValues }}
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"
# Convert input string to an array
IFS=', ' read -r -a dataArray <<< "$input"
# Initialize variables
output="["
index=0
# Loop through the data array
while [ $index -lt ${#dataArray[@]} ]; do
# Create a map object
map="{\"MapIdentifier\":\"${dataArray[$index]}\","
index=$((index + 1))
# Add key-value pairs
for keyIndex in {1..4}; do
key="Key$keyIndex"
value="Value$keyIndex"
map+="\"$key\":\"${dataArray[$index]}\", \"${value}\":\"${dataArray[$index + 1]}\", "
index=$((index + 2))
done
# Add Encrypted field
map+="\"Encrypted\":\"${dataArray[$index]}\"}"
index=$((index + 1))
# Add map to output
output+="$map,"
done
# Remove the trailing comma and close the output JSON array
output="${output%,}"
output+="]"
# Print the output
echo "********"
echo "$output"
echo "********"
echo "finalValues=$output" >> $GITHUB_OUTPUT
- name: Check list
run: |
echo ${{ steps.set-iterations.outputs.finalValues }}
call-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 }}
Encrypted: ${{ matrix.iteration.Encrypted }}
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 }}
echo ${{ matrix.iteration.Encrypted }}
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?
for steps.set-iterations.outputs.values
to work you need to actually generate the output for GH like this:
echo "values=$values" >> GITHUB_OUTPUT