Build and deploy Java to Azure Functions

Azure Pipelines

You can use Azure Functions to run small pieces of code in the cloud with 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.

Prerequisites

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

    Tip

    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 try creating a pipeline for that code.

However, if you are a new user, then you might get a better start by using our sample code. In that case, fork this repo in GitHub:

https://github.com/MicrosoftDocs/pipelines-java-function

Create an Azure Functions app

Sign in to the Azure Portal, and then select the Cloud Shell button in the upper-right corner.

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:

  1. From your project dashboard, select Project settings on the bottom left.

  2. On the settings page, select Pipelines > Service connections, select New service connection, and then select Azure Resource Manager.

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

    New service connection dialog box

Tip

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

  1. Sign in to your Azure DevOps organization and navigate to your project.

  2. Go to Pipelines, and then select New Pipeline.

  3. Walk through the steps of the wizard by first selecting GitHub as the location of your source code.

    Select GitHub

    Note

    If this is not what you see, then make sure the Multi-stage pipelines experience is turned on.

  4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. When the list of repositories appears, select your repository.

  6. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve and install.

When the Configure tab appears, select Maven. Your new pipeline appears.

  1. When prompted, select the Azure subscription in which you created your Web App.

  2. Select the Web App.

  3. 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.
  4. Take a look at the pipeline to see what it does. Make sure that all the default inputs are appropriate for your code.

  5. 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)/javafunctions

Run the pipeline and check out your site

You're now ready to save your changes and try it out!

  1. Select Save in the upper-right corner of the editor.

  2. In the dialog box that appears, add a Commit message such as add deployment to our pipeline, and then select Save.

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

Hello PipelineCreator

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

Type y when prompted.