Alexander Stephens
Alexander Stephens

Reputation: 11

Populate Jenkins String Parameter Dynamically from Terraform Template

I am attempting to pass a local environment variable into a string parameter of a Jenkins build. Currently the build is triggered from a Github webhook and runs each time there is a push to the repo but the build will fail if the string parameter in question is not populated correctly.

The default parameter value is blank, and the value I am trying to pass is updated hourly, so this field has to be updated dynamically.

Any support is greatly appreciated.

Upvotes: 1

Views: 974

Answers (1)

Anton Babenko
Anton Babenko

Reputation: 6636

I would recommend you to make a script which dumps output values from terraform state file into ini-file (or yaml, or anything what you can operate with) once something has changed in terraform and load content of that file during jenkins job. There are lots of considerations (like security, consistency) and there is even terraform output command.

In essence script does the following:

#!/bin/sh

json_lines=$(jq -r '.modules[] | select(.path == ["root"]) | .outputs | tojson' terraform.tfstate)

for json_line in $json_lines; do
    ini_values=$(echo $json_line | jq -r '. | to_entries | map("\(.key)=\(.value.value|tostring)") | .[]')

    if [[ -n $ini_values ]]; then
        cat <<EOF  >> terraform_outputs.ini
[terraform]
$ini_values
EOF
    fi
done

Upvotes: 1

Related Questions