APIs have become the defacto for connecting apps, services and data. Organizations have multiple environments such as Dev, Test and Prod for different purposes. Therefore, the APIs need to be available in each environment after developers specify the required conditions. Manually promoting APIs between environments is a tedious, error-prone, and time-consuming task. This drastically reduces an organization’s productivity.
WSO2 API Manager addresses the issue of API automation by providing a platform-agnostic, developer-centric solution. WSO2 API Controller, apictl tool plays a key role in the automation pipeline
For this demonstration the following architecture will be used.
- Deploy the backend server
- Install the Jenkins server
- Install WSO2 API Manager
Sample user service is used as the backend server and the complete source code of that server can be found here. Note that this is a Spring Boot application and the following commands can be used to start or build the application
- Start the application :
mvn spring-boot:run
- Build the application :
mvn clean install
Note that the jar is already built and stored here and that can be
simply started by executing java -jar target/backend-server-1.0.0.jar
All the available services will be shown in the Swagger UI.
Swagger UI URL: http://localhost:8595/swagger-ui.html
Installing jenkins server is straight forward. Please refer to their official documentation for this.
Installing WSO2 API Manager is straight forward and please refer to this documentation for more information on that. For this demonstration I'll be having two environments called Dev and Test but any number of environments can be there and the steps are pretty much the same.
-
For this demo, I have created an API called UserAPI and the API project can be found here
-
Once this project is cloned, this can be deployed on your development API Manager server by simply running the apictl tool. The CTL tool can be downloaded and installed by following this documentation.
-
Execute the below commands to deploy this API on your development server. If your devlopment server is not on the local machine, please change the URLs accordingly.
- Add the dev environment :
apictl add-env -e dev --apim https://localhost:9443 --token https://localhost:8243/token
- Login to the dev environment :
apictl login dev -u admin -p admin -k
- Import the UserAPI to the dev environment :
apictl import-api -f APIM_CICD/apim_artifacts/UserAPI e dev -k --preserve-provider --update —verbose
- Add the dev environment :
-
Now you can log into the dev environment and do any kind of modifications to the UserAPI.
-
If you want to export the API from the dev environment, the following commands can be used
- Add the dev environment :
apictl add-env -e dev --apim https://localhost:9443 --token https://localhost:8243/token
- Login to the dev environment :
apictl login dev -u admin -p admin -k
- Export the UserAPI from the dev environment :
apictl export-api -n UserAPI -v v1 -e dev -k
- Add the dev environment :
- Login to the jenkins server. You might have to create a user if this is the first time.
- Install npm and nodejs plugins and Newman globally in your Jenkins server. Newman is used to run the tests I have created using PostMan. The test script can be found here.
- Download and configure WSO2 API Controller to the Jenkins server and the developer machines.
Note that you need to add the environments properly to the API Controller in the Jenkins Server. You can add environments
by either manually editing the
<USER_HOME>/.wso2apictl/main_config.yaml
file or by running the following CTL command. Don't forget that usually jenkins create a new user called jenkins and the environment should be added to that user or else jenkins server won't notice this. The same commands mentioned in the above topic can be used for this. - Now we need to create a GitHub repository and later we are planning to push apim_artifacts folder to that repo.
- Once the repo is created, let's configure the webhook. This is how the jenkins server will get notified when ever there is a new commit. . Note that the url has to be passed here and if the Jenkins is deployed locally, then you can use ngrol to expose your localhost through internet.
- Let's commit the apim_artifacts to this newly created repo.
- Store the GitHub and APIM user credentials on Jenkins. Note that this can be done more securely using GitHub Apps.
- Add the Jenkins pipeline configuration defined in here. Don't forget to change the GitHub url accordingly.