|
| 1 | +#!/bin/bash |
| 2 | +# Tutorial: Create a CodeCommit repository and manage code |
| 3 | +# Source: https://docs.aws.amazon.com/codecommit/latest/userguide/getting-started-cc.html |
| 4 | + |
| 5 | +WORK_DIR=$(mktemp -d) |
| 6 | +LOG_FILE="$WORK_DIR/codecommit-$(date +%Y%m%d-%H%M%S).log" |
| 7 | +exec > >(tee -a "$LOG_FILE") 2>&1 |
| 8 | + |
| 9 | +REGION=${AWS_DEFAULT_REGION:-${AWS_REGION:-$(aws configure get region 2>/dev/null)}} |
| 10 | +if [ -z "$REGION" ]; then |
| 11 | + echo "ERROR: No AWS region configured. Set one with: export AWS_DEFAULT_REGION=us-east-1" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | +export AWS_DEFAULT_REGION="$REGION" |
| 15 | +echo "Region: $REGION" |
| 16 | + |
| 17 | +RANDOM_ID=$(openssl rand -hex 4) |
| 18 | +REPO_NAME="tutorial-repo-${RANDOM_ID}" |
| 19 | + |
| 20 | +handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; } |
| 21 | +trap 'handle_error $LINENO' ERR |
| 22 | + |
| 23 | +cleanup() { |
| 24 | + echo "" |
| 25 | + echo "Cleaning up resources..." |
| 26 | + aws codecommit delete-repository --repository-name "$REPO_NAME" > /dev/null 2>&1 && \ |
| 27 | + echo " Deleted repository $REPO_NAME" |
| 28 | + rm -rf "$WORK_DIR" |
| 29 | + echo "Cleanup complete." |
| 30 | +} |
| 31 | + |
| 32 | +# Step 1: Create a repository |
| 33 | +echo "Step 1: Creating repository: $REPO_NAME" |
| 34 | +aws codecommit create-repository --repository-name "$REPO_NAME" \ |
| 35 | + --repository-description "Tutorial repository" \ |
| 36 | + --query 'repositoryMetadata.{Name:repositoryName,Id:repositoryId}' --output table |
| 37 | + |
| 38 | +# Step 2: Add a file |
| 39 | +echo "Step 2: Adding a file to the repository" |
| 40 | +echo -e "# Tutorial Repository\n\nThis is a sample file created by the CodeCommit tutorial." > "$WORK_DIR/README.md" |
| 41 | +COMMIT_ID=$(aws codecommit put-file \ |
| 42 | + --repository-name "$REPO_NAME" \ |
| 43 | + --branch-name main \ |
| 44 | + --file-content "fileb://$WORK_DIR/README.md" \ |
| 45 | + --file-path README.md \ |
| 46 | + --commit-message "Initial commit" \ |
| 47 | + --name "Tutorial User" \ |
| 48 | + --email "tutorial@example.com" \ |
| 49 | + --query 'commitId' --output text) |
| 50 | +echo " Commit: $COMMIT_ID" |
| 51 | + |
| 52 | +# Step 3: Get the file |
| 53 | +echo "Step 3: Retrieving the file" |
| 54 | +aws codecommit get-file --repository-name "$REPO_NAME" \ |
| 55 | + --file-path README.md \ |
| 56 | + --query '{Path:filePath,Size:fileSize,CommitId:commitId}' --output table |
| 57 | + |
| 58 | +# Step 4: Create a branch |
| 59 | +echo "Step 4: Creating a branch" |
| 60 | +aws codecommit create-branch --repository-name "$REPO_NAME" \ |
| 61 | + --branch-name feature-branch --commit-id "$COMMIT_ID" |
| 62 | +aws codecommit list-branches --repository-name "$REPO_NAME" \ |
| 63 | + --query 'branches' --output table |
| 64 | + |
| 65 | +# Step 5: Add a file to the branch |
| 66 | +echo "Step 5: Adding a file to the feature branch" |
| 67 | +echo "console.log('Hello from CodeCommit');" > "$WORK_DIR/index.js" |
| 68 | +aws codecommit put-file \ |
| 69 | + --repository-name "$REPO_NAME" \ |
| 70 | + --branch-name feature-branch \ |
| 71 | + --file-content "fileb://$WORK_DIR/index.js" \ |
| 72 | + --file-path src/index.js \ |
| 73 | + --commit-message "Add source file" \ |
| 74 | + --parent-commit-id "$COMMIT_ID" \ |
| 75 | + --query 'commitId' --output text > /dev/null |
| 76 | +echo " File added to feature-branch" |
| 77 | + |
| 78 | +# Step 6: Get differences between branches |
| 79 | +echo "Step 6: Comparing branches" |
| 80 | +aws codecommit get-differences \ |
| 81 | + --repository-name "$REPO_NAME" \ |
| 82 | + --before-commit-specifier main \ |
| 83 | + --after-commit-specifier feature-branch \ |
| 84 | + --query 'differences[].{Path:afterBlob.path,Type:changeType}' --output table |
| 85 | + |
| 86 | +# Step 7: Get repository metadata |
| 87 | +echo "Step 7: Repository metadata" |
| 88 | +aws codecommit get-repository --repository-name "$REPO_NAME" \ |
| 89 | + --query 'repositoryMetadata.{Name:repositoryName,DefaultBranch:defaultBranch,Created:creationDate}' --output table |
| 90 | + |
| 91 | +echo "" |
| 92 | +echo "Tutorial complete." |
| 93 | +echo "Do you want to clean up all resources? (y/n): " |
| 94 | +read -r CHOICE |
| 95 | +if [[ "$CHOICE" =~ ^[Yy]$ ]]; then |
| 96 | + cleanup |
| 97 | +else |
| 98 | + echo "Manual cleanup:" |
| 99 | + echo " aws codecommit delete-repository --repository-name $REPO_NAME" |
| 100 | +fi |
0 commit comments