Skip to content

Commit a528ed7

Browse files
author
yolo h8cker 93
committed
Save codebuild temp code
1 parent 6fa8205 commit a528ed7

4 files changed

Lines changed: 288 additions & 0 deletions

File tree

docker/aws/codebuild/buildspec.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: 0.2
2+
3+
phases:
4+
pre_build:
5+
commands:
6+
- echo Logging in to Amazon ECR...
7+
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
8+
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
9+
- IMAGE_TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION:-latest}
10+
- BUILD_TIMESTAMP=$(date -u '+%Y-%m-%d %H:%M:%S UTC')
11+
build:
12+
commands:
13+
- echo Build started on `date`
14+
- echo Building the Docker image...
15+
- cd docker/aws
16+
- docker build --build-arg GITHUB_TOKEN="$GITHUB_TOKEN" --build-arg BUILD_TIMESTAMP="$BUILD_TIMESTAMP" --platform linux/amd64 -f AWSCodeClash.Dockerfile -t $REPOSITORY_URI:$IMAGE_TAG .
17+
- docker tag $REPOSITORY_URI:$IMAGE_TAG $REPOSITORY_URI:latest
18+
post_build:
19+
commands:
20+
- echo Build completed on `date`
21+
- echo Pushing the Docker image...
22+
- docker push $REPOSITORY_URI:$IMAGE_TAG
23+
- docker push $REPOSITORY_URI:latest
24+
- printf '[{"name":"codeclash","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
25+
artifacts:
26+
files: imagedefinitions.json
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Configuration
5+
PROJECT_NAME="codeclash-build"
6+
REGION="us-east-1"
7+
ECR_REPOSITORY="codeclash"
8+
AWS_ACCOUNT_ID="039984708918"
9+
GITHUB_REPO="emagedoc/CodeClash"
10+
11+
# Colors for output
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
YELLOW='\033[1;33m'
15+
NC='\033[0m' # No Color
16+
17+
echo -e "${GREEN}Creating AWS CodeBuild project for CodeClash...${NC}"
18+
19+
# Check if project already exists
20+
# if aws codebuild batch-get-projects --names "$PROJECT_NAME" --region "$REGION" >/dev/null 2>&1; then
21+
# echo -e "${YELLOW}Project $PROJECT_NAME already exists. Updating...${NC}"
22+
# ACTION="update-project"
23+
# else
24+
# echo -e "${YELLOW}Creating new project $PROJECT_NAME...${NC}"
25+
ACTION="create-project"
26+
# fi
27+
28+
# Create or update the CodeBuild project
29+
aws codebuild $ACTION \
30+
--name "$PROJECT_NAME" \
31+
--description "Build CodeClash Docker images on AWS" \
32+
--source type=S3,location="codeclash-source/source.zip" \
33+
--artifacts type=NO_ARTIFACTS \
34+
--environment type=LINUX_CONTAINER,image=aws/codebuild/standard:7.0,computeType=BUILD_GENERAL1_MEDIUM,privilegedMode=true \
35+
--service-role "arn:aws:iam::$AWS_ACCOUNT_ID:role/kilian-codeclash-codebuild-service-role" \
36+
--region "$REGION"
37+
38+
if [ $? -eq 0 ]; then
39+
echo -e "${GREEN}CodeBuild project created/updated successfully!${NC}"
40+
echo -e "${YELLOW}Project details:${NC}"
41+
echo " Name: $PROJECT_NAME"
42+
echo " Region: $REGION"
43+
echo " GitHub Repo: $GITHUB_REPO"
44+
echo " Build Spec: docker/aws/buildspec.yml"
45+
echo ""
46+
echo -e "${GREEN}Next steps:${NC}"
47+
echo "1. Make sure your GitHub repo has the buildspec.yml file committed"
48+
echo "2. Set up GitHub webhook (optional) for automatic builds"
49+
echo "3. Use trigger_aws_build.sh to start builds"
50+
echo ""
51+
echo -e "${YELLOW}Console URL:${NC}"
52+
echo "https://$REGION.console.aws.amazon.com/codesuite/codebuild/projects/$PROJECT_NAME"
53+
else
54+
echo -e "${RED}Failed to create/update CodeBuild project${NC}"
55+
echo -e "${YELLOW}You may need to:${NC}"
56+
echo "1. Create a CodeBuild service role with ECR permissions"
57+
echo "2. Ensure you have proper IAM permissions"
58+
exit 1
59+
fi
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Configuration
5+
ROLE_NAME="kilian-codeclash-codebuild-service-role"
6+
POLICY_NAME="CodeBuildServiceRolePolicy"
7+
REGION="us-east-1"
8+
AWS_ACCOUNT_ID="039984708918"
9+
10+
# Colors for output
11+
RED='\033[0;31m'
12+
GREEN='\033[0;32m'
13+
YELLOW='\033[1;33m'
14+
NC='\033[0m' # No Color
15+
16+
echo -e "${GREEN}Creating CodeBuild service role and policies...${NC}"
17+
18+
# Create trust policy for CodeBuild
19+
cat > /tmp/codebuild-trust-policy.json << EOF
20+
{
21+
"Version": "2012-10-17",
22+
"Statement": [
23+
{
24+
"Effect": "Allow",
25+
"Principal": {
26+
"Service": "codebuild.amazonaws.com"
27+
},
28+
"Action": "sts:AssumeRole"
29+
}
30+
]
31+
}
32+
EOF
33+
34+
# Create the service role
35+
echo -e "${YELLOW}Creating IAM role: $ROLE_NAME${NC}"
36+
if aws iam get-role --role-name "$ROLE_NAME" >/dev/null 2>&1; then
37+
echo -e "${YELLOW}Role $ROLE_NAME already exists, skipping creation${NC}"
38+
else
39+
aws iam create-role \
40+
--role-name "$ROLE_NAME" \
41+
--assume-role-policy-document file:///tmp/codebuild-trust-policy.json \
42+
--description "Service role for CodeBuild to build CodeClash Docker images"
43+
echo -e "${GREEN}✅ Created IAM role: $ROLE_NAME${NC}"
44+
fi
45+
46+
# Create policy for ECR + minimal logging (required by CodeBuild)
47+
cat > /tmp/codebuild-policy.json << EOF
48+
{
49+
"Version": "2012-10-17",
50+
"Statement": [
51+
{
52+
"Effect": "Allow",
53+
"Action": [
54+
"logs:CreateLogGroup",
55+
"logs:CreateLogStream",
56+
"logs:PutLogEvents"
57+
],
58+
"Resource": "arn:aws:logs:$REGION:$AWS_ACCOUNT_ID:log-group:/aws/codebuild/*"
59+
},
60+
{
61+
"Effect": "Allow",
62+
"Action": [
63+
"ecr:BatchCheckLayerAvailability",
64+
"ecr:GetDownloadUrlForLayer",
65+
"ecr:BatchGetImage",
66+
"ecr:GetAuthorizationToken",
67+
"ecr:InitiateLayerUpload",
68+
"ecr:UploadLayerPart",
69+
"ecr:CompleteLayerUpload",
70+
"ecr:PutImage"
71+
],
72+
"Resource": "*"
73+
},
74+
{
75+
"Effect": "Allow",
76+
"Action": [
77+
"s3:GetObject",
78+
"s3:GetObjectVersion"
79+
],
80+
"Resource": "arn:aws:s3:::codeclash-source/*"
81+
}
82+
]
83+
}
84+
EOF
85+
86+
# Attach the ECR + logging policy
87+
echo -e "${YELLOW}Creating and attaching ECR + logging policy: $POLICY_NAME${NC}"
88+
if aws iam get-role-policy --role-name "$ROLE_NAME" --policy-name "$POLICY_NAME" >/dev/null 2>&1; then
89+
echo -e "${YELLOW}Policy $POLICY_NAME already exists, updating...${NC}"
90+
aws iam put-role-policy \
91+
--role-name "$ROLE_NAME" \
92+
--policy-name "$POLICY_NAME" \
93+
--policy-document file:///tmp/codebuild-policy.json
94+
else
95+
aws iam put-role-policy \
96+
--role-name "$ROLE_NAME" \
97+
--policy-name "$POLICY_NAME" \
98+
--policy-document file:///tmp/codebuild-policy.json
99+
fi
100+
echo -e "${GREEN}✅ Attached ECR + logging policy: $POLICY_NAME${NC}"
101+
102+
# Clean up temporary files
103+
rm -f /tmp/codebuild-trust-policy.json /tmp/codebuild-policy.json
104+
105+
# Wait a moment for IAM to propagate
106+
echo -e "${YELLOW}⏳ Waiting for IAM role to propagate...${NC}"
107+
sleep 10
108+
109+
echo -e "${GREEN}✅ CodeBuild service role setup complete!${NC}"
110+
echo -e "${YELLOW}Role ARN:${NC} arn:aws:iam::$AWS_ACCOUNT_ID:role/service-role/$ROLE_NAME"
111+
echo ""
112+
echo -e "${GREEN}Next steps:${NC}"
113+
echo "1. Run ./create_codebuild_project.sh to create the CodeBuild project"
114+
echo "2. Commit and push buildspec.yml to your repository"
115+
echo "3. Use ./trigger_aws_build.sh to start fast cloud builds"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Configuration
5+
PROJECT_NAME="codeclash-build"
6+
REGION="us-east-1"
7+
ECR_REPOSITORY="codeclash"
8+
AWS_ACCOUNT_ID="039984708918"
9+
10+
# Colors for output
11+
RED='\033[0;31m'
12+
GREEN='\033[0;32m'
13+
YELLOW='\033[1;33m'
14+
BLUE='\033[0;34m'
15+
NC='\033[0m' # No Color
16+
17+
echo -e "${GREEN}🚀 Triggering AWS CodeBuild for CodeClash (fast cloud build)...${NC}"
18+
echo -e "${BLUE}💡 Tip: Use build_and_push_aws.sh as a fallback if CodeBuild is unavailable${NC}"
19+
20+
# Check if required tools are installed
21+
command -v aws >/dev/null 2>&1 || { echo -e "${RED}Error: AWS CLI is required but not installed.${NC}" >&2; exit 1; }
22+
23+
# Check if GITHUB_TOKEN is set
24+
if [ -z "${GITHUB_TOKEN:-}" ]; then
25+
echo -e "${RED}Error: GITHUB_TOKEN environment variable is required.${NC}"
26+
echo "Please set it with: export GITHUB_TOKEN=your_token_here"
27+
exit 1
28+
fi
29+
30+
# Start the build
31+
echo -e "${YELLOW}Starting CodeBuild project: $PROJECT_NAME${NC}"
32+
33+
BUILD_ID=$(aws codebuild start-build \
34+
--project-name "$PROJECT_NAME" \
35+
--environment-variables-override \
36+
name=GITHUB_TOKEN,value="$GITHUB_TOKEN" \
37+
name=AWS_DEFAULT_REGION,value="$REGION" \
38+
name=AWS_ACCOUNT_ID,value="$AWS_ACCOUNT_ID" \
39+
name=IMAGE_REPO_NAME,value="$ECR_REPOSITORY" \
40+
--query 'build.id' \
41+
--output text)
42+
43+
if [ $? -eq 0 ]; then
44+
echo -e "${GREEN}Build started successfully!${NC}"
45+
echo -e "${YELLOW}Build ID: $BUILD_ID${NC}"
46+
echo -e "${YELLOW}You can monitor the build at:${NC}"
47+
echo "https://$REGION.console.aws.amazon.com/codesuite/codebuild/projects/$PROJECT_NAME/build/$BUILD_ID"
48+
49+
# Optional: Wait for build to complete
50+
read -p "Do you want to wait for the build to complete? (y/n): " -n 1 -r
51+
echo
52+
if [[ $REPLY =~ ^[Yy]$ ]]; then
53+
echo -e "${YELLOW}⏳ Waiting for build to complete...${NC}"
54+
55+
# Poll for completion
56+
while true; do
57+
STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" --query 'builds[0].buildStatus' --output text)
58+
case $STATUS in
59+
"SUCCEEDED")
60+
echo -e "${GREEN}✅ Build completed successfully!${NC}"
61+
echo -e "${GREEN}🐳 Docker image pushed to: $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$ECR_REPOSITORY:latest${NC}"
62+
break
63+
;;
64+
"FAILED"|"FAULT"|"STOPPED"|"TIMED_OUT")
65+
echo -e "${RED}❌ Build failed with status: $STATUS${NC}"
66+
echo -e "${YELLOW}💡 You can use the fallback script: ./build_and_push_aws.sh${NC}"
67+
exit 1
68+
;;
69+
"IN_PROGRESS")
70+
echo -e "${YELLOW}🔄 Build in progress...${NC}"
71+
sleep 30
72+
;;
73+
*)
74+
echo -e "${YELLOW}📊 Build status: $STATUS${NC}"
75+
sleep 30
76+
;;
77+
esac
78+
done
79+
else
80+
echo -e "${BLUE}🔗 Monitor your build at:${NC}"
81+
echo " https://$REGION.console.aws.amazon.com/codesuite/codebuild/projects/$PROJECT_NAME/build/$BUILD_ID"
82+
echo -e "${BLUE}💡 Or check status with:${NC}"
83+
echo " aws codebuild batch-get-builds --ids $BUILD_ID --query 'builds[0].buildStatus'"
84+
fi
85+
else
86+
echo -e "${RED}Failed to start build${NC}"
87+
exit 1
88+
fi

0 commit comments

Comments
 (0)