Quickstart: Configure Jenkins using Azure CLI

This quickstart shows how to install Jenkins on an Ubuntu Linux VM with the tools and plug-ins configured to work with Azure.

In this quickstart, you'll complete these tasks:

  • Create a setup file that downloads and installs Jenkins
  • Create a resource group
  • Create a virtual machine with the setup file
  • Open port 8080 in order to access Jenkins on the virtual machine
  • Connect to the virtual machine via SSH
  • Configure a sample Jenkins job based on a sample Java app in GitHub
  • Build the sample Jenkins job

Prerequisites

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

Troubleshooting

If you encounter any problems configuring Jenkins, refer to the Cloudbees Jenkins installation page for the latest instructions and known issues.

Create a virtual machine

  1. Sign in to the Azure portal.

  2. Open Azure Cloud Shell and - if not done already - switch to Bash.

  3. Create a file named cloud-init-jenkins.txt.

    code cloud-init-jenkins.txt
    
  4. Paste the following code into the new file:

    #cloud-config
    package_upgrade: true
    runcmd:
      - apt install openjdk-8-jdk -y
      - wget -qO - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
      - sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
      - apt-get update && apt-get install jenkins -y
      - service jenkins restart
    
  5. Save the file (<Ctrl>S) and exit the editor (<Ctrl>Q).

  6. Create a resource group using az group create. You might need to replace the --location parameter with the appropriate value for your environment.

    az group create \
    --name QuickstartJenkins-rg \
    --location eastus
    
  7. Create a virtual machine using az vm create.

    az vm create \
    --resource-group QuickstartJenkins-rg \
    --name QuickstartJenkins-vm \
    --image UbuntuLTS \
    --admin-username "azureuser" \
    --generate-ssh-keys \
    --custom-data cloud-init-jenkins.txt
    
  8. Verify the creation (and state) of the new virtual machine using az vm list.

    az vm list -d -o table --query "[?name=='QuickstartJenkins-vm']"
    
  9. By default, Jenkins runs on port 8080. Therefore, open port 8080 on the new virtual machine using az vm open.

    az vm open-port \
    --resource-group QuickstartJenkins-rg \
    --name QuickstartJenkins-vm  \
    --port 8080 --priority 1010
    

Configure Jenkins

  1. Get the public IP address for the sample virtual machine using az vm show.

    az vm show \
    --resource-group QuickstartJenkins-rg \
    --name QuickstartJenkins-vm -d \
    --query [publicIps] \
    --output tsv
    

    Jenkins:

    • The --query parameter limits the output to the public IP addresses for the virtual machine.
  2. Using the IP address retrieved in the previous step, SSH into the virtual machine. You'll need to confirm the connection request.

    ssh azureuser@<ip_address>
    

    Jenkins:

    • Upon successful connection, the Cloud Shell prompt includes the user name and virtual machine name: azureuser@QuickstartJenkins-vm.
  3. Verify that Jenkins is running by getting the status of the Jenkins service.

    service jenkins status
    
  4. Get the autogenerated Jenkins password.

    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  5. Using the IP address, open the following URL in a browser: http://<ip_address>:8080

  6. Enter the password you retrieved earlier and select Continue.

    Initial page to unlock Jenkins

  7. Select Select plug-in to install.

    Select the option to install selected plug-ins

  8. In the filter box at the top of the page, enter github. Select the GitHub plug-in and select Install.

    Install the GitHub plug-ins

  9. Enter the information for the first admin user and select Save and Continue.

    Enter information for first admin user

  10. On the Instance Configuration page, select Save and Finish.

    Confirmation page for instance configuration

  11. Select Start using Jenkins.

    Jenkins is ready!

Create your first job

  1. On the Jenkins home page, select Create a job.

    Jenkins console home page

  2. Enter a job name of mySampleApp, select Freestyle project, and select OK.

    New job creation

  3. Select the Source Code Management tab. Enable Git and enter the following URL for the Repository URL value: https://github.com/spring-guides/gs-spring-boot.git. Then change the Branch Specifier to */main.

    Define the Git repo

  4. Select the Build tab, then select Add build step

    Add a new build step

  5. From the drop-down menu, select Invoke Gradle script.

    Select the Gradle script option

  6. Select Use Gradle Wrapper, then enter complete in Wrapper location and build for Tasks.

    Gradle script options

  7. Select Advanced and enter complete in the Root Build script field.

    Advanced Gradle script options

  8. Scroll to the bottom of the page, and select Save.

Build the sample Java app

  1. When the home page for your project displays, select Build Now to compile the code and package the sample app.

    Project home page

  2. A graphic below the Build History heading indicates that the job is being built.

    Job-build in progress

  3. When the build completes, select the Workspace link.

    Select the workspace link

  4. Navigate to complete/build/libs to see that the .jar file was successfully built.

    The target library verifies that the build succeeded

  5. Your Jenkins server is now ready to build your own projects in Azure!

Next steps