Introduction
Working with configuration files, like JSON or properties files, is a common task in software development. Developers often need to read values from these files and inject them into different contexts, such as CI pipeline stages, applications, or scripts. In this article, we will guide you through a practical example of how to read and inject values from a properties file into output variables for use in various situations.
How To
The following example demonstrates how to read a properties file and inject the key-value pairs as output variables using shell scripts. This approach can be adapted to different contexts.
Harness offers a powerful feature for specifying output variables in shell scripts within its Continuous Delivery platform. You can find detailed documentation on this feature here.
- Create a Harness pipeline with two shell script steps.
- In the first shell script step, read the contents of an existing properties file and store them in an output variable. In this example, we create a sample properties file for demonstration purposes, but in your actual use case, you will likely be working with an existing properties file:
steps:
- step:
type: ShellScript
name: Retrieve and output
identifier: ShellScript_1
spec:
shell: Bash
onDelegate: true
source:
type: Inline
spec:
script: |-
echo "database.host=localhost" > config.properties
echo "database.port=3306" >> config.properties
echo "database.username=myuser" >> config.properties
echo "database.password=mypassword" >> config.properties
PROPERTIES=$(cat config.properties)
outputVariables:
- name: PROPERTIES
type: String
value: PROPERTIES
- In the second shell script step, read the properties from the output variable, inject them into a file, and parse the key-value pairs:
steps:
- step:
type: ShellScript
name: Read properties from output
identifier: read_properties_from_output
spec:
shell: Bash
onDelegate: true
source:
type: Inline
spec:
script: |-
file="./config.properties"
echo ${steps.ShellScript_1.output.outputVariables.PROPERTIES.value} >> $file
cat $file
while IFS='=' read -r key value
do
key=$(echo $key | tr '.' '_')
eval ${key}=\${value}
done < "$file"
This example uses a properties file, but you can easily adapt it to work with JSON files by modifying the parsing logic accordingly.
Conclusion
This article provided an example of how to read and inject values from a properties file into output variables for use in various contexts using Harness shell scripts. This approach can be easily adapted for other configuration file formats, such as JSON files. By following this guide and leveraging the power of Harness Continuous Delivery platform, you can efficiently manage and use configuration data across different stages of your development process.