Skip to content

Commit

Permalink
chore: Automatically updates Terraform version used in repository and…
Browse files Browse the repository at this point in the history
… test-suite (#2755)
  • Loading branch information
maastha authored Oct 30, 2024
1 parent 8e79adb commit 32f7ea7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
27 changes: 26 additions & 1 deletion .github/workflows/update_tf_compatibility_matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,29 @@ jobs:
commit-message: "doc: Updates Terraform Compatibility Matrix documentation"
delete-branch: true
branch: terraform-compatibility-matrix-update
body: "Automatic updates for Terraform Compatibility Matrix documentation. **Action Required**: Update test-suite.yml, .tools-version files, and TF_VERSION_LATEST GitHub environment variable if needed."
body: "Automatic updates for Terraform Compatibility Matrix documentation. **Action Required**: Update TF_VERSION_LATEST GitHub environment variable if needed."

update-tf-version-in-repository:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: Update files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: make update-tf-version-in-repository
- name: Verify Changed files
uses: tj-actions/verify-changed-files@54483a2138ca67989bc40785aa22faee8b085894
id: verify-changed-files
- name: Create PR
uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f
if: steps.verify-changed-files.outputs.files_changed == 'true'
with:
token: ${{ secrets.APIX_BOT_PAT }}
title: "chore: Updates repository to use supported Terraform versions"
commit-message: "chore: Updates repository to use supported Terraform versions"
delete-branch: true
branch: terraform-versions-update
body: "Automatic updates for our repository to use supported Terraform versions."
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ generate-docs-all:
update-tf-compatibility-matrix: ## Update Terraform Compatibility Matrix documentation
./scripts/update-tf-compatibility-matrix.sh

.PHONY: update-tf-version-in-repository
update-tf-version-in-repository: ## Update Terraform versions
./scripts/update-tf-version-in-repository.sh

.PHONY: update-changelog-unreleased-section
update-changelog-unreleased-section:
./scripts/update-changelog-unreleased-section.sh
Expand Down
43 changes: 37 additions & 6 deletions scripts/get-terraform-supported-versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ usage() {
exit 1
}

fetch_terraform_releases_page() {
local page="$1"
local api_version="2022-11-28"
curl -s \
--request GET \
--url "https://api.github.com/repos/hashicorp/terraform/releases?per_page=100&page=$page" \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header "X-GitHub-Api-Version: $api_version"
}

get_last_day_of_month() {
last_day_of_month=0
case $1 in
Expand Down Expand Up @@ -66,14 +76,9 @@ add_end_support_date() {

get_terraform_supported_versions_details() {
page=1
api_version="2022-11-28"

while true; do
response=$(curl -s \
--request GET \
--url "https://api.github.com/repos/hashicorp/terraform/releases?per_page=100&page=$page" \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header "X-GitHub-Api-Version: $api_version")
response=$(fetch_terraform_releases_page "$page")
if [[ "$(printf '%s\n' "$response" | jq -e 'length == 0')" == "true" ]]; then
break
else
Expand Down Expand Up @@ -106,6 +111,30 @@ get_terraform_supported_versions() {
echo "[$formatted_output]" | jq -c .
}

get_latest_terraform_version() {
api_version="2022-11-28"
latest_version=""

for page in 1 2; do
response=$(fetch_terraform_releases_page "$page")

if [[ "$(printf '%s\n' "$response" | jq -e 'length == 0')" == "true" ]]; then
break
fi

versions=$(echo "$response" | jq -r '.[] | select(.tag_name | test("alpha|beta|rc"; "i") | not) | .tag_name')

for version in $versions; do
version_cleaned="${version#v}"
if [[ -z "$latest_version" || "$(echo -e "$latest_version\n$version_cleaned" | sort -V | tail -n 1)" == "$version_cleaned" ]]; then
latest_version="$version_cleaned"
fi
done
done

echo "$latest_version"
}

if [ $# -ne 1 ]; then
usage
fi
Expand All @@ -115,6 +144,8 @@ if [ "$get_details" = "true" ]; then
get_terraform_supported_versions_details
elif [ "$get_details" = "false" ]; then
get_terraform_supported_versions
elif [ "$get_details" = "latest" ]; then
get_latest_terraform_version
else
echo "Invalid parameter."
usage
Expand Down
40 changes: 40 additions & 0 deletions scripts/update-tf-version-in-repository.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

# Copyright 2024 MongoDB Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail

# example value of TF_VERSIONS_ARR='["1.9.x", "1.8.x", "1.7.x"]'
TF_VERSIONS_ARR=$(./scripts/get-terraform-supported-versions.sh "false")

TEST_SUITE_YAML_FILE=".github/workflows/test-suite.yml"
TOOL_VERSIONS_FILE=".tool-versions"
DOC_SCRIPT="scripts/generate-doc.sh"
DOC_ALL_SCRIPT="scripts/generate-docs-all.sh"

LATEST_TF_VERSION=$(echo "$TF_VERSIONS_ARR" | sed -E 's/^\["([^"]+).*/\1/')

# Update Terraform versions in test-suite.yml
sed -i.bak -E "/^ *terraform_matrix:/,/^ *provider_matrix:/ s|(default: ')[^']*(')|\1$TF_VERSIONS_ARR\2|" "$TEST_SUITE_YAML_FILE"

sed -i.bak -E "s|schedule_terraform_matrix: '.*'|schedule_terraform_matrix: '[\"$LATEST_TF_VERSION\"]'|" "$TEST_SUITE_YAML_FILE"

# Update patch version occurrences
LATEST_TF_PATCH_VERSION=$(./scripts/get-terraform-supported-versions.sh "latest")

# Update Terraform versions in .tool-versions
sed -i.bak -E "s|^(terraform) [0-9]+\.[0-9]+\.[0-9]+|\1 $LATEST_TF_PATCH_VERSION|" "$TOOL_VERSIONS_FILE"
# Update Terraform versions in generate-doc scripts
sed -i.bak -E "/TF_VERSION=/ s/[0-9]+\.[0-9]+\.[0-9]+/$LATEST_TF_PATCH_VERSION/g" "$DOC_SCRIPT"
sed -i.bak -E "/TF_VERSION=/ s/[0-9]+\.[0-9]+\.[0-9]+/$LATEST_TF_PATCH_VERSION/g" "$DOC_ALL_SCRIPT"

0 comments on commit 32f7ea7

Please sign in to comment.