Skip to content

Commit

Permalink
Merge pull request #27 from ConseilsTI/v1.0.23
Browse files Browse the repository at this point in the history
v1.0.23
  • Loading branch information
benyboy84 committed Apr 16, 2024
2 parents 8da1ac9 + 861a37a commit 14130fd
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions .github/workflows/create_module.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# This workflow runs manually.
#
# `Create Terraform Module` job run Terraform Cloud API call to retrieve the workspace id.
# It then uses API calls to find the variable ID and value. After several validations, it
# updates the variable in Terraform Cloud using an API call.
#
# Finally, it uses `checkout` to download the content of the repository. It runs
# `hashicorp/tfc-workflows-github/actions/upload-configuration` to creates a configuration version
# and uploads the directory containing files for a specified Terraform Cloud Workspace. Then, it
# performs an API driven run in Terraform Cloud, using a configuration version and the workspace's
# current variables.
#
# Documentation
# - https://github.com/actions/checkout
# - https://github.com/hashicorp/tfc-workflows-github/tree/main/actions/upload-configuration
# - https://github.com/hashicorp/tfc-workflows-github/tree/main/actions/create-run
#
# Prerequisites:
# - A Terraform Cloud workspace
# - A Terraform Cloud API token with required permissions
#
# Terraform Cloud user API token stored as a GitHub secret (e.g. TF_API_TOKEN) in the repository.
# Documentation:
# - https://www.terraform.io/docs/cloud/users-teams-organizations/api-tokens.html
# - https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets

name: "Create Terraform Module"

on: # yamllint disable-line rule:truthy
Expand Down Expand Up @@ -26,7 +52,7 @@ jobs:
echo "INFO | Building required variables."
workspace_name=$(echo "${EVENT_CONTEXT}" | jq -r ".repository.name")
workspace_name="${workspace_name,,}"
echo "workspace_name=${workspace_name}" >> $GITHUB_OUTPUT
echo "workspace_name=${workspace_name}" >> "${GITHUB_OUTPUT}"
echo "INFO | Workspace name: ${workspace_name}"
tfc_api_url="https://app.terraform.io/api/v2"
auth_header="Authorization: Bearer ${TF_API_TOKEN}"
Expand All @@ -35,7 +61,7 @@ jobs:
workspace_id=$(curl -sS -H "${auth_header}" -H "${content_header}" -L "${tfc_api_url}/organizations/${{ env.TF_CLOUD_ORGANIZATION }}/workspaces/${workspace_name}" | jq -r '.data."id"')
if [[ "${workspace_id}" != "null" ]]; then
echo "INFO | Workspace ID: ${workspace_id}"
echo "workspace_id=${workspace_id}" >> $GITHUB_OUTPUT
echo "workspace_id=${workspace_id}" >> "${GITHUB_OUTPUT}"
else
echo "ERROR | Unable to get workspace ID."
exit 1
Expand All @@ -52,16 +78,16 @@ jobs:
variable_id=$(curl -sS -H "${auth_header}" -H "${content_header}" -L "${tfc_api_url}/workspaces/${{ steps.get_workspace_id.outputs.workspace_id }}/vars" | jq '.data[] | select(.attributes.key == "${{ env.TF_VARIABLE }}" ) | .id')
if [[ "${variable_id}" != "null" ]]; then
echo "INFO | Variable ID: ${variable_id}"
echo "variable_id=${variable_id}" >> $GITHUB_OUTPUT
echo "variable_id=${variable_id}" >> "${GITHUB_OUTPUT}"
else
echo "ERROR | Unable to get variable id."
exit 1
fi
fi
echo "INFO | Running API call to get variable value."
variable_value=$(curl -sS -H "${auth_header}" -H "${content_header}" -L "${tfc_api_url}/workspaces/${{ steps.get_workspace_id.outputs.workspace_id }}/vars" | jq '.data[] | select(.attributes.key == "${{ env.TF_VARIABLE }}" ) | .attributes.value')
if [[ "${variable_value}" != "null" ]]; then
echo "INFO | Variable value: ${variable_value}"
echo "variable_value=${variable_value}" >> $GITHUB_OUTPUT
echo "variable_value=${variable_value}" >> "${GITHUB_OUTPUT}"
else
echo "ERROR | Unable to get variable value."
exit 1
Expand Down Expand Up @@ -92,7 +118,7 @@ jobs:
fi
echo "INFO | Adding new module's name into the array."
variable_value="${variable_value::-1}, \"${{ github.event.inputs.module_name }}\"]"
echo "INFO | Converting \\\" into \\\\\"."
printf 'INFO | Converting \\" into \\\\".'
variable_value=${variable_value//\"/\\\"}
echo "INFO | Building JSON payload."
json_string='{"data":{"id":"'"${{ steps.get_variable.outputs.variable_id }}"'","attributes":{"value":"'"${variable_value}"'"},"type":"vars"}}'
Expand Down

0 comments on commit 14130fd

Please sign in to comment.