Set up continuous integration and deployment to Azure App Service with Jenkins

This tutorial sets up continuous integration and deployment (CI/CD) of a sample Java web app developed with the Spring Boot framework to Azure App Service Web App on Linux using Jenkins.

You will perform the following tasks in this tutorial:

  • Install the Jenkins plug-ins needed to deploy to Azure App Service.
  • Define a Jenkins job to build Docker images from a GitHub repo when a new commit is pushed.
  • Define a new Azure Web App for Linux and configure it to deploy Docker images pushed to Azure Container registry.
  • Configure the Azure App Service Jenkins plug-in.
  • Deploy the sample app to Azure App Service with a manual build.
  • Trigger a Jenkins build and update the web app by pushing changes to GitHub.

Before you begin

To complete this tutorial, you need:

If you don't have an Azure subscription, create a free account before you begin.

Install Jenkins plug-ins

  1. Open a web browser to your Jenkins web console and select Manage Jenkins from the left-hand menu, then select Manage Plugins.
  2. Select the Available tab.
  3. Search for and select the checkbox next to the following plug-ins:

  4. Select Download now and install after restart to enable the plugins in your Jenkins configuration.

Configure GitHub and Jenkins

Set up Jenkins to receive GitHub webhooks when new commits are pushed to a repo in your account.

  1. Select Manage Jenkins, then Configure System. In the GitHub section, make sure Manage hooks is selected and then select Manage additional GitHub actions and choose Convert login and password to token.
  2. Select the From login and password radio button and enter your GitHub username and password. Select Create token credentials to create a new GitHub Personal Access Token.
    Create GitHub PAT from login and password
  3. Select the newly created token from the Credentials drop down in the GitHub Servers configuration. Select Test connection to verify that the authentication is working.
    Verify connection to GitHub once PAT is configured

Note

If your GitHub account has two-factor authentication enabled, create the token on GitHub and configure Jenkins to use it. Review the Jenkins GitHub plug-in documentation for full details.

Fork the sample repo and create a Jenkins job

  1. Open the Spring Boot sample application repo and fork it to your own GitHub account by selecting Fork in the top right-hand corner.
    Fork from GitHub
  2. In the Jenkins web console, select New Item, give it a name MyJavaApp, select Freestyle project, then select OK.
    New Jenkins Freestyle Project
  3. Under the General section, select GitHub project and enter your forked repo URL such as https://github.com/raisa/gs-spring-boot-docker
  4. Under the Source code management section, select Git, enter your forked repo .git URL such as https://github.com/raisa/gs-spring-boot-docker.git
  5. Under the Build Triggers section, select GitHub hook trigger for GITscm polling.
  6. Under the Build section, select Add build step and choose Invoke top-level Maven targets. Enter package in the Goals field.
  7. Select Save. You can test your job by selecting Build Now from the project page.

Configure Azure App Service

  1. Using the Azure CLI or Cloud Shell, create a new Web App on Linux. The web app name in this tutorial is myJavaApp, but you need to use a unique name for your own app.

    az group create --name myResourceGroupJenkins --location westus
    az appservice plan create --is-linux --name myLinuxAppServicePlan --resource-group myResourceGroupJenkins 
    az webapp create --name myJavaApp --resource-group myResourceGroupJenkins --plan myLinuxAppServicePlan --runtime "java|1.8|Tomcat|8.5"
    
  2. Create an Azure Container Registry to store the Docker images built by Jenkins. The container registry name used in this tutorial is jenkinsregistry, but you need to use a unique name for your own container registry.

    az acr create --name jenkinsregistry --resource-group myResourceGroupJenkins --sku Basic --admin-enabled
    
  3. Configure the web app to run Docker images pushed to the container registry and specify that the app running in the container listens for requests on port 8080.

    az webapp config container set -c jenkinsregistry/webapp --resource-group myResourceGroupJenkins --name myJavaApp
    az webapp config appsettings set --resource-group myResourceGroupJenkins --name myJavaApp --settings PORT=8080
    

Configure the Azure App Service Jenkins plug-in

  1. In the Jenkins web console, select the MyJavaApp job you created and then select Configure on the left hand of the page.
  2. Scroll down to Post-build Actions, then select Add post-build action and choose Publish an Azure Web App.
  3. Under Azure Profile Configuration, select Add next to Azure Credentials and choose Jenkins.
  4. In the Add Credentials dialog, select Microsoft Azure Service Principal from the Kind drop-down.
  5. Create an Active Directory Service principal from the Azure CLI or Cloud Shell.

    az ad sp create-for-rbac --name jenkins_sp --password secure_password
    
    {
        "appId": "BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBB",
        "displayName": "jenkins_sp",
        "name": "http://jenkins_sp",
        "password": "secure_password",
        "tenant": "CCCCCCCC-CCCC-CCCC-CCCCCCCCCCC"
    }
    
  6. Enter the credentials from the service principal into the Add credentials dialog. If you don't know your Azure subscription ID, you can query it from the CLI:

    az account list
    
       {
           "cloudName": "AzureCloud",
           "id": "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA",
           "isDefault": true,
           "name": "Visual Studio Enterprise",
           "state": "Enabled",
           "tenantId": "CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCC",
           "user": {
           "name": "raisa@fabrikam.com",
           "type": "user"
           }
    

    Configure Azure Service Principal

  7. Verify the service principal authenticates with Azure by selecting Verify Service Principal.
  8. Select Add to save the credentials.
  9. Select the service principal credential you just added from the Azure Credentials drop-down when you are back to the Publish an Azure Web App configuration.
  10. In App Configuration, choose your resource group and web app name from the drop-down.
  11. Select the Publish via Docker radio button.
  12. Enter complete/Dockerfile for Dockerfile path.
  13. Enter https://jenkinsregistry.azurecr.io in the Docker registry URL field.
  14. Select Add next to Registry Credentials.
  15. Enter the admin username for the Azure Container Registry you created for the Username.
  16. Enter the password for the Azure Container registry in the Password field. You can get your username and password from the Azure portal or through the following CLI command:

    az acr credential show -n jenkinsregistry
    

    Add your container registry credentials

  17. Select Add to save the credential.
  18. Select the newly created credential from the Registry credentials drop-down in the App Configuration panel for the Publish an Azure Web App. The finished post-build action should look like the following image:
    Post build action configuration for Azure App Service Deploy
  19. Select Save to save the job configuration.

Deploy the app from GitHub

  1. From the Jenkins project, select Build Now to deploy the sample app to Azure.
  2. Once the build completes, your app is live on Azure at it's publishing URL , for example http://myjavaapp.azurewebsites.net.
    View your deployed app on Azure

Push changes and redeploy

  1. From your Github fork, browse on the web to complete/src/main/java/Hello/Application.java. Select the Edit this file link from the right-hand side of the GitHub interface.
  2. Make the following change to the home() method and commit the change to the repo's master branch.

    return "Hello Docker World on Azure";
    
  3. A new build starts in Jenkins, triggered by the new commit on the master branch of the repo. Once it completes, reload your app on Azure.
    View your deployed app on Azure

Next steps