Build and deploy Java to Azure Functions
You can use Azure Functions to run small pieces of code in the cloud without the overhead of running a server. In this step-by-step guide you'll learn how to create a pipeline that continuously builds and deploys a your Java function app. Your team can then automatically build each commit in GitHub, and if you want, automatically deploy the change to Azure Functions.
A GitHub account, where you can create a repository. If you don't have one, you can create one for free.
An Azure DevOps organization. If you don't have one, you can create one for free. (An Azure DevOps organization is different from your GitHub organization. Give them the same name if you want alignment between them.)
If your team already has one, then make sure you're an administrator of the Azure DevOps project that you want to use.
An Azure account. If you don't have one, you can create one for free.
If you're new at this, the easiest way to get started is to use the same email address as the owner of both the Azure Pipelines organization and the Azure subscription.
Get the code
If you already have an app in GitHub that you want to deploy, you can create a pipeline for that code.
If you are a new user, fork this repo in GitHub:
Create an Azure Functions app
Create an Azure App Service on Linux. Select the runtime you want to use.
# Create a resource group az group create --location westus --name myapp-rg # Create a storage account az storage account create --name mystorage --location westeurope --resource-group myapp-rg --sku Standard_LRS # Create an Azure Functions app az functionapp create --resource-group myapp-rg --consumption-plan-location westeurope \ --name my-app-name --storage-account mystorage --runtime java
Sign in to Azure Pipelines and connect to Azure
Sign in to Azure Pipelines. After you sign in, your browser goes to
https://dev.azure.com/my-organization-name and displays your Azure DevOps dashboard.
Within your selected organization, create a project. If you don't have any projects in your organization, you see a Create a project to get started screen. Otherwise, select the Create Project button in the upper-right corner of the dashboard.
Now create the service connection:
From your project dashboard, select Project settings on the bottom left.
On the settings page, select Pipelines > Service connections, select New service connection, and then select Azure Resource Manager.
The Add an Azure Resource Manager service connection* dialog box appears.
- Name Type a name and then copy and paste it into a text file so you can use it later.
- Scope Select Subscription.
- Subscription Select the subscription in which you created the App Service.
- Resource Group Select the resource group you created earlier
- Select Allow all pipelines to use this connection.
If you need to create a connection to an Azure subscription that's owned by someone else, see Create an Azure Resource Manager service connection with an existing service principal.
Create the pipeline
Sign in to your Azure DevOps organization and navigate to your project.
Go to Pipelines, and then select New Pipeline.
Walk through the steps of the wizard by first selecting GitHub as the location of your source code.
You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.
When the list of repositories appears, select your repository.
You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve & install.
When the Configure tab appears, select Maven. Your new pipeline appears.
When prompted, select the Azure subscription in which you created your Web App.
Select the Web App.
Select Validate and configure.
As Azure Pipelines creates an azure-pipelines.yml file, which defines your CI/CD pipeline, it:
- Includes a Build stage, which builds your project, and a Deploy stage, which deploys it to Azure as a Linux web app.
- As part of the Deploy stage, it also creates an Environment with default name same as the Web App. You can choose to modify the environment name.
Take a look at the pipeline to see what it does. Make sure that all the default inputs are appropriate for your code.
After you've looked at what the pipeline does, select Save and run, after which you're prompted for a commit message because Azure Pipelines adds the azure-pipelines.yml file to your repository. After editing the message, select Save and run again to see your pipeline in action.
You just created and ran a pipeline that we automatically created for because your code appeared to be a good match for the Maven Azure Pipelines template.
Edit the pipeline
After the pipeline has run, select the vertical ellipses in the upper-right corner of the window and then select Edit pipeline.
Set some variables for your deployment
# at the top of your YAML file # set some variables that you'll need when you deploy variables: # the name of the service connection that you created above serviceConnectionToAzure: name-of-your-service-connection # the name of your web app here is the same one you used above # when you created the web app using the Azure CLI appName: my-app-name # ...
Deploy to Azure Functions
# ... # add these as the last steps # to deploy to your app service - task: CopyFiles@2 displayName: Copy Files inputs: SourceFolder: $(system.defaultworkingdirectory)/target/azure-functions/ Contents: '**' TargetFolder: $(build.artifactstagingdirectory) - task: PublishBuildArtifacts@1 displayName: Publish Artifact inputs: PathtoPublish: $(build.artifactstagingdirectory) - task: AzureFunctionApp@1 displayName: Azure Function App deploy inputs: azureSubscription: $(serviceConnectionToAzure) appType: functionApp appName: $(appName) package: $(build.artifactstagingdirectory)
Run the pipeline and check out your site
You're now ready to save your changes and try it out!
Select Save in the upper-right corner of the editor.
In the dialog box that appears, add a Commit message such as
add deployment to our pipeline, and then select Save.
In the pipeline editor, select Run.
When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action.
After the pipeline has run, test the function app running on Azure. For example, in bash or from a command prompt enter:
curl -w '\n' https://my-app-name-00000000000000000.azurewebsites.net/api/HttpTrigger-Java -d fromYourPipeline
Your function then returns:
Clean up resources
Whenever you're done with the resources you created above, you can use the following command to delete them:
az group delete --name myapp-rg
y when prompted.