In this section, you will create and setup a build project in AWS CodeBuild.
AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy.
With CodeBuild, you don’t need to provision, manage, and scale your own build servers
Open the AWS CodeBuild console. Double check that you are using CodeBuild in the same AWS Region that you have used in the previous steps.
Click on Create build project.
In the Project configuration section, enter MyDemoBuild as the Project name and leave the rest as defaults in this section.
export IMAGE_URI=$(aws ecr describe-repositories --repository-name sc22-container --query "repositories[0].repositoryUri" --output text)
echo $IMAGE_URI
In the Buildspec section, select Use a buildspec file option. By default CodeBuild looks for a file named buildspec.yml in the source code root directory. We will create a buildspec.yml file in a later step.
Keep the defaults in Batch configuration and Artifacts section.
In the Logs section enable the CloudWatch logs. This option will upload the build output logs to CloudWatch
Click on Create build project
aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess --role-name codebuild-MyDemoBuild-service-role
When executing the above if you run into an error as shown below, it means you have not disabled AWS managed temporary credentials in Cloud9 as covered in the Preparation section of the Lab. Kindly fix that and re-do the above step.
A buildspec is a collection of build commands and related settings in YAML format. This file is used by AWS CodeBuild to automatically create an updated version of the container upon code changes. The buildspec file informs CodeBuild of all the actions that should be taken during a build run for your application. In the next section, you will dive deeper on what is CodeBuild and how to set it up as part of a pipeline.
cat > ~/environment/MyDemoRepo/buildspec.yml << EOF
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- \$(aws ecr get-login --region \$AWS_REGION --no-include-email)
- IMAGE_TAG=\$(echo \$CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-8)
- echo IMAGE TAG \$IMAGE_TAG
build:
commands:
- echo Build started at \$(date)
- echo Building the Docker image...
- docker build -t \$IMAGE_URI:latest .
- docker tag \$IMAGE_URI:latest \$IMAGE_URI:\$IMAGE_TAG
post_build:
commands:
- echo Build completed at $(date)
- echo Pushing the Docker images...
- docker push \$IMAGE_URI:latest
- docker push \$IMAGE_URI:\$IMAGE_TAG
EOF
git add buildspec.yml
git commit -m "add build specification file"
git push
In the next section, you will build a CodePipeline which you will use to automate your container build process