Automate Flex Gateway in Connected Mode via Gitlab Pipeline

Rohit Singh
Another Integration Blog
5 min readOct 30, 2023

In this blog, we’re covering a step-by-step guide on how to automate the process of publishing an asset to the exchange and creating an API instance for Flex Gateway using GitLab CI/CD. By automating these steps, you can greatly improve your efficiency and reduce the chance of errors in the publishing process.

Prerequisites

  • Anypoint Platform with Administrator Access
  • Gitlab Access
  • Flex Gateway created on Anypoint Platform

1. Create Gitlab Repository:

We need to create a Gitlab Repository for our Project. Here we’re giving the repository name Blog-Automation. Add a raml or oas file whichever you want to publish over exchange. In our case it’s a raml file.

2. Add Gitlab CI file:

Now add a “.gitlab-ci.yml” file to the root directory of the repository. At this stage, it’s an empty file.

After adding this file, our repository should look like this:

3. Create Connected App:

We should have a connected app and use the App acts on its behalf (client credentials) type and include the following scopes.

4. Configure the Gitlab Pipeline:

In this example, we’ve defined one stage — dev.

stages:
— dev
  • Let’s start configuring the dev stage. We’re first installing the curl here so that we can call the anypoint api from our gitlab pipeline using the command: apt-get install curl. Next command: apt-get update && apt-get install -yqq jq will update the list of newest versions of packages and its dependencies for Ubuntu.
dev:
stage: dev
script:
— apt-get install curl
— apt-get update && apt-get install -yqq jq
  • Now inside the script parameter, we’ll add more steps to accomplish the below:
  • Extract AuthToken
  • Publish Asset to Exchange
  • Create an API Instance
  • Deploy API Instance
  • Create repository variables for connectedAppClientId and connectedAppClientSecret by going to Repository Settings>CICD>Variables.
  • To extract the Bearer Authentication Token to login into Anypoint Platform, add below script. Here we’re using the clientId & clientSecret Repo variables that we created in the last step.
  • We are creating here a variable authToken that will have the token to login Anypoint Platform. Later we’re printing the extracted auth Token using echo ${authToken}
- echo Get Bearer Auth Token to login into Anypoint Platform
- |
export authToken=$(curl -d “client_id=$connectedAppClientId&client_secret=$connectedAppClientSecret&grant_type=$grantType” ‘https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token' | jq -r ‘.access_token’)
- echo ${authToken}
  • Now we’ve the authentication token, the next step is to publish the asset to exchange. Add variables in the repository for assetId(as you wish) , organizationId(Anypoint Platform), groupId(Anypoint Platform), AssetVersion(1.0.0), apiTitle(name of asset you want to provide, in our case it’s “Demo Automation”), classifier(raml), apiVersion(v1). Like this:
  • Creating a variable assetFileName in the script which will store the file name with .raml extension. As we know the AssetId should always be unique, so we’re creating a variable which is appending Job Id to assetId.
- export assetFileName=$(ls *.raml)
- export uniqueAssetId=${assetId}${CI_JOB_ID}
- echo Publish Asset to exchange
- |
curl — location — request POST https://anypoint.mulesoft.com/exchange/api/v1/assets — header “Authorization:Bearer ${authToken}” — form “organizationId=${organizationId}” — form “groupId=${groupId}” — form “assetId=${uniqueAssetId}” — form “version=${AssetVersion}” — form “name=${apiTitle}” — form “classifier=${classifier}” — form “apiVersion=${apiVersion}” — form “main=${assetFileName}” — form “asset=@${assetFileName}”
  • After publishing the asset to exchange, now we need to create the API Instance to the API Manager. For this add these variables to the repository: deploymentType(HY), proxyUri(flex gateway url — http://0.0.0.0:8081/ ), type(rest), implementationUri(url of implementation api), technology(flexGateway). We’ll not create variables for organizationId, envrionmentId etc, at this step as we already have these variables created from our last step.
- echo Add API INSTANCE
- |
export apiInstanceId=$(curl — location https://anypoint.mulesoft.com/apimanager/api/v1/organizations/${organizationId}/environments/${environmentId}/apis — header ‘Content-Type: application/json’ — header “Authorization:Bearer ${authToken}” — data ‘{
“endpoint”:{
“deploymentType”:”’${deploymentType}’”,
“isCloudHub”:null,
“proxyUri”:”’${proxyUri}’”,
“type”:”’${type}’”,
“uri”:”’${implementationUri}’”
},
“technology”:”’${technology}’”,
“spec”:{
“assetId”:”’${uniqueAssetId}’”,
“groupId”:”’${groupId}’”,
“version”:”’${AssetVersion}’”
}
}’ | jq -r ‘.id’)
  • Last step would be to deploy our API Instance. For the below script, we need to add a few variables to the repository: gatewayVersion(flex gateway version — 1.4.4), targetId(your flex gateway targetId), targetName(your flex gateway name), targetType(server).
- echo Deploy API INSTANCE
- |
curl — location https://anypoint.mulesoft.com/proxies/xapi/v1/organizations/${organizationId}/environments/${environmentId}/apis/$apiInstanceId/deployments — header ‘Content-Type: application/json’ — header “Authorization:Bearer ${authToken}” — data ‘{
“gatewayVersion”:”’${gatewayVersion}’”,
“targetId”:”’${targetId}’”,
“targetName”:”’${targetName}’”,
“targetType”:”’${targetType}’”,
“type”:”’${deploymentType}’”,
“environmentId”:”’${environmentId}’”,
“environmentName”:”’${environmentName}’”
}’
  • After everything, now .gitlab-ci.yml file should look like below:
