Tutorial: Deploy to Azure Functions using Jenkins


Many Azure services have Jenkins plug-ins. Some of these plug-ins will be out of support as of February 29, 2024. Azure CLI is the currently recommended way to integrate Jenkins with Azure services. For more information, refer to the article Jenkins plug-ins for Azure.

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.


  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Jenkins server: If you don't have a Jenkins server installed, refer to the article, Create a Jenkins server on Azure.

View the source code

The source code used for this tutorial is located in the Visual Studio China GitHub repo.

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> --runtime java --consumption-plan-location eastus --name <function_app> --storage-account <storage_account> --functions-version 2

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 Az CLI,  version 2.0.67 or higher.

  4. Install maven using the following command:

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

    curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
    sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
    sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -cs)-prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list'
    cat /etc/apt/sources.list.d/dotnetdev.list
    sudo apt-get update
    sudo apt-get install azure-functions-core-tools-3
  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. Make sure the Credentials plug-in is installed.

    1. From the menu, select Manage Jenkins.

    2. Under System Configuration, select Manage plug-in.

    3. Select the Installed tab.

    4. In the filter field, enter credentials.

    5. Verify that the Credentials plug-in is installed. If not, you'll need to install it from the Available tab.

    The Credentials Plug-in needs to be installed.

  8. From the menu, select Manage Jenkins.

  9. Under Security, select Manage Credentials.

  10. Under Credentials, select (global).

  11. From the menu, select Add Credentials.

  12. Enter the following values for your Microsoft Azure service principal:

    • Kind: Select the value: Username with password.
    • Username: Specify the appId of the service principal created.
    • Password: Specify the password (secret) of the service principal.
    • ID: Specify the credential identifier, such as azuresp.
  13. Select OK.

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. In the Pipeline->Definition section, select Pipeline script from SCM.

  4. Enter your GitHub fork's URL and script path ("doc/resources/jenkins/JenkinsFile") to use in the JenkinsFile example.

     node {
             'AZURE_TENANT_ID=99999999-9999-9999-9999-999999999999']) {
         stage('Init') {
             checkout scm
         stage('Build') {
             sh 'mvn clean package'
         stage('Publish') {
             def RESOURCE_GROUP = '<resource_group>' 
             def FUNC_NAME = '<function_app>'
             // login Azure
             withCredentials([usernamePassword(credentialsId: 'azuresp', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
             sh '''
                 az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
                 az account set -s $AZURE_SUBSCRIPTION_ID
             sh 'cd $PWD/target/azure-functions/odd-or-even-function-sample && zip -r ../../../archive.zip ./* && cd -'
             sh "az functionapp deployment source config-zip -g $RESOURCE_GROUP -n $FUNC_NAME --src archive.zip"
             sh 'az logout'

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