1+ #! /bin/bash
2+ set -euo pipefail
3+
4+ # Deploy kubeapply to specified environment via terraform PR
5+ # Usage: deploy-to-env.sh <environment> [create-pr]
6+ # environment: "stage" or "production"
7+ # create-pr: if "true", creates PR automatically; if "false", just prepares branch
8+
9+ ENVIRONMENT=${1:- }
10+ CREATE_PR=${2:- true}
11+
12+ if [[ -z " $ENVIRONMENT " ]]; then
13+ echo " ❌ Error: Environment required (stage or production)"
14+ exit 1
15+ fi
16+
17+ if [[ " $ENVIRONMENT " != " stage" && " $ENVIRONMENT " != " production" ]]; then
18+ echo " ❌ Error: Environment must be 'stage' or 'production'"
19+ exit 1
20+ fi
21+
22+ # Get the image tag from git
23+ IMAGE_TAG=$( git describe --tags --always --dirty=" -dev" )
24+ echo " 🏷️ Deploying image tag: $IMAGE_TAG to $ENVIRONMENT "
25+
26+ # Clone terracode-infra repository
27+ WORK_DIR=" /tmp/terracode-infra-$ENVIRONMENT -$$ "
28+ git clone https://github.com/segmentio/terracode-infra.git " $WORK_DIR "
29+ cd " $WORK_DIR "
30+
31+ # Configure git user for commits
32+ git config user.email
" [email protected] " 33+ git config user.name " buildkite-kubeapply-pipeline"
34+
35+ # Create a new branch for the deployment
36+ BRANCH_NAME=" kubeapply-$ENVIRONMENT -deploy-$IMAGE_TAG -$( date +%s) "
37+ git checkout -b " $BRANCH_NAME "
38+
39+ # Update terraform configs based on environment
40+ if [[ " $ENVIRONMENT " == " stage" ]]; then
41+ echo " 📝 Updating stage terraform configs..."
42+ sed -i " s/lambda_image_tag.*=.*\" .*\" /lambda_image_tag = \" $IMAGE_TAG \" /" stage/eu-west-1/common-kubeapply/config.tf
43+ sed -i " s/lambda_image_tag.*=.*\" .*\" /lambda_image_tag = \" $IMAGE_TAG \" /" stage/us-west-2/core/kubeapply/config.tf
44+ git add stage/* /* /kubeapply/config.tf stage/* /common-kubeapply/config.tf
45+ REGIONS=" eu-west-1, us-west-2"
46+ elif [[ " $ENVIRONMENT " == " production" ]]; then
47+ echo " 📝 Updating production terraform config..."
48+ sed -i " s/lambda_image_tag.*=.*\" .*\" /lambda_image_tag = \" $IMAGE_TAG \" /" production/us-west-2/core/kubeapply/config.tf
49+ git add production/* /* /kubeapply/config.tf
50+ REGIONS=" us-west-2"
51+ fi
52+
53+ # Commit and push changes
54+ if git diff --cached --quiet; then
55+ echo " 📝 No changes to commit - terraform configs already up to date with $IMAGE_TAG "
56+ else
57+ git commit -m " Deploy kubeapply $IMAGE_TAG to $ENVIRONMENT "
58+ fi
59+ git push origin " $BRANCH_NAME "
60+
61+ echo " ✅ Branch created and pushed: $BRANCH_NAME "
62+
63+ # Create PR if requested
64+ if [[ " $CREATE_PR " == " true" ]]; then
65+ echo " 🔄 Creating pull request..."
66+
67+ if [[ " $ENVIRONMENT " == " stage" ]]; then
68+ PR_BODY=" Automated deployment of kubeapply image tag \` $IMAGE_TAG \` to stage environment.
69+
70+ **Instructions:**
71+ 1. Review the changes in this PR
72+ 2. Comment \` atlantis apply\` to deploy to stage environment
73+ 3. Merge this PR after successful deployment
74+
75+ **Image tag:** \` $IMAGE_TAG \`
76+ **Environment:** Stage
77+ **Regions:** $REGIONS "
78+ else
79+ PR_BODY=" Automated deployment of kubeapply image tag \` $IMAGE_TAG \` to production environment.
80+
81+ **Instructions:**
82+ 1. Review the changes in this PR carefully
83+ 2. Comment \` atlantis apply\` to deploy to production environment
84+ 3. Merge this PR after successful deployment
85+
86+ **Image tag:** \` $IMAGE_TAG \`
87+ **Environment:** Production
88+ **Region:** $REGIONS "
89+ fi
90+
91+ PR_URL=$( gh pr create \
92+ --title " Deploy kubeapply $IMAGE_TAG to $ENVIRONMENT " \
93+ --body " $PR_BODY " \
94+ --head " $BRANCH_NAME " \
95+ --base master)
96+
97+ echo " ✅ Pull request created!"
98+ echo " 🔗 PR URL: $PR_URL "
99+ echo " 📋 Instructions: Go to the PR and comment 'atlantis apply' to deploy to $ENVIRONMENT ."
100+
101+ # Add Buildkite annotation with deployment information
102+ buildkite-agent annotate --style info " 🚀 **Deployment PR Created**
103+
104+ 📦 **Image tag:** \` $IMAGE_TAG \`
105+ 🌍 **Environment:** $ENVIRONMENT
106+ 🗺️ **Regions:** $REGIONS
107+
108+ 🔗 **[View Deployment PR]($PR_URL )**
109+
110+ **Next Steps:**
111+ 1. Review the changes in the deployment PR
112+ 2. Comment \` atlantis apply\` on the PR to deploy to $ENVIRONMENT
113+ 3. Merge the PR after successful deployment"
114+ else
115+ echo " 📋 Branch ready for manual PR creation: $BRANCH_NAME "
116+ echo " 🔗 Create PR at: https://github.com/segmentio/terracode-infra/compare/master...$BRANCH_NAME "
117+ fi
0 commit comments