Organizations can have multiple environments like Development, Testing, QA, Staging, Production with own instance of API Managers. These environments often have different configurations for APIs.
WSO2 API Manager supports creating fully automated CI/CD pipelines for APIs. It supports a variety of features including maintaining the lifecycle of API, changing environment-specific configurations throughout the CI/CD process. This allows seamlessly migrating APIs across different environments reducing human intervene on the migration process.
CI/CD for API Manager relies on a Version Control system which acts as a Single Source of Truth for the pipeline. After APIs are exported from one environment, promoting it to the other environment is done via the CLI tool for API Manager known as apimcli. It is capable of handling environment-related configurations and can promote the API seamlessly to other environments via a single command.
Running the CLI tool
Step 1 - Deploy the API import/export tool
Download the latest version of WSO2 API Manager from http://wso2.com/products/api-manager/.
- Start WSO2 API Manager.
Download the latest WSO2 API import/export tool (
api-import-export-2.6.0-v14.war) from here.
Note that the import/export tool attached is specific to this version of WSO2 API Manager.
Make sure to delete all previous versions of the web app (e.g.,
api-import-export-2.6.0-v14.war), both the compressed and the extracted files, before copying over the new web app.
- This version of the API Import and Export web application is compatible with the WSO2 WUM updates from July 8, 2019 onwards. For more information on updating WSO2 API Manager, see Updating WSO2 API Manager.
- The previous version of the web application which is compatible with WUM updates that are prior to the above-mentioned date can be download from here.
Copy the downloaded
api-import-export-2.6.0-v14.warfile to the
The file is automatically deployed as hot deployment is enabled.
Step 2- Run the CLI tool
- Navigate to the API Management Tooling page - https://wso2.com/api-management/tooling/
- Click Download under CLI.
Select a generated archive suitable for your platform (i.e., Mac, Windows, Linux) and extract it the CLI tool that you downloaded to a desired location and
Navigate to the working directory where the executable CLI Tool resides.
Execute the following command to start the CLI tool.
Add the location of the extracted folder to your system's
$PATHvariable to be able to access the executable from anywhere.
For further instructions execute the following command.
Global flags for CLI tool
The following are some global flags that you can use with the CLI tool.
- Then you need to install apimcli tool from here. After downloading the correct version for your OS, add the CLI to the $PATH of the system to access CLI tool from anywhere.
Once the tool installed you can introduce API Manager environments using add-env command.
For example, you can configure an API Manager running on port 9443 as your development(dev) environment by issuing the following command.
Let’s add another environment for production purposes
Exporting APIs with CLI
Now we have to add two different environments. Our end goal is to automate the API migration between dev and prod environments. Publish an API in dev environment using Publisher UI of API Manager. If you don’t know how to follow Quick Start Guide to learn how to deploy an API in the API Manager.
For this example, we are gonna use the Swagger Petstore API.
Start creating it by using “Add New API” and selecting an existing API option. Then create the API and set its name as SwaggerPetstore and Version as 1.0.0
2. Next, put backend URLs for Production and Sandbox and next page Publish the API.
For this example, we can use
Login to API Manager from CLI
Now before exporting this API as a project, we need to login to the API Manager from the CLI.
Export an API
After successfully logged in we can export the API via CLI using a simple command,
After providing name, version and provider you can see the API is downloaded as an archive to your machine.
We are using insecure(-k) flag because we are using self-signed certificates.
Preparing API Project for CI/CD
After exporting the API as an archive, extract the content. You will find a directory called SwaggerPetstore-1.0.0
Copy this directory into your Version Control Repository. Rename it to SwaggerPetstore to reference it easily.
Next, when we promoting API to the production environment we want to have different backend URLs for Production and Sandbox. For easily do this CLI tool supports a special file called api_params
Adding environment-specific parameters for APIs
Create a file called api_params.yaml inside your SwaggerPetstore directory. Open this api_params.yaml file in your favourite text editor.
Adding environment-specific parameters for APIs is done via this file. We are going to change backends in the production environment to point for production endpoints
Now we can commit this project to a version control system.
Importing API into a different environment
Now we can try out importing this API into the production environment and test if the API works. To do this issue
(You need to login to prod environment before issuing the command)
With -f flag we point to the archive or project directory and with -e flag we provide target environment for the import process.
2. Now the tool will automatically detect the target environment and prepare a new artifact containing environment-related details.
3. You can investigate that the API has been imported with correct environment-specific details you defined and you can also see the API is in the published state too.
If you try again importing the same API to prod environment it will fail as API Manager does not allow updating existing APIs by default, it is to ensure that any accidental modification not happen.
But don’t worry, you can issue the same command with an additional flag called --update to update an existing API
Now if you try again you can see it successfully imports the API
Automating API promotion with CI/CD Pipeline
Now you know building blocks of API import/export. By using the above commands you can create an automated pipeline for API promotion between environments.
After installing the CLI tool on the CI/CD server you can configure environments using add-env as we did earlier. As the next step, you can create a pipeline with your automation server.
For a reference, we are providing you with a Jenkins pipeline
You can see we have used an additional flag called --preserveProvider in the Jenkinsfile. It is used because if the User exported API does not exist in the target environment provider will be set to the current user who executes the import command.
Using environment variables
There are situations where hiding certain information is required in parameters file or reuse values across the script. You can define them as environment variables in the parameters file in usual notation
When importing the CLI tool will pick these values from your shell environment and replace them.
An example project containing all these can be found here.