This serverless app solves a common complaint when using AWS CodeBuild as a CI solution: PR contributors don't have access to the build logs if the CI build fails on their PR branch. The app creates publicly accessible links to PR build logs for a given AWS CodeBuild project and posts them as a comment on the corresponding GitHub PR.
Here is an example GitHub PR comment:
- Contributors create or update a PR.
- Assuming AWS CodeBuild is already setup as the CI solution for this repo, the PR triggers a new CI build.
- Once the CI build completes (success or failure), a CloudWatch Event triggers an AWS Lambda function.
- If the event is for a PR build, the Lambda function
- copies the build log to an S3 bucket. Note, the build log auto-expires after a configurable number of days (default: 30).
- publishes a comment on the GitHub PR with a publicly accessible link to the logs. Note, the app uses the CodeBuild project's GitHub OAUTH token to post the comment.
- The logs link goes to an API Gateway endpoint, which redirects to a pre-signed URL for the build logs in the S3 bucket.
To attach this app to an existing AWS CodeBuild project in your AWS account,
- Go to the app's page on the Serverless Application Repository and click "Deploy"
- Provide the CodeBuild project name and any other parameters (see parameter details below) and click "Deploy"
Alternatively, if your CodeBuild project is defined in an AWS SAM template, this app can be embedded as a nested app inside that SAM template. To do this, visit the app's page on the AWS Lambda Console. Click the "Copy as SAM Resource" button and paste the copied YAML into your SAM template.
If you are an AWS CDK user, you can use the aws-serverless.CfnApplication construct to embed this app in your CDK application. Here is a TypeScript example:
import serverless = require('@aws-cdk/aws-sam');
new serverless.CfnApplication(this, 'GitHubCodeBuildLogsSAR', {
location: {
applicationId: 'arn:aws:serverlessrepo:us-east-1:277187709615:applications/github-codebuild-logs',
semanticVersion: '1.6.0'
},
parameters: {
CodeBuildProjectName: project.projectName
}
});
CodeBuildProjectName
(required) - Name of CodeBuild project this app is posting logs for.ExpirationInDays
(optional) - Number of days before a build's log page expires. Default: 30CodeBuildProjectCustomLogGroupName
(optional) - If the CodeBuild Project has a custom log group name, you can specify it here. If not provided, the app will assume the CodeBuild default log group name format of/aws/codebuild/<project name>
.GitHubOAuthToken
(optional) - OAuth token used for writing comments to GitHub PRs. If not provided, the app will attempt to pull an OAuth token from the CodeBuild project. Note, if your CodeBuild project does not have a GitHub OAuth token, e.g., it is being used to build a public GitHub repo, then this parameter will be required for the app to function properly.- NOTE: The access token used requires
public_repo
permissions for public repositories orrepo
for private repositories.
- NOTE: The access token used requires
DeletePreviousComments
(optional) - Set totrue
to delete previously posted PR comments before posting a new one. Default: falseLogLevel
(optional) - Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc. Default: INFOCommentOnSuccess
(optional) - Set tofalse
to not publish a comment when build is successful. Default: trueBuildEventTimeout
(optional) - Timeout for Process Build Event Lambda. Default: 60
ProcessBuildEventsFunctionName
- ProcessBuildEvents Lambda function name.ProcessBuildEventsFunctionArn
- ProcessBuildEvents Lambda function ARN.BuildLogsBucketName
- Build logs S3 bucket name.BuildLogsBucketArn
- Build logs S3 bucket ARN.
The following precautions are taken when the GitHubOAuthToken
parameter is provided since it's sensitive data:
- The NoEcho option is used on the parameter so the value will never be shown by CloudFormation.
- The app stores the value in AWS Secrets Manager.
This code is made available under the MIT license. See the LICENSE file.