Continuously deploy from a Jenkins build

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, run and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

Azure Pipelines supports integration with Jenkins so that you can use Jenkins for Continuous Integration (CI) while gaining several DevOps benefits from an Azure Pipelines release pipeline that deploys to Azure:

  • Reuse your existing investments in Jenkins build jobs
  • Track work items and related code changes
  • Get end-to-end traceability for your CI/CD workflow
  • Consistently deploy to a range of cloud services
  • Enforce quality of builds by gating deployments
  • Define work flows such as manual approval processes and CI triggers
  • Integrate Jenkins with JIRA and Azure Pipelines to show associated issues for each Jenkins job
  • Integrate with other service management tools such as ServiceNow

A typical approach is to use Jenkins to build an app from source code hosted in a Git repository such as GitHub and then deploy it to Azure using Azure Pipelines.

Schematic of deployment from GitHub and Jenkins to Azure

Before you begin

  • You'll need the source code for your app hosted in a repository such as GitHub, Azure Repos, GitHub Enterprise Server, Bitbucket Cloud, or any another source control provider that Jenkins can interact with.
  • You'll need a Jenkins server where you run your CI builds. You can quickly set up a Jenkins server on Azure.
  • You'll need a Jenkins project that builds you app. For example, you can build a Java app with Maven on Jenkins.

Create a Jenkins service connection from the Service connections section of the project settings page.

In TFS, open the Services page from the "settings" icon in the top menu bar.

For more information, see Jenkins service connection. If you are not familiar with the general concepts in this section, see Accessing your project settings and Creating and using a service connection.

Add a Jenkins artifact

Create a new release pipeline and add a Jenkins artifact to it. After you select the Jenkins service connection, you can select an existing Jenkins job to deploy.

It's possible to store the output from a Jenkins build in Azure blob storage. If you have configured this in your Jenkins project, choose Download artifacts from Azure storage and select the default version and source alias.

For more information, see Jenkins artifacts. If you are not familiar with the general concepts in this section, see Creating a release pipeline and Release artifacts and artifact sources.

Define the deployment steps

Add the tasks you require to deploy your app to your chosen target in the Agent job section in the Tasks page of your release pipeline. For example, add the Azure App Service Deploy task to deploy a web app.

Add the Azure App Service Deploy task YAML code to a job in the .yml file at the root of the repository.


...
jobs:
- job: DeployMyApp
  pool:
    name: Default
  steps:
    - task: AzureRmWebAppDeployment@4
      inputs:
        connectionType: 'AzureRM'
        azureSubscription: your-subscription-name
        appType: webAppLinux
        webAppName: 'MyApp'
        deployToSlotOrASE: false
        packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'
        takeAppOfflineFlag: true
...

YAML builds aren't yet available on TFS.

Whenever you trigger your Azure release pipeline, the artifacts published by the Jenkins CI job are downloaded and made available for your deployment. You get full traceability of your workflow, including the commits associated with each job.

See more details of the Azure App Service Deploy task If you are not familiar with the general concepts in this section, see Build and release jobs and Using tasks in builds and releases.

Enable continuous deployment

If your Jenkins server is hosted in Azure, or your Azure DevOps organization has direct visibility to your Jenkins server, you can easily enable a continuous deployment (CD) trigger within your release pipeline that causes a release to be created and a deployment started every time the source artifact is updated.

To enable continuous deployment for an Azure hosted or directly visible Jenkins server:

  1. Open the continuous deployment trigger pane from the Pipelines page of your release pipeline.

  2. Change the setting to Enabled.

  3. Choose Add and select the branch you want to create the trigger for. Or select the default branch.

However, if you have an on-premises Jenkins server, or your Azure DevOps organization does not have direct visibility to your Jenkins Server, you can trigger a release for an Azure pipeline from a Jenkins project using the following steps:

  1. Create a Personal Access Token (PAT) in your Azure DevOps or TFS organization. Jenkins requires this information to access your organization. Ensure you keep a copy of the token information for upcoming steps in this section.

  2. Install the Team Foundation Server plugin on your Jenkins server.

  3. Within your Jenkins project, you will find a new post build action named Trigger release in TFS/Team Services. Add this action to your project.

  4. Enter the collection URL for your Azure DevOps organization or TFS server as https://<accountname>.visualstudio.com/DefaultCollection/

  5. Leave username empty and enter your PAT as the password.

  6. Select the Azure DevOps project and the release definition to trigger.

Now a new CD release will be triggered every time your Jenkins CI job is completed.

See also