Tutorial: Deploy to Azure Functions using Jenkins

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.


Create 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:

  1. Create a resource group, replacing the <resource_group> placeholder with your resource group name.

    az group create --name <resource_group> --location eastus
  2. 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    
  3. 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:

  1. 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.

  2. Sign in to the Jenkins instance with SSH.

  3. On the Jenkins instance, install maven using the following command:

    sudo apt install -y maven
  4. On the Jenkins instance, install the Azure Functions Core Tools by issuing the following commands at a terminal prompt:

    wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
    sudo dpkg -i packages-microsoft-prod.deb
    sudo apt-get update
    sudo apt-get install azure-functions-core-tools
  5. In the Jenkins dashboard, install the following plugins:

    • Azure Functions Plug-in
    • EnvInject Plug-in
  6. 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.

  7. Using the Azure service principal, add a "Microsoft Azure Service Principal" credential type in Jenkins. Refer to the Deploy to Azure App Service tutorial.

Fork the sample GitHub repo

  1. Sign in to the GitHub repo for the odd or even sample app.

  2. In the upper-right corner in GitHub, choose Fork.

  3. Follow the prompts to select your GitHub account and finish forking.

Create a Jenkins Pipeline

In this section, you create the Jenkins Pipeline.

  1. In the Jenkins dashboard, create a Pipeline.

  2. Enable Prepare an environment for the run.

  3. Add the following environment variables in Properties Content, replacing the placeholders with the appropriate values for your environment:

  4. In the Pipeline->Definition section, select Pipeline script from SCM.

  5. 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.

  1. First, obtain the authorization key via the instructions in the Azure Functions HTTP triggers and bindings article.

  2. 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.

  3. 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>

Next steps