In this post you will learn how to automate your own build of postman collections with aws codecommit, codepipeline, and codebuild.
Newman is a command-line collection runner for Postman.
Postman is an API platform for building and using APIs.
CodeCommit is a secure, highly scalable, managed source control service that hosts private Git repositories.
CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy.
CodePipeline is a continuous delivery service you can use to model, visualize, and automate the steps required to release your software.
File Structure
── /CodeCommitRepo/ # root folder
├── /buildspec.yml # source code for build commands
└── /api.postman_collection.json # postman collection
Overview
- Export Postman Collection
- Create Buildspec.yml
- Create CodeCommit Repository and push files
- Create S3 bucket for output report
- Create a build project in AWS CodeBuild
- Create pipeline in CodePipeline and build all together
1. Export Postman Collection
Open your postman collection and select the more actions icon More actions icon next to the collection, then select Export.
Select the format you'd like your collection to export as then select export to download your newly generated JSON file.
2. Create Buildspec.yml
Copy the code below to buildspec.yml
. Make sure to place your file along with your exported postman collection.
version: 0.2
phases:
pre_build:
commands:
- npm install -g newman
- npm install -g newman-reporter-html
build:
commands:
- newman run -r html,cli test_api.postman_collection.json --reporter-html-export report.html
artifacts:
files:
- report.html
This installs newman and newman-reporter-html dependencies for our postman cli. After prebuild, this will run cli command to run and generate html report from our postman collection newman run -r html,cli {name}.postman_collection.json --reporter-html-export report.html
.
3. Create CodeCommit Repository and push files
Open the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home then click create repository
Enter Repository name and description (optional) then click create
Clone repository and push your buildspec and postman file. You can specify a certain branch but we will just be using master.
4. Create s3 bucket for report
This will serve as the output folder for our generated html report.
Open the S3 console at https://console.aws.amazon.com/s3/ then click Create bucket
Enter bucket name and click Create bucket
5. Create a build project in AWS CodeBuild
Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home then click Create project
Fill in the following sections. Once complete, choose Create build project at the bottom of the page.
Sections:
Project configuration - Name and description
Source provider - AWS Codecommit, repo name
Environment - Ubuntu, Standard, aws/codebuild/standard:6.0
Buildspec
Batch configuration
Artifacts - s3, bucket name
Logs
6. Create pipeline in CodePipeline and build all together
We will now combine our process by using AWS CodePipeline to pull our source code in CodeCommit, run build with AWS CodeBuild and output generated html in S3
Open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home and choose Create Pipeline
On the Step 1: Choose pipeline settings page; in Pipeline name, enter the name for your pipeline.
Choose New service role to allow CodePipeline to create a new service role in IAM.
On the Step 2: Add source stage page; in Source provider, select AWS CodeCommit, repo name, and branch name.
On the Step 3: Add build stage page, select AWS Codebuild and project name
On the Step 4: Add deploy stage page, Choose Skip deploy stage.
On the Step 5: Review, Review all changes then click create pipeline.
Output
You should see Succeeded status after running pipeline. The build log shows from prebuild ,running command, and generating output.
Go to your s3 bucket to view successful report
To share this html report with others. You can use presigned URLs
To generate a presigned URL using the AWS Management Console
- In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for.
- In the Objects list, select the object that you want to create a presigned URL for.
Choose Create presigned URL.
When a confirmation appears, the URL is automatically copied to your clipboard. You will see a button to copy the
presigned URL if you need to copy it again.
References:
https://learning.postman.com/docs/running-collections/using-newman-cli/installing-running-newman/
https://www.npmjs.com/package/newman-reporter-html
https://unmesh.me/2017/06/10/api-testing-with-postman-collections-in-aws-codepipeline/
Top comments (1)
Awesome article, will definitely try this!