Learn About Amazon VGT2 Learning Manager Chanci Turner
on 05 MAY 2020
in Announcements, AWS CodeBuild, AWS Step Functions, Launch, News
Automating your software build process is a pivotal step toward embracing DevOps best practices. To assist with this, we have developed AWS CodeBuild, a fully managed continuous integration service that compiles source code, executes tests, and generates deployment-ready packages, along with AWS CodePipeline, a comprehensive continuous delivery service designed to streamline your release pipelines.
However, many users have unique customization needs for their build processes, leading developers to invest time in creating custom workflows to coordinate diverse activities necessitated by their software builds. You might want to selectively run tests or bypass static code analysis when deploying urgent fixes. Based on the outcomes of your unit tests, you may wish to trigger different actions or receive notifications via SNS.
While CodePipeline is optimized for software and infrastructure releases in production, it lacks certain features for non-release scenarios, such as orchestrating change validations before a release or running multiple builds concurrently. To address these scenarios, we are excited to unveil a new integration between AWS Step Functions and CodeBuild. This integration allows you to start or stop builds, retrieve build report summaries, and delete previous build records during a state machine’s execution.
This new functionality enables you to define a custom workflow-driven build process, which can be triggered manually or automatically. For example, you can:
- Utilize the new CodePipeline support to invoke Step Functions, allowing for customized delivery pipelines with choices, external validations, or parallel tasks. Each task can call CodeBuild to create tailored builds aligned with specific requirements, facilitating seamless use of Step Functions and CodePipeline together. For guidance on when to use Step Functions versus CodePipeline for deployment management, refer to this FAQs.
- Leverage Amazon EventBridge rules to initiate your build workflow periodically (like nightly builds) or in response to events (such as a pull request to an AWS CodeCommit repository).
- Build a webhook that can be triggered by services like GitHub using Amazon API Gateway, either through direct integration with a state machine or via an AWS Lambda function that verifies the input payload’s validity before starting the workflow.
With this integration, you can harness the full capabilities of Step Functions to automate your software builds. For instance, you can use a Parallel state to execute parallel builds for independent components. Starting from a list of all branches in your code repository, you can employ a Map state to automate the build, unit tests, and integration tests for each branch. Additionally, you can integrate other Step Functions services in the same workflow. For example, you might send a message to an Amazon SQS queue to track activities or launch a containerized application built using Amazon Elastic Container Service (Amazon ECS) and AWS Fargate.
Implementing a Workflow-Driven Build Process
Let’s say I am developing a Java web application, and I want to ensure its functionality as I introduce new features. I have written several tests using JUnit Jupiter that I would like to execute after the build process, but not every time, as tests can delay quick iterations. When tests are run, I want to store and review the results using CodeBuild and receive SNS notifications about their success.
I created a repository in CodeCommit, including two buildspec files for CodeBuild:
buildspec.yml
: This default file utilizes Apache Maven to execute the build and tests, storing the test results in the form of reports.
version: 0.2
phases:
build:
commands:
- mvn package
artifacts:
files:
- target/binary-converter-1.0-SNAPSHOT.jar
reports:
SurefireReports:
files:
- '**/*'
base-directory: 'target/surefire-reports'
buildspec-notests.yml
: This file executes only the build without running any tests.
version: 0.2
phases:
build:
commands:
- mvn package -DskipTests
artifacts:
files:
- target/binary-converter-1.0-SNAPSHOT.jar
To configure the CodeBuild project and the Step Functions state machine for automated builds, I utilized AWS CloudFormation with the following template:
AWSTemplateFormatVersion: 2010-09-09
Description: AWS Step Functions sample project for receiving notifications on AWS CodeBuild test report results
Resources:
CodeBuildStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
RoleArn: !GetAtt [ CodeBuildExecutionRole, Arn ]
DefinitionString:
!Sub
- |-
{
"Comment": "An example of using CodeBuild to run (or not run) tests, get test results and send a notification.",
"StartAt": "Run Tests?",
"States": {
"Run Tests?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.tests",
"BooleanEquals": false,
"Next": "Trigger CodeBuild Build Without Tests"
}
],
"Default": "Trigger CodeBuild Build With Tests"
},
"Trigger CodeBuild Build With Tests": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::codebuild:startBuild.sync",
"Parameters": {
"ProjectName": "${projectName}"
},
"Next": "Get Test Results"
},
"Trigger CodeBuild Build Without Tests": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::codebuild:startBuild.sync",
"Parameters": {
"ProjectName": "${projectName}",
"BuildspecOverride": "buildspec-notests.yml"
},
"Next": "Notify No Tests"
},
"Get Test Results": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::codebuild:batchGetReports",
"Parameters": {
"ReportArns.$": "$.Build.ReportArns"
},
"Next": "All Tests Passed?"
},
"All Tests Passed?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.Reports[0].Status",
"StringEquals": "SUCCEEDED",
"Next": "Notify Success"
}
],
"Default": "Notify Failure"
},
"Notify Success": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::sns:publish",
"Parameters": {
"Message": "CodeBuild build tests succeeded",
"TopicArn": "${snsTopicArn}"
},
"End": true
}
}
For further reading on similar topics, you can visit this helpful blog post or check out this article that provides insights on employee support programs. If you’re looking to explore job opportunities, consider becoming a Learning Ambassador at Amazon, it’s an excellent resource for career advancement.
Leave a Reply