Muokkaa

Jaa


Get Started: Install Jenkins on an Azure Linux VM

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

In this article, you'll learn how to:

  • 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

1. Configure your environment

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

2. Open Cloud Shell

  1. If you already have a Cloud Shell session open, you can skip to the next section.

  2. Browse to the Azure portal

  3. If necessary, log in to your Azure subscription and change the Azure directory.

  4. Open Cloud Shell.

    Open Cloud Shell from the top menu in the Azure portal.

  5. If you haven't previously used Cloud Shell, configure the environment and storage settings.

  6. Select the command-line environment.

    Select the CLI you want to use in Cloud Shell.

3. Create a virtual machine

  1. Create a test directory called jenkins-get-started.

  2. Switch to the test directory.

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

  4. Paste the following code into the new file:

    #cloud-config
    package_upgrade: true
    runcmd:
      - sudo apt install openjdk-11-jre -y
      - curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
      -  echo 'deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/' | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
      - sudo apt-get update && sudo apt-get install jenkins -y
      - sudo service jenkins restart
    
    
    
  5. Run az group create to create a resource group.

    az group create --name jenkins-get-started-rg --location eastus
    
  6. Run az vm create to create a virtual machine.

    az vm create \
    --resource-group jenkins-get-started-rg \
    --name jenkins-get-started-vm \
    --image UbuntuLTS \
    --admin-username "azureuser" \
    --generate-ssh-keys \
    --public-ip-sku Standard \
    --custom-data cloud-init-jenkins.txt
    
  7. Run az vm list to verify the creation (and state) of the new virtual machine.

    az vm list -d -o table --query "[?name=='jenkins-get-started-vm']"
    
  8. As Jenkins runs on port 8080, run az vm open to open port 8080 on the new virtual machine.

    az vm open-port \
    --resource-group jenkins-get-started-rg \
    --name jenkins-get-started-vm  \
    --port 8080 --priority 1010
    

4. Configure Jenkins

  1. Run az vm show to get the public IP address for the sample virtual machine.

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

    Key points:

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

    Key points:

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

    service jenkins status
    

    Key points:

    • If you receive an error regarding the service not existing, you may have to wait a couple of minutes for everything to install and initialize.
  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!

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

6. 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 the build succeeded.

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

Troubleshooting

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

Next steps