Working with scheduled CircleCI pipelines can sometimes be cumbersome. In this blog post, we will learn how Postman can supercharge your experience with CircleCI pipelines, particularly with scheduled pipeline runs.
Let’s go over the problems and solutions when working with scheduled CircleCI pipelines.
Limitations associated with scheduled CircleCI pipelines
Imagine you want to schedule your CircleCI pipeline to run at midnight for your nightly workflow (test run). Below is the code snippet of how you could schedule that in CircleCI config.yml:
There is nothing wrong with this: The pipeline run will start on time, and you will have your scheduled run fully functioning. However, what if you want to change the schedule? What if you want to modify the cron pattern?
You would need to make changes in the pipeline’s config.yml file, commit the changes, push to a remote repository, raise a pull request, get approval, and finally merge your changes. Sigh. That can be a long way to go. Now, what if you have multiple scheduled workflows and you want to make changes in several workflow schedules? This is not an easy or fast task to achieve.
So how can you avoid this endless loop of a pipeline configuration change, commit, merge, etc.? The best way is to use Postman monitors in conjunction with CircleCI.
How to trigger a CircleCI pipeline via API request in Postman
CircleCI provides some handy APIs for those who want to run pipelines programmatically. First, let’s look at what you need to change in the config.yml file to make maximum use of APIs.
A config.yml file configuration with parameters and conditional workflow
As you can see in the above image, we have removed the hard-coded schedule logic from the config.yml file because we are going to trigger the pipeline run through an API call. Instead, we implemented a conditional logic with the help of CircleCI pipeline parameters so that the nightly workflow will run only when we provide the run_nightly_tests parameter inside our API request body with a Boolean value equal to true. Why is this necessary? Because if we don’t implement conditional logic for our workflow, it will run upon every code commit, which we don’t want.
If you want to dive deeper, here is the full configuration config.yml file:
Below is the API request inside a Postman Collection named Run CircleCI Pipeline:
You can see that the API request body contains information about the branch on which we want to execute our nightly tests and also the required parameter to trigger the nightly test run workflow run_nightly_tests: true.
What about the URL? We need to specify the version control system (VCS), username or organization name, and the VCS repository name. You can find detailed guidelines about how to construct the URL of the request and all requirements in the official documentation of CircleCI API.
Note: You need to have CIRCLECI_PERSONAL_TOKEN set up through your CircleCI project and include that token in the API request header as shown below:
Now, when our API request is ready, we can send it and see in CircleCI that it triggers the pipeline run:
So far, we’ve figured out how to trigger pipelines through API requests, but we still have to set up our desired schedule. We are almost there!
How to schedule a CircleCI pipeline run with Postman monitors
Postman’s monitoring service allows you to monitor API health, performance, schedule monitor runs for collections, perform continuous testing, integrate monitors with third-party services such as Slack, Opsgenie, Datadog, and more.
Let’s create a monitor for our Run CircleCI Pipeline collection, which contains our API request:
Select the collection from the Collection drop-down and specify the desired schedule for the monitor: In this case, select every day at midnight.
Run the monitor, and it will again trigger the same nightly test pipeline run in CircleCI.
You can now move on and modify the monitor schedule however you want with the wide variety of options that the Postman monitoring service allows—and all that without changing a single line of configuration in the CircleCI config.yml file. Congratulations, now you have a graphical user interface (GUI) for your scheduled pipeline.
4 key advantages of Postman monitors
Here is a summary of several crucial advantages of using Postman monitors for running CircleCI pipelines:
- Easy, fast, and flexible schedule maintenance. With the help of Postman monitor schedules, you can now just edit a monitor and choose whatever schedule you want for your pipeline instead of making changes in the pipeline configuration file, committing changes, etc.
- Postman monitor graphs allow you to visualize your monitor runs, which means that you can see when and how many times your pipeline was triggered.
- Postman allows you to select “Pause” or “Resume” monitors, which means that you can pause or resume CircleCI pipelines. This is an entirely new feature that is only possible if you use Postman monitors because CircleCI does not completely support it on its own.
- With the help of the Postman Monitor API, you can run your monitor with just a single API call. In comparison with the CircleCI API request, where we provided our parameters and branch in the request body, you don’t need to specify anything for monitor run. Just a single POST request with an empty payload, and your monitor/pipeline is running.
Monitor details showing the “Paused” state of the monitor, “Resume” button, and a single monitor run in the graph
In this post, we saw how extremely convenient it is to work with CircleCI pipelines using Postman—especially with Postman monitors. Postman monitors not only allow you to schedule pipeline runs in a fast and easy way, but they also provide a completely new feature of pausing or resuming pipelines, giving you more power over your CI processes and testing.
Postman provides a wide range of functions and features to assist with API development, testing, and collaboration. Here are some commonly used functions in Postman:
Creating and Managing Requests: Postman allows you to create API requests by specifying the request method, URL, headers, parameters, and body. You can manage and organize requests within collections, including creating folders, adding descriptions, and reordering requests.
Request and Response Visualization: Postman provides a user-friendly interface to view and analyze request and response data. It supports syntax highlighting for various data formats such as JSON, XML, and HTML, making it easier to understand and validate the data.
Environment and Variables: Postman allows you to define variables and environments. Variables enable you to store and reuse dynamic values across requests, making them flexible and easy to maintain. Environments provide sets of variables specific to different environments (e.g., development, staging, production).
Pre-request Scripts: Postman enables you to execute scripts before sending API requests using pre-request scripts. These scripts can be used to dynamically generate values, manipulate data, or set variables based on specific conditions.
Collection Runner: The Collection Runner allows you to execute a series of requests in a collection. It enables you to perform data-driven testing by iterating over multiple sets of data or environments. You can configure iterations, delays, and data sources for more comprehensive testing.
Mock Servers: Postman allows you to create mock servers for simulating API responses without a live backend. Mock servers are useful during development, allowing frontend developers to work independently by providing simulated API responses.
Documentation Generation: Postman can automatically generate documentation for your APIs based on your requests and collections. It provides a simple way to share API specifications and details with stakeholders.
Collaboration and Teamwork: Postman offers collaboration features such as sharing collections, collaborating on requests, and commenting on specific requests or collections. It also supports version control integration to manage changes and updates effectively.
Integration and Automation: Postman integrates with various tools and services, including version control systems (e.g., Git), CI/CD platforms (e.g., Jenkins), and API management solutions. It provides options for integrating with these tools to automate API testing and deployment processes.