I was playing with our new AMPLIFY API Builder Standalone release and decided to cook up an automation recipe.
Here are the ingredients – make sure you have these handy before getting started:
- Axway API Builder 4.x, get it here
- Azure account, you can sign up a free trial
- A Visual Studio Team Services (VSTS) account, it’s free
- Optional – Azure CLI 2.x
Here is what we are doing:
Install API Builder and Create a Project
Follow the steps documented here. Dockerfile is created by default when you setup a new project. There will be some changes required in first build step if you are going to use environment variables.
Login to Azure account
- Create a Resource Group: A resource group is a unit of Azure Resource management. We are going to create all resources under this group.
- Create an Azure Container Registry: An Azure Container Registry is your private docker registry in Azure. Alternatively, you can use your own Docker registry. We are going to push Docker images to this registry and use the images to deploy API Builder app.
Take a note of ‘Login server‘ and ‘Access keys‘, we will need this to access registry using Azure CLI while pushing and pulling the container images
Login to VSTS
VSTS is the central piece of this architecture. It will host our remote code repository, build and release definitions, work items, Azure subscriptions and authorizations, etc. VSTS build agents are going to make our task very easy in building and pushing out containers to Azure. So, a few things to setup here:
- Create a new project and select ‘Git’ as source code
- Setup a Build Definition: Choose ‘Docker container‘ template from the list. We are going to use this pre-defined build template to build container image and push the image to container registry.
This build template has 2 tasks, and we need to define configuration for each of the tasks. We are going to use Hosted Linux agent to execute each task. Hosted Linux agent is currently in preview mode. You will get 2 free hosted agents when you sign-up for a VSTS account. Alternatively, you can use your own build agent.
- Azure Subscription and Authorization: After adding your Azure Subscription info, you will have to Authorize it. VSTS creates service endpoints and obtains authorization token (OAuth) from Azure to execute Azure Resource Manager tasks. If you want to know more, please read about ARM, service principle and service endpoints.
- Build an Image task: Define the path where the agent should locate the dockerfile in your source code. Action step is selected by default – “Build an image”. When this step is executed, build agent will locate dockerfile in source code repository and create container image.
- Push an image: We will have to specify the Azure container registry and build number scheme if any. This step will use the container image created in the first step and push it to the Azure Container registry. At the end of this step, we will have our API Builder app image pushed in Azure.
- Deploy API Builder app: We will need to add a new task in this pipeline to be able to deploy the container. We are going to choose Azure CLI task for this step.
At this step, we have 3 options for deployment and this is where it becomes interesting.
Our customers may have a complete container orchestration solution using Azure Kubernetes Service (AKS) or docker hosts. Some might opt for immutable container infrastructure by creating new Container instances with each deployment and retire the old ones.
Personally, I love Azure Container Instance (CI) service. It is the fastest way to deploy apps in containers without setting up complete orchestration platform. We are going to deploy API Builder app in Azure CI. Here is the inline script –
az container create --resource-group <name of Azure resource Group> --name <container name> --image <login server>/<container image name>:$(Build.BuildId) --cpu 1 --memory 1 --registry-login-server <login server> --registry-username <username> --registry-password <password from access keys> --dns-name-label <give a name for access> --ports <all port numbers separated by space>
- Enable Continuous Integration
That’s all we have to setup in VSTS!
Back to API Builder
Now we are back to our API Builder. We are going to push our API Builder app code to local and remote repository
- Commit changes to local git repo
git commit -m "<tag>"
- Add VSTS remote Git repository
git remote add origin <VSTS project Git Url, get it from your VSTS project>
- Push the changes to remote repository
git push -u origin --all
The above steps should trigger the build definition we created in VSTS, and if everything goes well, you will see your API Builder app running in container.