CodeBuild is a managed build service, similar to Jenkins’ but focused specifically on building automation and testing. It can be used with CodePipeline to build a fully automated CI / CD Pipeline.
Set up a construction project
If you use CodePipeline, you should create your CodeBuild project from the dialog box under “Add Construction Steps.”; This configures it to use CodePipeline as the source. If you do not use CodePipeline, you can create your construction project from the CodeBuild Management Console and manually enter the information for your source control.
Anyway, the first thing you configure is the environment in which your code will be built. It should not mean for most projects, but for those that do, you can specify custom Docker images to have full control over it. Otherwise, the default settings for Amazon Linux 2 and the default times should be good.
Under “Advanced settings” for environment, you will find the settings for changing the calculation settings, for selecting a different server type. You can also change environment variables from here.
For VPC settings, it is best to leave this alone, unless you have no reason to do otherwise. The default settings (blank) are probably good.
Next, you need to configure CodeBuild with a Buildspec file. This is a YAML template that defines the commands that CodeBuild will actually run. You can use an actual file, stored in the root of your project as
buildspec.yml, or you can enter some build commands manually if your configuration is not super complex.
Either way, you will probably need to take some time to find out your Buildspec file. This will depend a lot on the type of app you are running and what is needed to build and test.
For example, the following configuration will install the modules required for a TypeScript project, compile with
npm run buildand drive
npm test. The Buildspec file has a few different phases, which you can use to run commands at different times.
version: 0.2 phases: install: runtime-versions: nodejs: 10 commands: - npm install -g typescript - npm install pre_build: commands: - echo prebuild phase build: commands: - echo Build started on `date` - npm run build - npm test post_build: commands: - echo Build completed on `date` cache: paths: - 'node_modules/**/*' artifacts: files: - '**/*' name: yourBuildName-$(date +%Y-%m-%d)
You can consult AWS’s Buildspec reference for more information on how everything works.
Once you have received a file that you are happy with, you can choose to activate CloudWatch logs or log in to S3 instead.
Click on “Create construction project” and the installation should be complete.
Test your building
CodeBuild is usually run as part of CodePipeline. If you use CodePipeline, you must complete the installation for the deployment step and create your pipeline. It should run automatically and send the source to CodeBuild. Otherwise, you can start construction manually from the console, or use the AWS CLI:
aws codebuild start-build --project-name
This command also has many overrides.
You can see your builds output by clicking build in the management console. This shows you the logs from the construction process, which can be crucial in tracking errors.
If any command fails, the whole build will probably fail, so you want to make sure everything is correct. If you use CodePipeline, you change yours
buildspec.yml will trigger new pipeline updates and new builds.