How To Schedule An AWS CodeBuild Using Trigger
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed different components of a buildspec file in CodeBuild.
https://cloudaffaire.com/aws-codebuild-buildspec-file-elements-examples/
In this blog post, we will discuss how to schedule an AWS CodeBuild using a trigger. You can create a trigger on a project to schedule a build once every hour, day, or week. You can also create a trigger using a custom rule with an Amazon CloudWatch cron expression. For example, using a cron expression, you can schedule a build at a specific time every weekday.
Next, we are going to create a CodeBuild project and then schedule the project build. Unfortunately at the time of writing this blog post, I am not able to find any AWS CLI option to schedule the build trigger, hence we will use both AWS CLI and AWS console for this demo. If you are aware of any such option in AWS CLI then kindly share in the comment section.
How To Schedule An AWS CodeBuild Using Trigger:
Requirements:
AWS CLI installed and configured. You can follow the below blog post to install and configure AWS CLI.
https://cloudaffaire.com/how-to-install-aws-cli/
https://cloudaffaire.com/how-to-configure-aws-cli/
Git installed and configured.
https://cloudaffaire.com/category/devops/git/
Step 1: Setup AWS CodeCommit repository to host your CodeBuild source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#################################################### ## How To Schedule An AWS CodeBuild Using Trigger ## #################################################### ## I am using a Linux shell to execute AWS CLI commands ## -------------------------------------------------- ## Setup CodeCommit to host source code for CodeBuild ## -------------------------------------------------- ## create a directory for this demo mkdir codebuld && cd codebuld ## clone the java source code git clone https://github.com/CloudAffaire/CodeBuild.git ## create a new repository aws codecommit create-repository \ --repository-name "myapp" \ --repository-description "myapp respository" \ --tags "Key=Name,Value=MYAPP" ## follow step 3 to 5 of below blog post to configure https ## cresentials for codecommit required to clone the repo throgh https ## https://cloudaffaire.com/how-to-access-an-aws-codecommit-repository-remotely/ ## clone your codecommit repository GIT_CLONE_HTTPS_URL=$(aws codecommit get-repository \ --repository-name "myapp" \ --query 'repositoryMetadata.cloneUrlHttp' \ --output text) && git clone $GIT_CLONE_HTTPS_URL ## provide username and password when prompted ## copy the files to codecommit local repo cp -r CodeBuild/src myapp/ cp CodeBuild/buildspec.yml myapp/ cp CodeBuild/pom.xml myapp/ ## commit and push cd myapp git config --global user.name "Debjeet" git config --global user.email "cloudaffaire@gmail.com" git add . git commit -m "code upload" git push cd .. |
Note: CodeBuild supports S3 bucket, CodeCommit, GitHub, and Bitbucket as your build source code location. In this demo we are using CodeCommit to show the integration of CodeCommit with CodeBuild.
Step 2: Create an IAM role for AWS CodeBuild.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
## -------------------------------- ## Create an IAM role for CodeBuild ## -------------------------------- ## create iam policy cat < { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "codebuild.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } EOF ## create iam role IAM_ROLE_ARN=$(aws iam create-role \ --role-name CodeBuildServiceRole \ --assume-role-policy-document file://role_assume_policy.json \ --query 'Role.Arn' \ --output text) ## attach some policies to the role (S3, CodeCommit, CloudWatch) aws iam attach-role-policy \ --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess \ --role-name CodeBuildServiceRole && aws iam attach-role-policy \ --policy-arn arn:aws:iam::aws:policy/AWSCodeCommitFullAccess \ --role-name CodeBuildServiceRole && aws iam attach-role-policy \ --policy-arn arn:aws:iam::aws:policy/CloudWatchFullAccess \ --role-name CodeBuildServiceRole |
Step 3: Create an S3 bucket to store your build outcome (artifacts).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ----------------------------------------- ## Create an S3 bucket for CodeBuild artifact ## ----------------------------------------- ## S3 bucket name (must be uniqe globally) S3_BUCKET_NAME="myappcbcloudaffaire" ## Create a new s3 bucket aws s3api create-bucket \ --bucket "$S3_BUCKET_NAME" \ --create-bucket-configuration "LocationConstraint=ap-south-1" ## Add a tag to the s3 bucket aws s3api put-bucket-tagging \ --bucket "$S3_BUCKET_NAME" \ --tagging 'TagSet=[{Key=Name,Value=MYAPP}]' |
Note: We can deploy this artifact using CodeDeploy in CodePipeline, which will be covered under CodeDeploy.
Step 4: Create a CodeBuild build project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## -------------------------------- ## Create a CodeBuild build project ## -------------------------------- ## create build project definition cat < { "name": "mycodebuildproject", "source": { "type": "CODECOMMIT", "location": "$GIT_CLONE_HTTPS_URL" }, "artifacts": { "type": "S3", "location": "$S3_BUCKET_NAME" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/amazonlinux2-x86_64-standard:3.0", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "$IAM_ROLE_ARN" } EOF ## create the build project aws codebuild create-project \ --cli-input-json file://build_project_def.json ## list all projects in your codebuild aws codebuild list-projects ## get project build details aws codebuild batch-get-projects \ --names "mycodebuildproject" |
Step 5: Log in to your AWS console and navigate to AWS CodeBuild, select your project, and click on the ‘Build trigger’ tab and click on ‘Create trigger’.
Step 6: Provide a name, description, and schedule for your build trigger and click on ‘Create trigger’.
AWS CodeBuild supports cron expression in the form of cron(Minutes Hours Day_of_month Month Day_of_week Year). To get more details you can refer to AWS documentation.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions
Our trigger created successfully.
Next, you can either wait for the trigger to get executed or execute the build manually. If you want to wait for the trigger then don’t forget to delete all the resources afterward or you will get charged.
Step 8: Start your CodeBuild build project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## ---------------------------------- ## Start your CodeBuild build project ## ---------------------------------- ## start build aws codebuild start-build \ --project-name "mycodebuildproject" ## get list of build for the project aws codebuild list-builds-for-project \ --project-name "mycodebuildproject" ## get build details AWS_CODEBULD_BUILD_ID=$(aws codebuild list-builds-for-project \ --project-name "mycodebuildproject" \ --query 'ids[0]' \ --output text) && aws codebuild batch-get-builds \ --ids $AWS_CODEBULD_BUILD_ID |
Note: Additional charges apply to build your source code. CodeBuild builds is charged on per minutes basis and you get 120 free every month for small compute which is used in this demo.
Step 9: Get your build artifacts and logs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## --------------------------------------------------- ## Get your CodeBuild build project logs and artifacts ## --------------------------------------------------- ## check the artifacts for the builds aws s3api list-objects \ --bucket $S3_BUCKET_NAME ## check cloudwatch logs for the build logs AWS_CLOUDWATCH_LOG_STREAM=`echo $AWS_CODEBULD_BUILD_ID | awk -F":" '{print $2}'` AWS_CLOUDWATCH_LOG_GROUP=$(aws logs describe-log-groups \ --query 'logGroups[0].logGroupName' \ --output text) aws logs get-log-events \ --log-group-name $AWS_CLOUDWATCH_LOG_GROUP \ --log-stream-name $AWS_CLOUDWATCH_LOG_STREAM \ --limit 10 |
Step 10: Cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
## ------- ## Cleanup ## ------- ## delete codebuild project aws codebuild delete-project \ --name "mycodebuildproject" ## delete cloudwatch logs aws logs delete-log-group \ --log-group-name $AWS_CLOUDWATCH_LOG_GROUP ## delete s3 artifact bucket aws s3 rm s3://$S3_BUCKET_NAME \ --recursive && aws s3 rb s3://$S3_BUCKET_NAME \ --force ## delete your codecommit repository aws codecommit delete-repository \ --repository-name "myapp" ## detach IAM role policy aws iam detach-role-policy \ --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess \ --role-name CodeBuildServiceRole && aws iam detach-role-policy \ --policy-arn arn:aws:iam::aws:policy/AWSCodeCommitFullAccess \ --role-name CodeBuildServiceRole && aws iam detach-role-policy \ --policy-arn arn:aws:iam::aws:policy/CloudWatchFullAccess \ --role-name CodeBuildServiceRole ## delete IAM role aws iam delete-role \ --role-name "CodeBuildServiceRole" ## delete the directory for this dmeo cd .. && rm -rf codebuld |
Hope you have enjoyed this blog post. To get more details on AWS CodeBuild, please refer below AWS documentation
https://docs.aws.amazon.com/codebuild/index.html