Gitlab CI/CD Pipeline to Deploy APIs to WSO2 API Manager

Use APICTL with Gitlab CI/CD

Mahesh Chinthaka
5 min readAug 18, 2021

CICD has become a widely adopted practice now a days. Automating both infrastructure deployments and artefact deployments has been a main focus point in almost all the projects in the industry these days. There are so many automation tools out there. But in this article I will use a Gitlab CICD Pipeline.

In this article Im going to explain how we can automate API deployment for a WSO2 API Manager. WSO2 API Manager provides a very sophisticated tool called APICTL to perform all API deployment related activities. That is a command line tool, I will be integrating APICTL with a Gitlab CICD pipeline to achieve this.

In this example , I will have one API inside one Gitlab Project with multiple branches like, main, develop, test etc. Whenever a new code is pushed to a certain branch, the pipeline will be executed automatically and will deploy the updated API to respective environment.(eg: Develop branch will deploy APIs in to Dev environment)

To read more about the APICTL tool please read below WSO2 Oficial Documentation

To read more about Automating API deployment via APICTL please read below WSO2 Official Documentation

Prerequisites

Verified Gitlab Account

Option #01

You will have to provide credit card details to get your account verified, but they dont charge anything. This purely for verification purposes. They dont even store the card details (As they claim :) )

Verified Gitlab Account is offered with 400 CICD minutes per month at zero cost.

Option #2

Another option is to register your own runner and let the CICD pipelines to run in your own runner.

Gitlab Project

This is straight forward, you just have to go to your Gitlab account → Create New Project → Create Blank Project

Create Blank Project in Gitlab

Step 01 : Add API Artefacts to the repository

Here you have two approaches,

  1. Create the API from Publisher portal and download the API
  2. Use APICTL command to generate API artifacts.

I will be using approach #2.

Step 1.1 — Clone the project and create branches

You can create branches from the Gitlab UI as well. But I prefer this way

# clone gitlab project to local machine
git clone https://gitlab.com/mcvidanagama/swagger-petstore-api.git
# go inside cloned repo
cd swagger-petstore-api
# create new branches for develop and test
git checkout -b develop
git push --set-upstream origin develop
git checkout -b test
git push --set-upstream origin test
#switch back to develop branch
git checkout develop

Step 1.2 — Generate API project using APICTL command

Please read below doc for more information

Option #01 — Initialize the project using a Swagger/OpenAPI specification

apictl init Petstore --oas https://petstore.swagger.io/v2/swagger.json

Option #02 — Initialize from the scratch

apictl init SampleAPI --definition sample-api.yaml --force=true

Your repository would have below file structure. Please Commit and push your code.

.
├── SwaggerPetstore
│ ├── Client-certificates
│ ├── Definitions
│ │ └── swagger.yaml
│ ├── Docs
│ ├── Image
│ ├── Interceptors
│ ├── README.md
│ ├── Sequences
│ │ ├── fault-sequence
│ │ ├── in-sequence
│ │ └── out-sequence
│ ├── api.yaml
│ ├── api_meta.yaml
│ ├── deployment_environments.yaml
│ └── libs
└── README.md

Step 02 — Create Gitlab Pipeline

Go to your Gitlab Repository → CICD →Pipelines

Select Bash Template

Select your branch , add following content and commit the code. Please note that this .gitlab-ci.yml should be available in each branch that you want the pipeline to be executed.


# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Bash.gitlab-ci.yml

# See https://docs.gitlab.com/ee/ci/yaml/index.html for all available options

# you can delete this line if you're not using Docker
image: busybox:latest

stages:
- deploy

before_script:
- |
echo "Before script section"
pwd
wget https://apim.docs.wso2.com/en/4.0.0/assets/attachments/learn/api-controller/apictl-4.0.0-linux-i586.tar.gz
tar -xvf apictl-4.0.0-linux-i586.tar.gz
ls
export PATH=$PATH:/builds/mcvidanagama/wso2apim-apictl-demo/apictl
apictl version

after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"

deploy_develop:
stage: deploy
script:
- |
apictl add env dev --apim $DEV_APIM_URL
apictl login dev -u $DEV_ADMIN_USERNAME -p $DEV_ADMIN_PASSWORD -k
apictl import api -f SwaggerPetstore -e dev --params=./SwaggerPetstore/params/dev.yaml --update --rotate-revision --verbose -k
rules:
- if: $CI_COMMIT_BRANCH == "develop"

Step 03 — Prepare Environment specific params

Create yaml files for each of your environments under SwaggerPetstore/params/ directory.

I will create a dev.yaml and will add dev environment specific params there.

environments:
- name: dev
configs:
endpoints:
production:
url: 'https://petstore.swagger.io/vdev'
policies:
- Gold
- Silver
- Unlimited
- name: test
configs:
endpoints:
production:
url: 'https://petstore.swagger.io/v2'
sandbox:
url: 'https://petstore.swagger.io/v2'
- name: production
configs:
endpoints:
production:
url: 'https://petstore.swagger.io/v2'

Step 04 — Create Variables in Gitlab

If you look at the cicd pipeline, you may see that we have used parameters like $DEV_APIM_URL, $DEV_ADMIN_USERNAME, $DEV_ADMIN_PASSWORD etc. These are retrieved from GitLab CICD Variables.

Go to your repository → Settings → CICD → Variables → Add Variable

All set! Push a change to your repository and see the pipeline running and API getting deployed to the environment!!

Cheers!!!

--

--