Tutorial: Deploy to Azure Functions using Jenkins
Important
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.
Prerequisites
- 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:
Create a resource group, replacing the <resource_group> placeholder with your resource group name.
az group create --name <resource_group> --location eastus
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> --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:
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 Az CLI, version 2.0.67 or higher.
Install maven using the following command:
sudo apt install -y maven
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
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.
Make sure the Credentials plug-in is installed.
From the menu, select Manage Jenkins.
Under System Configuration, select Manage plug-in.
Select the Installed tab.
In the filter field, enter
credentials
.Verify that the Credentials plug-in is installed. If not, you'll need to install it from the Available tab.
From the menu, select Manage Jenkins.
Under Security, select Manage Credentials.
Under Credentials, select (global).
From the menu, select Add Credentials.
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
.
Select OK.
Fork the sample GitHub repo
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 this section, you create the Jenkins Pipeline.
In the Jenkins dashboard, create a Pipeline.
Enable Prepare an environment for the run.
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 { withEnv(['AZURE_SUBSCRIPTION_ID=99999999-9999-9999-9999-999999999999', 'AZURE_TENANT_ID=99999999-9999-9999-9999-999999999999']) { stage('Init') { cleanWs() 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.
First, obtain the authorization key via the instructions in the Azure Functions HTTP triggers and bindings article.
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>