• Products
  • Get started
  • Documentation
  • Resources

Use deployment gating with Jenkins

Use deployment gating to create a change request with an approval gate at any stage in your Jenkins pipeline. Control if (and when) the pipeline should continue using statuses in the change request Jira workflow. Stakeholders can allow or prevent the deployment from Jira Service Management.

To use deployment gating with Jenkins:

  1. Go to your Jenkinsfile.

  2. Find the stage in the pipeline where you would like to raise a change request. For example, before the ‘Production’ deployment stage.

    • We recommend adding 2 stages to your pipeline for deployment gating: One stage to raise a change request, and another stage to check the status of the change request.

  3. Add the following snippet to your pipeline, replacing your Jira site name, environment ID, environment type and service ID(s) you copied from your Jira Service Management project.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 stage('Request approval') { // Raise change request steps { echo 'Raise change request...' jiraSendDeploymentInfo(site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1', environmentName:'us-prod-1', environmentType:'production', state:"pending", enableGating:true, serviceIds: [ '<YOUR-SERVICE-ID>' ] ) } } stage("Approval gate") { // Check request status steps { retry(20) { // Poll every 30s for 10min waitUntil { sleep 30 checkGatingStatus( site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1' ) } } } }

Snippet example: Raise gated change request

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 stage('Request approval') { // Raise change request steps { echo 'Raise change request...' jiraSendDeploymentInfo(site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1', environmentName:'us-prod-1', environmentType:'production', state:"pending", // Deployment has not started yet enableGating:true, // Notify Jira the pipeline is gated serviceIds: [ '<YOUR-SERVICE-ID>' ] ) } }

Snippet example: Manually check change request status

1 2 3 4 5 6 7 8 9 10 11 12 stage("Approval gate") { steps { waitUntil { input message: "Check for approval?" // Manually trigger check status checkGatingStatus( site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1' ) } } } }

Snippet example: Automatically check change request status after a delay

1 2 3 4 5 6 7 8 9 10 11 12 stage("Approval gate") { steps { waitUntil { sleep 30 // check status after 30s checkGatingStatus( site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1' ) } } } }

Snippet example: Automatically check change request status (poll)

1 2 3 4 5 6 7 8 9 10 11 12 13 stage("Approval gate") { steps { retry(20) { // Retry every 30s for 10min waitUntil { sleep 30 checkGatingStatus( site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1' ) } } } }

Full Jenkinsfile example

Raise a change request and wait for approval before deploying to production. When the approval is complete, restart the pipeline automatically.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 pipeline { agent any stages { stage("Test") { steps { echo "Deploying to test" } } stage("Stage") { steps { echo "Deploying to staging" } } stage('Request approval') { // Raise change request steps { echo 'Raise change request...' jiraSendDeploymentInfo(site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1', environmentName:'us-prod-1', environmentType:'production', state:"pending", enableGating:true, serviceIds: [ '<YOUR-SERVICE-ID>' ] ) } } stage("Approval gate") { // Check change request status steps { retry(20) { // Poll every 30s for 10min waitUntil { sleep 30 checkGatingStatus( site:'<YOUR-SITE>.atlassian.net', environmentId:'us-prod-1' ) } } } } stage("Production") { steps { echo "Deploying to production!!" } post { always { sh 'sleep 2' } // Notify Jira based on deployment step result success { jiraSendDeploymentInfo ( site: '<YOUR-SITE>.atlassian.net', environmentId: 'us-prod-1', environmentName: 'us-prod-1', environmentType: 'production', state: 'successful', serviceIds: [ '<YOUR-SERVICE-ID>' ] ) } } } } }

 

Last modified on Nov 9, 2020
Cached at 8:30 AM on Nov 25, 2020 |

Additional Help

Ask the Community