DEV Community

Pranav Lonsane
Pranav Lonsane

Posted on

Streamlining Jenkins-Jira Integration: Automating Deployment Data Submission

In modern software development practices, the seamless integration of different tools and platforms is crucial for efficient collaboration and automation. Jenkins, a popular continuous integration and continuous delivery (CI/CD) tool, and Jira, a widely used issue tracking and project management software, are two such platforms that are often integrated to streamline the development process. In this blog post, we'll explore how to automate the submission of deployment data from Jenkins to Jira using the Submit Deployment Data API.

Understanding Jenkins-Jira Integration:-

  • Jenkins is a powerful automation server that enables developers to automate various tasks, including building, testing, and deploying software.
  • Jira, on the other hand, is a comprehensive project management tool that allows teams to plan, track, and manage their software development projects.
  • Integrating Jenkins with Jira allows teams to track the progress of builds and deployments directly within Jira issues, providing better visibility and traceability.

The Need for Deployment Data Submission:-

  • In many software development workflows, it's essential to keep track of when and where code changes are deployed.
  • By submitting deployment data to Jira, teams can associate specific deployments with corresponding Jira issues, providing a comprehensive view of the deployment history for each issue.
  • This information can be invaluable for troubleshooting, auditing, and compliance purposes.

Automating Deployment Data Submission:-

  • To automate the submission of deployment data from Jenkins to Jira, we can leverage the Submit Deployment Data API provided by Jira.
  • The API allows us to submit deployment information, such as deployment sequence number, update sequence number, environment details, and more, directly to Jira.
  • We can use scripting to retrieve relevant deployment information from Jenkins, format it according to the API requirements, and submit it to Jira.

Connectivity Steps:-

  • Install the Jenkins for Jira app in Jira:-

Log in to your Jira site and go to Apps > Explore more apps.
Search for the Jenkins for Jira (Official) app and select Get app.
As I have already installed it, I can locate it under Manage apps.

Image description

Start a connection to Jenkins in Jira:-

In Jira, go to Apps > Manage your apps.
In the left sidebar, under Apps, select Jenkins for Jira.
Follow the on-screen prompts in the connection wizard, after giving your server a name, you will be provided with two credential items: a webhook URL and secret token to use in Jenkins.

Install Jira Software Cloud Plugin for Jenkins:-

Log in to your Jenkins server and go to Manage Jenkins > Plugins > Available plugins.
Search for Atlassian Jira Software Cloud and install the plugin.
As I have already installed it, I can locate it under Installed plugins.

Image description

Complete the connection on your Jenkins server:-

  • In Jenkins, go to Manage Jenkins > Configure System > Jira Software Cloud Integration.
  • Select Add Jira Cloud Site.
  • Enter your Site name
  • Enter your Webhook URL
  • Enter your Secret using the Jenkins Credential Provider.
  • Select Add
  • From the Kind dropdown, select Secret text
  • Paste the secret provided in the Jenkins for Jira connection wizard in the Secret field
  • Give your secret a descriptive name (such as your site name) in the Description field
  • Select Add
  • Use the Secret dropdown to select the secret you just entered.
  • Select Test connection to make sure your credentials are valid for your Jira site.
  • Select Save.
  • If you have successfully entered your site name and credentials, you’ll be returned to Manage Jenkins > Configure System > Jira Software Cloud Integration.
  • Select Save

Image description
At this stage, your Jenkins server is connected to Jira. Now, let's delve into the actual implementation process.

Implementation Steps:-

  • Obtain OAuth Credentials as shown below:

Image description

Image description

  • After filling in the necessary details, you will receive CLIENT_ID and CLIENT_SECRET from Jira. You must use these credentials in an API provided by Jira to generate an API token. This token will be utilized in your script to make the necessary API calls as per your requirements.

  • Detailed information about the authorization flow is provided here as shown below:

Image description

  • When making API calls, ensure that you structure the URL in the following format:

https://api.atlassian.com/jira/<entity type>/0.1/cloud/<cloud ID>/bulk

entity type : either builds, deployments, devinfo or feature flags
cloud ID : is retrieved by calling below url

