Get start code build

Know How Guide and Hands on Guide for AWS

Get start code build

How CodeBuild works

  1. Provide CodeBuild with a build project. Includes information about
    • How to run a build
    • Where to get the source code
    • Which build environment to use
    • Which build commands to run
    • Where to store the build output.
  2. CodeBuild uses the build project to create the build environment.

  3. CodeBuild downloads the source code into the build environment and then uses the build specification (buildspec)to run a build.

  4. The build environment uploads its output to an S3 bucket. It can also send build notifications to an Amazon SNS topic

  5. While the build is running, the build environment sends information to CodeBuild and Amazon CloudWatch Logs.

  6. While the build is running, you can use the AWS CodeBuild console, AWS CLI, or AWS SDKs to get summarized build information from CodeBuild and detailed build information from Amazon CloudWatch Logs.

codebuild-arch

Getting started with AWS CodeBuild

getting-started-console

getting-started-cli

  1. Create two S3 buckets
    • S3 bucket for build input. codebuild-regionID-accountID-input-bucket
    • S3 bucket for build output. codebuild-regionID-accountID-output-bucket
      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
      
  2. Create source code
    • Source code structure
      (root directory name)
       |-- pom.xml
       `-- src
          |-- main
          |     `-- java
          |           `-- MessageUtil.java
          `-- test
                `-- java
                      `-- TestMessageUtil.java
      
4.0.0 org.example messageUtil 1.0 jar Message Utility Java Sample App junit junit 4.11 test org.apache.maven.plugins maven-compiler-plugin 3.8.0

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
  1. Check in the code to CodeCommit repository Follow up the Get start codecommit to check in the code to CodeCommit repository MyDemoRepo.

  2. 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"
}

codebuild-project

codebuild-codecommit-source

codebuild-environment

codebuild-role

codebuild-buildspec

codebuild-artifact

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"
}
  1. Run the build In the list of build projects, choose codebuild-demo-project, and then choose Start build.
    aws codebuild start-build --project-name codebuild-demo-project --region cn-northwest-1
    
  2. View build information
    • Select codebuild-demo-project:build-ID -> Build history -> Phase details to see the summary info
    • Select codebuild-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]]'
      

      codebuild-build-status