CodeBuild is a managed build service, similar in function to Jenkins but focused specifically on automating building and testing. It can be used in conjunction with CodePipeline to build a fully automated CI/CD Pipeline.
Setting Up a Build Project
If you’re using CodePipeline, you should create your CodeBuild project from the dialog during “Add Build Stage.” This will configure it to use CodePipeline as the source. If you’re not using CodePipeline, you can create your build project from the CodeBuild Management Console, and manually enter in the details for your source control.
Under “Advanced Settings” for Environment, you’ll find the settings to change the compute settings, to select a different server type. You can also change environment variables from here.
For VPC settings, it’s best to leave this alone, unless you have a reason to do otherwise. The default settings (empty) are likely fine.
Next up, you’ll have 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 at the root of your project as buildspec.yml, or you can enter in a few build commands manually, if your configuration isn’t super complex.
Either way, you’ll likely have to take some time to figure out your Buildspec file. This will be highly dependant on the type of app you’re running, and what is necessary to build and test.
For example, the following configuration will install the modules necessary for a TypeScript project, compile with npm run build, and run npm test. The buildspec file has a few distinct phases, which you can use to run commands at different times.
You can consult AWS’s Buildspec reference for more information on how everything works.
Once you’ve got a file you’re happy with, you can choose to enable CloudWatch logs, or log to S3 instead.
Click “Create Build Project,” and your setup should be complete.
Testing Your Build
CodeBuild is usually ran as part of CodePipeline. If you’re using CodePipeline, you’ll have to finish up the setup for the deployment stage, and create your pipeline. It should run automatically, and send your source over to CodeBuild. Otherwise, you can start the build manually from the console, or using the AWS CLI:
This command has a lot of overrides as well.
You’ll be able to view your build’s output by clicking on the build in the management console. This will show you the logs from the build process, which can be crucial in tracking down errors.
If any command fails, the whole build will likely fail, so you’ll want to make sure everything is correct. If you’re using CodePipeline, changes to your buildspec.yml will trigger new pipeline updates, and new builds.