Know How Guide and Hands on Guide for AWS
CodeBuild uses the build project to create the build environment.
CodeBuild downloads the source code into the build environment and then uses the build specification (buildspec)to run a build.
The build environment uploads its output to an S3 bucket. It can also send build notifications to an Amazon SNS topic
While the build is running, the build environment sends information to CodeBuild and Amazon CloudWatch Logs.
aws s3 mb s3://codebuild-regionID-accountID-input-bucket --region cn-northwest-1
aws s3 mb s3://codebuild-regionID-accountID-output-bucket --region cn-northwest-1
(root directory name)
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- MessageUtil.java
`-- test
`-- java
`-- TestMessageUtil.java
3. Create the buildspec file buildspec.yml
- buildspec.yml
```yaml
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
pre_build:
commands:
- echo Nothing to do in the pre_build phase...
build:
commands:
- echo Build started on `date`
- mvn install
post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- target/messageUtil-1.0.jar
(root directory name)
|-- pom.xml
|-- buildspec.yml
`-- src
|-- main
| `-- java
| `-- MessageUtil.java
`-- test
`-- java
`-- TestMessageUtil.java
Check in the code to CodeCommit repository Follow up the Get start codecommit to check in the code to CodeCommit repository MyDemoRepo.
Create the build project
If a CodeBuild console, choose Create build project. OR, on the navigation pane, expand Build, choose Build projects, and then choose Create build project codebuild-demo-project
.
Make sure the user with the AWSCodeBuildAdminAccess, AmazonS3ReadOnlyAccess, and IAMFullAccess managed permission to create the CodeBuild project
The CodeBuild service role used for run a CodeBuild build.
aws codebuild create-project --cli-input-json file://create-project.json --region cn-northwest-1
create-project.json
{
"name": "codebuild-demo-project",
"source": {
"type": "CODECOMMIT",
"location": "https://git-codecommit.cn-northwest-1.amazonaws.com.cn/v1/repos/MyDemoRepo",
"sourceIdentifier": "source-codecommit"
},
"artifacts": {
"type": "S3",
"location": "codebuild-regionID-accountID-output-bucket"
},
"environment": {
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/amazonlinux2-x86_64-standard:2.0",
"computeType": "BUILD_GENERAL1_SMALL"
},
"serviceRole": "arn:aws-cn:iam::account-ID:role/codebuild-project-service-role",
"encryptionKey": "arn:aws-cn:kms:region-ID:account-ID:key/key-ID"
}
You can also create the source code location on S3 bucket
MessageUtil.zip
|-- pom.xml
|-- buildspec.yml
`-- src
|-- main
| `-- java
| `-- MessageUtil.java
`-- test
`-- java
`-- TestMessageUtil.java
aws s3 cp MessageUtil.zip s3://codebuild-regionID-accountID-input-bucket/codebuild-demo-project/MessageUtil.zip --region cn-northwest-1
aws codebuild create-project --cli-input-json file://create-project-s3.json --region cn-northwest-1
create-project-s3.json
{
"name": "codebuild-demo-project",
"source": {
"type": "S3",
"location": "codebuild-regionID-accountID-input-bucket/MessageUtil.zip"
},
"artifacts": {
"type": "S3",
"location": "codebuild-regionID-accountID-output-bucket"
},
"environment": {
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/amazonlinux2-x86_64-standard:2.0",
"computeType": "BUILD_GENERAL1_SMALL"
},
"serviceRole": "arn:aws-cn:iam::account-ID:role/codebuild-project-service-role",
"encryptionKey": "arn:aws-cn:kms:region-ID:account-ID:key/key-ID"
}
codebuild-demo-project
, and then choose Start build
.
aws codebuild start-build --project-name codebuild-demo-project --region cn-northwest-1
codebuild-demo-project:build-ID
-> Build history
-> Phase details
to see the summary infocodebuild-demo-project:build-ID
-> Build history
-> Build logs
to see the details info
aws codebuild batch-get-builds --ids codebuild-demo-project:8cf3268e-1210-4cd5-bc42-02db2decdfbf --region cn-northwest-1 --query 'builds[*].[id,currentPhase,buildStatus,phases[*].[phaseType, phaseStatus, contexts]]'