image: maven:3.6.3-jdk-8
stages:
— dev
dev:
stage: dev
script:
— apt-get install curl
— apt-get update && apt-get install -yqq jq

— echo Get Bearer Auth Token to login into Anypoint Platform
— |
export authToken=$(curl -d “client_id=$connectedAppClientId&client_secret=$connectedAppClientSecret&grant_type=$grantType” ‘https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token' | jq -r ‘.access_token’)
— echo ${authToken}


— export assetFileName=$(ls *.raml)
— export uniqueAssetId=${assetId}${CI_JOB_ID}
— echo Publish Asset to exchange
— |
curl — location — request POST https://anypoint.mulesoft.com/exchange/api/v1/assets — header “Authorization:Bearer ${authToken}” — form “organizationId=${organizationId}” — form “groupId=${groupId}” — form “assetId=${uniqueAssetId}” — form “version=${AssetVersion}” — form “name=${apiTitle}” — form “classifier=${classifier}” — form “apiVersion=${apiVersion}” — form “main=${assetFileName}” — form “asset=@${assetFileName}”
— echo Add API INSTANCE
— |
export apiInstanceId=$(curl — location https://anypoint.mulesoft.com/apimanager/api/v1/organizations/${organizationId}/environments/${environmentId}/apis — header ‘Content-Type: application/json’ — header “Authorization:Bearer ${authToken}” — data ‘{
“endpoint”:{
“deploymentType”:”’${deploymentType}’”,
“isCloudHub”:null,
“proxyUri”:”’${proxyUri}’”,
“type”:”’${type}’”,
“uri”:”’${implementationUri}’”
},
“technology”:”’${technology}’”,
“spec”:{
“assetId”:”’${uniqueAssetId}’”,
“groupId”:”’${groupId}’”,
“version”:”’${AssetVersion}’”
}
}’ | jq -r ‘.id’)

— echo Deploy API INSTANCE
— |
curl — location https://anypoint.mulesoft.com/proxies/xapi/v1/organizations/${organizationId}/environments/${environmentId}/apis/$apiInstanceId/deployments — header ‘Content-Type: application/json’ — header “Authorization:Bearer ${authToken}” — data ‘{
“gatewayVersion”:”’${gatewayVersion}’”,
“targetId”:”’${targetId}’”,
“targetName”:”’${targetName}’”,
“targetType”:”’${targetType}’”,
“type”:”’${deploymentType}’”,
“environmentId”:”’${environmentId}’”,
“environmentName”:”’${environmentName}’”
}’
when: always

5. After Successful Pipeline Run:

  • The Asset is published in the Exchange.
  • API Instance is also created on API Manager.

Hence, Gitlab Pipeline is a continuous integration and continuous delivery (CI/CD) platform that can be used to automate the workflow for Flex Gateway in Connected Mode. With Gitlab Pipeline, you can create a pipeline that will automatically run a Flex Gateway and use features of the API Manager API, such as creating and deploying API instances. But keep in mind that unavailability of third party/open source dependencies such as yq, jq and nodejs installations may lead to failures.

--

--