Tutorial: Scale Jenkins deployments with VM running in Azure

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.

This tutorial shows how to create a Linux virtual machines in Azure and add the VM as a work node to Jenkins.

In this tutorial, you will:

  • Create agent machine
  • Add agent to Jenkins
  • Create a new Jenkins freestyle job
  • Run the job on an Azure VM agent

Prerequisites

Configure agent virtual machine

  1. Use az group create to create an Azure resource group.

    az group create --name <resource_group> --location <location>
    
  2. Use az vm create to create a virtual machine.

    az vm create --resource-group <resource-group> --name <vm_name> --image UbuntuLTS --admin-username azureuser --admin-password "<password>"
    

    Key points:

    • You can also upload your ssh key with the following command --ssh-key-value <ssh_path>.
  3. Install the JDK.

    1. Log in to the virtual machine using an SSH tool.

      ssh username@123.123.123.123
      
    2. Install the JDK with apt. You can also install with other package manager tools such as yum or pacman.

      sudo apt-get install -y default-jdk
      
    3. After installation is complete, run java -version to verify the Java environment. The output will include the version numbers associated with various parts of the JDK.

Configure Jenkins URL

If you use JNLP, you'll need to configure the Jenkins URL.

  1. From the menu, select Manage Jenkins.

  2. Under System Configuration, select Configure System.

  3. Verify that the Jenkins URL is set to the HTTP address of your Jenkins installation - http://<your_host>.<your_domain>:8080/.

  4. Select Save.

Add agent to Jenkins

  1. From the menu, select Manage Jenkins.

  2. Under System Configuration, select Manage Nodes and Clouds.

  3. From the menu, select New Node.

  4. Enter a value for Node Name.

  5. Select Permanent Agent.

  6. Select OK.

  7. Specify values for the following fields:

    • Name: Specify a unique name that identifies an agent within the new Jenkins installation. This value can be different from the agent hostname. However, it's convenient to make them the two values the same. The name value is allowed any special character from the following list: ?*/\%!@#$^&|<>[]:;.

    • Remote root directory: An agent needs to have a directory dedicated to Jenkins. Specify the path to this directory on the agent. It is best to use an absolute path, such as /home/azureuser/work or c:\jenkins. This should be a path local to the agent machine. There is no need for this path to be visible from the master. If you use a relative path, such as ./jenkins-agent, the path will be relative to the working directory provided by the Launch method.

    • Labels: Labels are used to group semantically related agents into one logical group. For example, you could define a label of UBUNTU for all your agents running the Ubuntu distro of Linux.

    • Launch method: There are two options to start the remote Jenkins node: Launch agents via SSH and Launch agent via execution of command on the master:

      • Launch agents via SSH: Specify the values for the following fields:

        • Host: VM public IP address or domain name. For example, 123.123.123.123 or example.com

        • Credentials: Select a credential to be used for logging in to the remote host. You can also select the Add button to define a new credential and then select that new credential once it's create.

        • Host Key Verification Strategy: Controls how Jenkins verifies the SSH key presented by the remote host whilst connecting.

        Node configuration example specifying a launch method of Launch agents via SSH.

      • Launch agent via execution of command on the master:

        • Download the agent.jar from https://<your_jenkins_host_name>/jnlpJars/agent.jar. For example, https://localhost:8443/jnlpJars/agent.jar.

        • Upload agent.jar to your virtual machine

        • Start Jenkins with command ssh <node_host> java -jar <remote_agentjar_path>. For example, ssh azureuser@99.99.999.9 java -jar /home/azureuser/agent.jar.

        Node configuration example specifying a launch method of Launch agent via execution of command on the master.

  8. Select Save.

After you define the configurations, Jenkins adds the virtual machine as a new work node.

Example of virtual machine as new work node

Create a job in Jenkins

  1. From the menu, select New Item.

  2. Enter demoproject1 for the name.

  3. Select Freestyle project.

  4. Select OK.

  5. In the General tab, choose Restrict where project can be run and type ubuntu in Label Expression. You see a message confirming that the label is served by the cloud configuration created in the previous step.

    Setting up a new Jenkins job

  6. In the Source Code Management tab, select Git and add the following URL into the Repository URL field: https://github.com/spring-projects/spring-petclinic.git

  7. In the Build tab, select Add build step, then Invoke top-level Maven targets. Enter package in the Goals field.

  8. Select Save.

Build the new job on an Azure VM agent

  1. Select the job you created in the previous step.

  2. Select Build now. A new build is queued, but doesn't start until an agent VM is created in your Azure subscription.

  3. Once the build is complete, go to Console output. You see that the build was performed remotely on an Azure agent.

    Console output

Next steps