|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +trap cleanup SIGINT SIGTERM ERR EXIT |
| 5 | + |
| 6 | +temp_dir=$(mktemp -d) |
| 7 | + |
| 8 | +cleanup() { |
| 9 | + trap - SIGINT SIGTERM ERR EXIT |
| 10 | + if [[ "${no_clean-}" == "yes" ]]; then |
| 11 | + echo -e "\nSkipping cleanup" |
| 12 | + else |
| 13 | + rm -rf "${temp_dir}" |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +show_usage() { |
| 18 | + cat << EOF |
| 19 | +Usage: $(basename "$0") [OPTIONS] |
| 20 | +
|
| 21 | +Release a Terraform module by packaging, publishing, and tagging. |
| 22 | +
|
| 23 | +Options: |
| 24 | + -p, --publish Publish the module to Cloudsmith repository |
| 25 | + -t, --tag-push Create and push a Git tag for the release |
| 26 | + -m, --module-path Path to the module directory (required) |
| 27 | + -n, --no-clean Skip cleanup of temporary files |
| 28 | + -h, --help Show this help message |
| 29 | +
|
| 30 | +Example: |
| 31 | + $(basename "$0") -m modules/my-module -p -t |
| 32 | +EOF |
| 33 | +} |
| 34 | + |
| 35 | +module_publish=no |
| 36 | +tag_push=no |
| 37 | +module_path="" |
| 38 | + |
| 39 | +while :; do |
| 40 | + case "${1-}" in |
| 41 | + -h|--help) |
| 42 | + show_usage |
| 43 | + exit 0 |
| 44 | + ;; |
| 45 | + -p|--publish) |
| 46 | + module_publish=yes |
| 47 | + ;; |
| 48 | + -t|--tag-push) |
| 49 | + tag_push=yes |
| 50 | + ;; |
| 51 | + -m|--module-path) |
| 52 | + module_path="${2-}" |
| 53 | + shift |
| 54 | + ;; |
| 55 | + -n|--no-clean) |
| 56 | + no_clean=yes |
| 57 | + ;; |
| 58 | + *) |
| 59 | + break |
| 60 | + ;; |
| 61 | + esac |
| 62 | + shift |
| 63 | +done |
| 64 | + |
| 65 | +if [[ -z "$module_path" ]]; then |
| 66 | + echo "Module path is required" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +if ! [[ -f "$module_path/.module.toml" ]]; then |
| 71 | + echo "Module file not found: $module_path/.module.toml" |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +module_name="$(yq .module.name $module_path/.module.toml)" |
| 76 | +module_version="$(yq .module.version $module_path/.module.toml)" |
| 77 | +module_type="$(yq .module.type $module_path/.module.toml)" |
| 78 | + |
| 79 | +if ! echo "$module_version" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then |
| 80 | + echo "Invalid version format: $module_version (expected: x.y.z)" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +if [[ "$module_type" != "terraform" ]]; then |
| 85 | + echo "Invalid module type: $module_type (expected: terraform)" |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | +echo "Packaging $module_name-$module_version" |
| 90 | +package_file="$temp_dir/terraform-${module_name}-${module_version}.tar.gz" |
| 91 | + |
| 92 | +tar \ |
| 93 | + --exclude='.module.toml' \ |
| 94 | + --exclude='.terraform' \ |
| 95 | + --exclude='*.tfstate*' \ |
| 96 | + --exclude='*_override.tf*' \ |
| 97 | + -C $module_path \ |
| 98 | + -czvf "$package_file" . |
| 99 | + |
| 100 | +echo -e "\nPackage created: $package_file" |
| 101 | + |
| 102 | +if [[ "$module_publish" == "yes" ]]; then |
| 103 | + echo -e "\nPublishing $module_name-$module_version" |
| 104 | + cloudsmith push terraform \ |
| 105 | + --republish \ |
| 106 | + elastio/public \ |
| 107 | + "$package_file" |
| 108 | +else |
| 109 | + echo -e "\nSkipping publish" |
| 110 | +fi |
| 111 | + |
| 112 | +if [[ "$tag_push" == "yes" ]]; then |
| 113 | + echo -e "\nTagging $module_name-$module_version" |
| 114 | + git tag -a "$module_name-$module_version" -m "Release $module_name $module_version" |
| 115 | + git push origin "$module_name-$module_version" |
| 116 | +else |
| 117 | + echo -e "\nSkipping tag push" |
| 118 | +fi |
| 119 | + |
| 120 | +if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then |
| 121 | + { |
| 122 | + echo "### Module: $module_name" |
| 123 | + echo "- Version: $module_version" |
| 124 | + [[ "$module_publish" == "yes" ]] && echo "- Status: Published ✅" |
| 125 | + [[ "$tag_push" == "yes" ]] && echo "- Tag: $module_name-$module_version ✅" |
| 126 | + } >> "$GITHUB_STEP_SUMMARY" |
| 127 | +fi |
0 commit comments