Install Azure functions core tools using
npm i -g azure-functions-core-tools@core
Install Java Extension Pack for Visual Studio Code.
Create a new storage account by executing (The UniqueID
is just used to make sure your account name doesn't conflict with others, for example you can use the "current date & time", or "your alias + sequence number"; make sure the total length of the names are not exceeding 24 characters, and make sure you only use lower-case letters or numbers):
az group create -n BoothAzFunc<UniqueID> -l westus
az storage account create -n boothazfunc<UniqueID> -g BoothAzFunc<UniqueID> -l westus --sku Standard_LRS
Finally to get the storage connection string which will be used in the future:
az storage account show-connection-string -n boothazfunc<UniqueID> -g BoothAzFunc<UniqueID>
Copy it (Starting with DefaultEndpointsProtocol...
) to somewhere.
Go to your working directory and clone the sample code repository from:
git clone https://github.com/Microsoft/function-demo-java-on-azure.git
cd function-demo-java-on-azure
And make sure you replace the $UniqueId$
in walkthrough/pom.xml
with the actual string of your unique ID. And you also need to replace $ResourceGroupName$
in walkthrough/pom.xml
with the actual string of your resource group name (which should be BoothAzFunc<UniqueID>
).
Then open walkthrough/local.settings.json
and paste the connection string of your storage account to the value of AzureWebJobsStorage
.
Enter the folder walkthrough
, and execute:
mvn clean package
Then you may run it by:
mvn azure-functions:run
The timer and the queue functions will be triggered once per 30 seconds. To trigger the HTTP function, you need to execute the following command in a new command line window:
curl -X POST -d "World" http://localhost:7071/api/hello
To terminate the app, press Ctrl + C
.
Try the following command to deploy the function app, and that's it.
mvn azure-functions:deploy
And you will find your functions app named walkthrough-<UniqueID>
under Java Demos
subscription.
You will also be able to find the URL of the deployed app within the last several lines of the command line output. So it is possible to verify that it is actually running on Azure.
curl -X POST -d "Azure World" <Deployed Host URL>/api/hello
Get to the built-in terminal by pressing the Ctrl + Backtick
.
Enter the folder walkthrough
in the built-in terminal, and execute:
mvn clean package
Then you may run it by:
mvn azure-functions:run
The timer and the queue functions will be triggered once per 30 seconds. To trigger the HTTP function, you need to execute the following command in a new command line window:
curl -X POST -d "World" http://localhost:7071/api/hello
To terminate the app, press Ctrl + C
.
Try the following command to deploy the function app, and that's it.
mvn azure-functions:deploy
And you will find your functions app named walkthrough-<UniqueID>
under Java Demos
subscription.
You will also be able to find the URL of the deployed app within the last several lines of the command line output. So it is possible to verify that it is actually running on Azure.
curl -X POST -d "Azure World" <Deployed Host URL>/api/hello
Get to the built-in terminal by pressing the Ctrl + Backtick
.
Enter the folder walkthrough
in the built-in terminal, and execute:
mvn clean package
Navigate to the Debug page of Visual Studio Code by pressing Ctrl + Shift + D
; then click the little gear icon (Open launch.json) besides "No Configurations" dropdown; and select "Java".
In the newly created launch.json
, locate the Debug (Attach)
block, and set the port
to be 5005
.
Now you run the functions app by:
mvn azure-functions:run
Wait for the functions host to start, and select the Debug (Attach)
item in the dropdown list, and click the little green run icon (Start debugging) beside that dropdown list.
Now you can set the breakpoints in:
Those breakpoints will be hit when the corresponding function is triggered. And when any of them is hit, hover the mouse to some variables, like executionContext
or timerInfo
, to demostrate that the user could inspect all the execution environment here. And press F5
to continue running.
- Install the Azure Function Plugin for Jenkins. You can install/update the plugin in Jenkins Update Center (Manage Jenkins -> Manage Plugins, search Azure Function Plugin).
- Create an Azure Service Principal through Azure CLI or Azure Portal.
- Open Jenkins dashboard, go to Credentials, add a new Microsoft Azure Service Principal with the credential information you just created.
You can deploy your Azure Function using a classic freestyle project:
- Create a new freestyle project in Jenkins, add necessary build steps to build your code. For example, if you use maven commands above you should get following directory structure after build:
target/azure-functions
-- walkthrough-prototype
|-- Queue
|-- function.json
|-- Timer
|-- function.json
|-- hello
|-- function.json
|-- host.json
|-- walkthrough-1.0-SNAPSHOT.jar
- Add a post-build action 'Publish an Azure Function'.
- Select your Azure credential in Azure Profile Configuration section.
- In App Configuration section, choose the resource group and function app in your subscription.
- Fill in 'Files' field with
**/*.jar,**/*.json
to select files we want to deploy. - Fill in 'Source Directory' field with
target/azure-functions
. - Save the project and build it, your function app will be deployed to Azure when build is completed.
You can also use this plugin in pipeline (Jenkinsfile). Here are some samples to use the plugin in pipeline script:
To deploy a Java function app:
azureFunctionAppPublish azureCredentialsId: '<credential_id>',
resourceGroup: '<resource_group_name>', appName: '<app_name>',
filePath: '**/*.jar,**/*.json', sourceDirectory: 'target/azure-functions'
For advanced options, you can use Jenkins Pipeline Syntax tool to generate a sample script.