Author- Rajat Malik (Cloud Engineer)
Azure Functions is a serverless compute service. Using Azure Functions, you can run code on-demand without provisioning or managing infrastructure. This tutorial shows how to deploy a Java function to Azure Functions using the Azure Functions plug-in.
Prerequisites
- Azure subscription: If you don’t have an Azure subscription, create a free account before you begin.
- Jenkins server
Source Code
The source code used for this tutorial is in this GitHub repo.
Creating A Java Function
To create a Java function with the Java runtime stack, use either the Azure portal or the Azure CLI.
The following steps show how to create a Java function using the Azure CLI:
- Create a resource group, replacing the <resource_group> placeholder with your resource group name.
az group create –name <resource_group> –location eastus
(Azure CLI) - Create an Azure storage account, replacing the placeholders with the appropriate values.
az storage account create –name <storage_account> –location eastus –resource-group <resource_group> –sku Standard_LRS
- Create the test function app, replacing the placeholders with the appropriate values
az functionapp create –resource-group <resource_group> –consumption-plan-location eastus –name <function_app> –storage-account <storage_account>
Prepare Jenkins Server
The following steps explain how to prepare the Jenkins server:
- Deploy a Jenkins server on Azure. If you don’t already have an instance of the Jenkins server installed, the article, create a Jenkins server on Azure guides you through the process.
- Sign in to the Jenkins instance with SSH.
- On the Jenkins instance, install maven using the following command:
sudo apt install -y maven
(Terminal) - On the Jenkins instance, install the Azure Functions Core Tools by issuing the following commands at a terminal prompt:
wget -q <a href="https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb">https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb</a>
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install azure-functions-core-tools - In the Jenkins dashboard, install the following plugins:
-
- Azure Functions Plug-in
- EnvInject Plug-in
- Jenkins needs an Azure service principal to authenticate and access Azure resources. Refer to the Deploy to Azure App Service for step-by-step instructions.
- Using the Azure service principal, add a “Microsoft Azure Service Principal” credential type in Jenkins.
Fork the sample GitHub repo
- Sign in to the GitHub repo for the odd or even sample app.
- In the upper-right corner in GitHub, choose Fork.
- Follow the prompts to select your GitHub account and finish forking.
Create a Jenkins Pipeline
- In the Jenkins dashboard, create a Pipeline.
- Enable Prepare an environment for the run.
- Add the following environment variables in Properties Content, replacing the placeholders with the appropriate values for your environment:
<ol>
<li>AZURE_CRED_ID=<service_principal_credential_id>
RES_GROUP=<resource_group>
FUNCTION_NAME=<function_name></li>
</ol>
<ol>
<li></li>
</ol>
- In the Pipeline->Definition section, select Pipeline script from SCM.
- Enter your GitHub fork’s URL and script path (“doc/resources/jenkins/JenkinsFile”) to use in the JenkinsFile example.:
node {
stage('Init') {
checkout scm
}stage('Build') {
sh 'mvn clean package'
}stage('Publish') {azureFunctionAppPublish appName: env.FUNCTION_NAME,azureCredentialsId: env.AZURE_CRED_ID,filePath: '**/*.json,**/*.jar,bin/*,HttpTrigger-Java/*',resourceGroup: env.RES_GROUP,sourceDirectory: 'target/azure-functions/odd-or-even-function-sample'}}
Build and deploy
It’s now time to run the Jenkins job.
- First, obtain the authorization key via the instructions in the Azure Functions HTTP triggers and bindings
- In your browser, enter the app’s URL. Replace the placeholders with the appropriate values and specify a numeric value for<input_number> as input for the Java function.
https://<function_app>.azurewebsites.net/api/HttpTrigger-Java?code=<authorization_key>&number=<input_number>
- You’ll see results similar to the following example output (where an odd number – 365 – was used as a test):
The number 365 is Odd.
Clean up resources
If you’re not going to continue to use this application, delete the resources you created with the following step:
az group delete -y –no-wait -n <resource_group>