CodeBuild Basic


  • AWS 託管 Build Service類似 Jenkins。可以編譯,跑單元測試,或者打包 package 到 artifacts.
  • 根據 運行力 和 執行時間來計價
  • Build Log 可以送到 CloudWatch or S3
  • CodeBuild 是使用 Docker 技術,所以可以在 Local 執行。也可以客製化個人 Docker Image 去當 Build 環境
  • Source 可以從 Github, CodeCommit, S3, Bitbucket 和 Github Enterprise 拉
  • 可以設定 Build Timeout 或 Queue Timeout
  • 也可以設定特定 VPC 執行
  • AWS 有預設 Environment Variable ,也可以客製化個人的環境變數

Cloud Watch


  • Monitor Metrix
    • type
      • Succeeded Builds Sum
      • Failed Builds Sum
      • Build Sum
      • Duration Average
    • 可以根據 Matrix 設定 Alert 通知
  • CloudWatch Event
    • 可以設定 Scheduler 去觸發 Build
    • 可以聽特定 Event 去 Trigger 其他 AWS 服務 類似 Lambda , SNS 等等

Buildspec Example Java Package to Artifacts


 1version: 0.2
 2
 3env:
 4  variables:
 5    JAVA_HOME: "/usr/lib/jvm/java-8-openjdk-amd64"
 6  parameter-store:
 7    LOGIN_PASSWORD: /CodeBuild/dockerLoginPassword
 8
 9phases:
10  install:
11    commands:
12      - echo Entered the install phase...
13      - apt-get update -y
14      - apt-get install -y maven
15    finally:
16      - echo This always runs even if the update or install command fails 
17  pre_build:
18    commands:
19      - echo Entered the pre_build phase...
20      - docker login -u User -p $LOGIN_PASSWORD
21    finally:
22      - echo This always runs even if the login command fails 
23  build:
24    commands:
25      - echo Entered the build phase...
26      - echo Build started on `date`
27      - mvn install
28    finally:
29      - echo This always runs even if the install command fails
30  post_build:
31    commands:
32      - echo Entered the post_build phase...
33      - echo Build completed on `date`
34
35reports:
36  arn:aws:codebuild:your-region:your-aws-account-id:report-group/report-group-name-1:
37    files:
38      - "**/*"
39    base-directory: 'target/tests/reports'
40    discard-paths: no
41  reportGroupCucumberJson:
42    files:
43      - 'cucumber/target/cucumber-tests.xml'
44    discard-paths: yes
45    file-format: CUCUMBERJSON # default is JUNITXML
46artifacts:
47  files:
48    - target/messageUtil-1.0.jar
49  discard-paths: yes
50  secondary-artifacts:
51    artifact1:
52      files:
53        - target/artifact-1.0.jar
54      discard-paths: yes
55    artifact2:
56      files:
57        - target/artifact-2.0.jar
58      discard-paths: yes
59cache:
60  paths:
61    - '/root/.m2/**/*'

Buildspec Example Build Docker Image to ECR


 1version: 0.2
 2
 3phases:
 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  build:
 9    commands:
10      - echo Build started on `date`
11      - echo Building the Docker image...          
12      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
13      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
14  post_build:
15    commands:
16      - echo Build completed on `date`
17      - echo Pushing the Docker image...
18      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

使用 Code Build 驗證 Code Commit Pull Request

原始 AWS 文章 Validating AWS CodeCommit Pull Requests with AWS CodeBuild and AWS Lambda

validating-aws-codecommit-pull-requests-with-aws-codebuild-and-aws-lambda