|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# This script creates a pull request with all the files from the generated folder. |
| 4 | +# It handles the complete workflow of cloning the repo, adding files, committing, |
| 5 | +# pushing, and creating a PR. |
| 6 | +# |
| 7 | +# This script is designed to be called from the runbook Command block, which |
| 8 | +# automatically passes values from the boilerplate input block with id |
| 9 | +# "infrastructure-live-root-repo-inputs" as command-line arguments: |
| 10 | +# - GithubOrgName -> $1 |
| 11 | +# - GithubRepoName -> $2 |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +# Save the original directory before any cd commands |
| 16 | +ORIGINAL_DIR=$(pwd) |
| 17 | + |
| 18 | +# Parse arguments (automatically passed from boilerplate inputs in the runbook) |
| 19 | +GITHUB_ORG="{{ .GithubOrgName }}" |
| 20 | +REPO_NAME="{{ .GithubRepoName }}" |
| 21 | + |
| 22 | +if [ -z "$GITHUB_ORG" ] || [ -z "$REPO_NAME" ]; then |
| 23 | + echo "Error: Missing required arguments" |
| 24 | + echo "Usage: $0 <github-org> <repo-name>" |
| 25 | + echo "Example: $0 acme-corp infrastructure-live-root" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Configuration |
| 30 | +BRANCH_NAME="add-terragrunt-foundations" |
| 31 | +GENERATED_DIR="generated" |
| 32 | +TEMP_DIR=$(mktemp -d) |
| 33 | + |
| 34 | +echo "🚀 Starting pull request creation process..." |
| 35 | +echo " Repository: ${GITHUB_ORG}/${REPO_NAME}" |
| 36 | +echo " Branch: ${BRANCH_NAME}" |
| 37 | +echo "" |
| 38 | + |
| 39 | +# Check if generated directory exists |
| 40 | +if [ ! -d "${ORIGINAL_DIR}/${GENERATED_DIR}" ]; then |
| 41 | + echo "❌ Error: Generated directory not found at ${ORIGINAL_DIR}/${GENERATED_DIR}" |
| 42 | + echo " Please make sure you've generated the files first." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +# Check if there are any files in the generated directory |
| 47 | +if [ -z "$(ls -A ${ORIGINAL_DIR}/${GENERATED_DIR})" ]; then |
| 48 | + echo "❌ Error: Generated directory is empty" |
| 49 | + echo " Please generate files before running this script." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +echo "📦 Cloning repository..." |
| 54 | +cd "$TEMP_DIR" |
| 55 | +gh repo clone "${GITHUB_ORG}/${REPO_NAME}" repo |
| 56 | +cd repo |
| 57 | + |
| 58 | +# Check if the repository is empty (no commits on main) |
| 59 | +REPO_IS_EMPTY=false |
| 60 | +if ! git rev-parse --verify main >/dev/null 2>&1; then |
| 61 | + REPO_IS_EMPTY=true |
| 62 | + echo "📝 Repository is empty (no main branch yet)" |
| 63 | + echo " Creating initial commit on main to establish base branch..." |
| 64 | + |
| 65 | + # Ensure we're on main branch (in case default branch isn't main) |
| 66 | + git checkout -b main 2>/dev/null || git checkout main |
| 67 | + |
| 68 | + # Create a minimal README for the initial commit |
| 69 | + echo "# ${REPO_NAME}" > README.md |
| 70 | + echo "" >> README.md |
| 71 | + echo "Infrastructure repository managed with Terragrunt." >> README.md |
| 72 | + |
| 73 | + git add README.md |
| 74 | + git commit -m "Initial commit" |
| 75 | + |
| 76 | + echo "⬆️ Pushing initial commit to main..." |
| 77 | + git push -u origin main |
| 78 | + |
| 79 | + echo "✅ Base branch established" |
| 80 | +fi |
| 81 | + |
| 82 | +# Check if the branch already exists remotely |
| 83 | +if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then |
| 84 | + echo "🌿 Branch '$BRANCH_NAME' already exists, checking it out..." |
| 85 | + git fetch origin "$BRANCH_NAME" |
| 86 | + git checkout "$BRANCH_NAME" |
| 87 | + git pull origin "$BRANCH_NAME" |
| 88 | +else |
| 89 | + echo "🌿 Creating new branch '$BRANCH_NAME'..." |
| 90 | + git checkout -b "$BRANCH_NAME" |
| 91 | +fi |
| 92 | + |
| 93 | +echo "📁 Copying generated files..." |
| 94 | +cp -r "${ORIGINAL_DIR}/${GENERATED_DIR}"/* . |
| 95 | + |
| 96 | +echo "➕ Adding files to git..." |
| 97 | +git add . |
| 98 | + |
| 99 | +# Check if there are any changes to commit |
| 100 | +if git diff --staged --quiet; then |
| 101 | + echo "⚠️ Warning: No changes detected. The generated files may already exist in the repo." |
| 102 | + echo " Cleaning up and exiting..." |
| 103 | + cd "$ORIGINAL_DIR" |
| 104 | + rm -rf "$TEMP_DIR" |
| 105 | + exit 0 |
| 106 | +fi |
| 107 | + |
| 108 | +echo "💾 Committing changes..." |
| 109 | +git commit -m "Add Terragrunt foundations |
| 110 | +
|
| 111 | +This commit adds the foundational Terragrunt configuration files including: |
| 112 | +- Root terragrunt.hcl configuration |
| 113 | +- Common configuration files |
| 114 | +- Account definitions |
| 115 | +- Tags configuration |
| 116 | +- README documentation" |
| 117 | + |
| 118 | +echo "⬆️ Pushing to GitHub..." |
| 119 | +git push origin "$BRANCH_NAME" |
| 120 | + |
| 121 | +# Check if PR already exists |
| 122 | +PR_EXISTS=$(gh pr list --head "$BRANCH_NAME" --json number --jq 'length') |
| 123 | + |
| 124 | +if [ "$PR_EXISTS" -gt 0 ]; then |
| 125 | + PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --json number --jq '.[0].number') |
| 126 | + echo "🔀 Pull request #${PR_NUMBER} already exists, updated with new commits!" |
| 127 | + echo " View it at: https://github.com/${GITHUB_ORG}/${REPO_NAME}/pull/${PR_NUMBER}" |
| 128 | +else |
| 129 | + echo "🔀 Creating pull request..." |
| 130 | + gh pr create \ |
| 131 | + --base main \ |
| 132 | + --head "$BRANCH_NAME" \ |
| 133 | + --title "Add Terragrunt foundations" \ |
| 134 | + --body "This PR adds the Terragrunt foundations to the repo. |
| 135 | +
|
| 136 | +## Changes included: |
| 137 | +- Root terragrunt.hcl configuration |
| 138 | +- Common configuration files |
| 139 | +- Account definitions |
| 140 | +- Tags configuration |
| 141 | +- README documentation |
| 142 | +
|
| 143 | +These files provide the foundation for managing infrastructure with Terragrunt." |
| 144 | + |
| 145 | + echo "" |
| 146 | + echo "✅ Pull request created successfully!" |
| 147 | + echo " View it at: https://github.com/${GITHUB_ORG}/${REPO_NAME}/pulls" |
| 148 | +fi |
| 149 | + |
| 150 | +# Cleanup |
| 151 | +cd "$ORIGINAL_DIR" |
| 152 | +rm -rf "$TEMP_DIR" |
| 153 | + |
| 154 | +echo "" |
| 155 | +echo "🎉 Done! Your pull request is ready for review." |
| 156 | + |
0 commit comments