https://<your-jira-instance>.atlassian.net/_edge/tenant_info

  • Till this point, we have completed the necessary steps to access the APIs provided by JIRA. Now, it's up to your specific use case to determine how you will write a script to send deployment information from Jenkins to Jira. In my scenario, I have developed a parameterized Jenkins job containing a bash script. This script will be triggered upon successful completion of the deployment job and will subsequently update the JIRA board.

Here's the script for your reference. Before executing this script, ensure you include your own CLOUD-ID as mentioned earlier.

#!/bin/bash

# Define variables
issueIds="$issueId"
tokenUrl="https://api.atlassian.com/oauth/token"
client_id="$client_id"
client_secret="$client_secret"

# Obtain OAuth2 access token
response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"audience": "api.atlassian.com", "grant_type": "client_credentials", "client_id": "'"$client_id"'", "client_secret": "'"$client_secret"'"}' "$tokenUrl")
access_token=$(echo "$response" | jq -r '.access_token')

# Get current time in required format
converted_time=$(date -u +"%FT%T.%3NZ")

# Define function to perform deployment actions
perform_deployment_actions() {
    local issueId="$1"
    local integer_part="${issueId//[^0-9]/}"
    local geturl="https://api.atlassian.com/jira/deployments/0.1/cloud/CLOUD-ID/pipelines/$issueId/environments/$BUILD_TargetEnvironmentID/deployments/$integer_part"
    local deleteurl="https://api.atlassian.com/jira/deployments/0.1/cloud/CLOUD-ID/pipelines/$issueId/environments/$BUILD_TargetEnvironmentID/deployments/$integer_part"
    local deployment_payload='{
    "deployments": [
      {
        "deploymentSequenceNumber": "'"$integer_part"'",
        "updateSequenceNumber": "'"$EXECUTOR_NUMBER"'",
        "associations": [
          {
            "associationType": "issueIdOrKeys",
            "values": ["'"$issueId"'"]
          }
        ],
        "displayName": "'"$JOB_NAME"'",
        "url": "'"$BUILD_URL"'",
        "description": "'"$BUILD_TAG"'",
        "lastUpdated": "'"$converted_time"'",
        "state": "successful",
        "pipeline": {
          "id": "'"$issueId"'",
          "displayName": "'"$JOB_NAME"'",
          "url": "'"$BUILD_URL"'"
        },
        "environment": {
          "id": "'"$BUILD_TargetEnvironmentID"'",
          "displayName": "'"$BUILD_TargetEnvironment"'",
          "type": "'"$BUILD_TargetEnvironmentType"'"
        }
      }
    ]
  }'
    local posturl="https://api.atlassian.com/jira/deployments/0.1/cloud/CLOUD-ID/bulk"

    # Retrieve existing deployment information
    get_response=$(curl -s -X GET -H "Accept: application/json" -H "Authorization: Bearer $access_token" "$geturl")
    echo "get_response: $get_response"

    # Delete existing deployment
    delete_response=$(curl -s -X DELETE -H "Authorization: Bearer $access_token" "$deleteurl")
    echo "delete_response: $delete_response"

    # Submit new deployment data
    post_res=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $access_token" -d "$deployment_payload" "$posturl")
    echo "post_response: $post_res"
}

# Loop through issueIds
while IFS= read -r line; do
  echo "issueIdArray: $line"
  perform_deployment_actions "$line"
done <<< "$issueIds"

Enter fullscreen mode Exit fullscreen mode

So, with this approach, you can simultaneously update multiple Jira issues on a Jira board with one specific environment, as shown below.

Image description
Image description

Image description

Upon clicking the pipeline URL from the Jira board, you can determine which Jenkins job triggered the deployment, as demonstrated below.

For the purpose of this demonstration, I have manually started the Jenkins job. However, as mentioned earlier, you can configure the trigger accordingly.

Image description

Conclusion:
Automating the submission of deployment data from Jenkins to Jira streamlines the development workflow, enhances traceability, and facilitates collaboration among team members. By leveraging the Submit Deployment Data API provided by Jira, teams can seamlessly integrate deployment information into their issue tracking and project management processes, leading to greater efficiency and productivity in software development projects.

References:
https://support.atlassian.com/jira-cloud-administration/docs/integrate-with-jenkins/
https://developer.atlassian.com/cloud/jira/software/rest/api-group-deployments/#api-group-deployments
https://developer.atlassian.com/cloud/jira/software/integrate-jsw-cloud-with-onpremises-tools/

Top comments (0)