Authors: Anjali Kushwaha & Rohit Singh
This blog post describes how to use GitLab CI/CD to automate the creation of an API instance for Flex Gateway and the process of publishing an asset to the exchange. By automating these activities, you can significantly increase your productivity and reduce the possibility of publishing-related mistakes.
Prerequisites
- Anypoint Platform with Administrator Access
- GitLab Access
- Flex Gateway created on Anypoint Platform
1. Create GitLab Repository.
For our project, we need to set up a GitLab repository. We have chosen ‘Blog-Automation’ as the name of the repository. To publish any file type through Exchange, add a RAML or OAS file. In this case, it’s a RAML file.
2. Add GitLab CI file.
Now include a “.gitlab-ci.yml” file in the repository’s root directory. The file is currently blank.
After adding this file, our repository should now look as follows:

3. Create a Connected App.
We should have a connected app using the ‘App acts on its behalf’ (client credentials) type, and it should provide the following scopes.

4. Configure the GitLab Pipeline.
In this example, only the stage ‘dev’ has been defined.
stages:
- dev
- Let’s start configuring the ‘dev’ stage. To use the Anypoint API from our GitLab pipeline, we must first install curl using the command: apt-get install curl.’ The next command: ‘apt-get update && apt-get install -yqq jq will update the list of the newest versions of packages and their dependencies for Ubuntu.
dev:
stage: dev
script:
- apt-get install curl
- apt-get update && apt-get install -yqq jq
We’ll now include more steps inside the script parameter to complete the following:
- Extract AuthTokenPublish Asset to ExchangeCreate an API Instance.
- Deploy API Instance.
- By navigating to Repository Settings>CICD>Variables, create repository variables for connectedAppClientId and connectedAppClientSecret.


- To extract the Bearer Authentication Token to log in to the Anypoint Platform, add the below script. The clientId and clientSecret Repo variables that we created in the previous step are used here.
- Here, we’re creating a variable called ‘authToken’ that will contain the Anypoint Platform login token. Later, we’re printing the extracted auth Token using ‘echo ${authToken}’.
- echo Get a Bearer Auth Token to access the 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 that we have the authentication token, the next step is to publish the asset to the exchange. Add variables for assetId (as required), organizationId (Anypoint Platform), groupId (Anypoint Platform), AssetVersion (1.0.0), apiTitle (the name of the asset you want to provide, in our instance it’s “Demo Automation”), classifier (raml), and apiVersion (v1) to the repository. Like this:

- In the script, we are creating a variable ‘assetFileName’ that will store the file name with the .raml extension. Additionally, we are creating a variable that appends the Job Id to assetId because we know that the AssetId must always be unique.
- 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}"
- We now need to create the API Instance in the API Manager after publishing the asset to the exchange. For this, add the following repository variables: technology (flexGateway), deploymentType (HY), proxyUri (flex gateway URL – http://0.0.0.0:8081/), type (rest), and implementationUri (URL of implementation API). As these variables have already been formed in our previous phase, we won’t establish variables for organizationId, environmentId, etc. at this stage.
- 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')
- The last step would be to deploy our API Instance. For the script below, we need to add the following variables to the repository: gatewayVersion (flex gateway version – 1.4.4), targetId (your flex gateway targetId), targetName (your flex gateway name), and 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 making all the necessary changes, the .gitlab-ci.yml file should now look like the following:
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 a Bearer Auth Token to access the 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. Successful Pipeline Run.
- The Asset is published in the Exchange.

- API Instance is also created on API Manager.

