Back to blog

CI/CD with Jenkins Pipeline and Azure

R. Tyler Croy
R. Tyler Croy
August 10, 2017

This is a guest post by Pui Chee Chen, Product Manager at Microsoft working on Azure DevOps open source integrations.

Recently, we improved the Azure Credential plugin by adding a custom binding for Azure Credentials which allows you to use an Azure service principal (the analog to a service or system account) via the Credentials Binding plugin. This means it’s now trivial to run Azure CLI commands from a Jenkins Pipeline. We also recently published the first version of the Azure App Service plugin which makes it very easy to deploy Azure Web Apps directly from Jenkins Pipeline. While we’ll have much more to discuss in our Jenkins World presentation on Azure DevOps open source integrations, in this blog post I wanted to share some good snippets of what is possible today with Jenkins Pipeline and Azure.

First, a simple example using the Azure CLI to list resources in the subscription:

Jenkinsfile (Scripted Pipeline)
node {
    /* .. snip .. */
    stage('Deploy') {
        withCredentials([azureServicePrincipal('principal-credentials-id')]) {
            sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
            sh 'az account set -s $AZURE_SUBSCRIPTION_ID'
            sh 'az resource list'
        }
    }
}

azureServicePrincipal() cannot be used in Declarative Pipeline until JENKINS-46103 is resolved.

Once a Pipeline can interact with Azure, there are countless ways one could implement continuous delivery with Jenkins and Azure. From a deploying a simple webapp with the Azure App Service plugin and the azureWebAppPublish step, or a more advanced container-based delivery pipeline to deliver new containers to Kubernetes via Azure Container Service.

With the Docker Pipeline plugin and a little bit of extra scripting, a Jenkins Pipeline can also build and publish a Docker container to an Azure Container Registry:

Jenkinsfile (Scripted Pipeline)
import groovy.json.JsonSlurper

node {
    def container
    def acrSettings

    withCredentials([azureServicePrincipal('principal-credentials-id')]) {
        stage('Prepare Environment') {
            sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
            sh 'az account set -s $AZURE_SUBSCRIPTION_ID'
            acrSettings = new JsonSlurper().parseText(
                                            sh(script: "az acs show -o json -n my-acr", returnStdout: true))
        }

        stage('Build') {
            container = docker.build("${acrSettings.loginServer}/my-app:${env.BUILD_ID}")
        }

        stage('Publish') {
            /* https://issues.jenkins.io/browse/JENKINS-46108 */
            sh "docker login -u ${AZURE_CLIENT_ID} -p ${AZURE_CLIENT_SECRET} ${acrSettings.loginServer}"
            container.push()
        }

        stage('Deploy') {
            echo 'Orchestrating a new deployment with kubectl is a simple exercise left to the reader ;)'
        }
    }
}

If you have been following our Azure Blog, you may have noticed we have shipped a lot of updates to provide better support for Azure on Jenkins, and vice versa, such as:

  • Hosted Jenkins. New Solution Template in Azure Marketplace lets you spin up a Jenkins Controller on Azure in minutes. Not only is it easy and fast, the solution template gives you option to scale up by selecting the VM disk type and size. And guess what? You can even select the Jenkins release type you want to use - LTS, weekly build or Azure verified - all under your control.

  • Continuous integration experience. In the latest version of our Azure VM Agents plugin, we improved the user experience and added the option to let you to select Managed Disk for disk type (which is currently used extensively on ci.jenknis.io. You no longer need to worry about exceeding the number of VMs on your subscription.

  • Continuous deployment experience. Now, if Azure CLI is not your cup of tea, we released our first plugin to provide continuous deployment support to Azure App Service. The plugin supports all languages Azure App Service supports. We even have a walkthrough here in the brand new Jenkins Hub where you can find all Jenkins on Azure resources.

  • Pipeline readiness. Also, all Azure plugins are and will be pipeline ready. Have you been leveraging our Azure Storage plugin in your Pipeline?

So, what’s next? We have a big surprise in store at Jenkins World! :)

We are serious about supporting open source and the open source community. Be sure to catch our talk on Azure DevOps open source integrations. See you at Jenkins World 2017!

Join the Azure DevOps team at Jenkins World in August, register with the code JWFOSS for a 30% discount off your pass.

About the author

R. Tyler Croy

R. Tyler Croy

R. Tyler Croy has been part of the Jenkins project for the past seven years. While avoiding contributing any Java code, Tyler is involved in many of the other aspects of the project which keep it running, such as this website, infrastructure, governance, etc.