How to integrate Jenkins with Jira?

Nikita Sevostyanov
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 29, 2021

Hello! I wonder how I can integrate Jenkins and Jita ( Deployment info). 
I successfully did everything from this tutorial: https://support.atlassian.com/jira-cloud-administration/docs/integrate-with-jenkins/
But I can't find the following parameters: environmentIdenvironmentName, and environmentType. Where I can find them?

Thanks in advance!

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 31, 2021

Hi @Nikita Sevostyanov 

While I'm not an expert in this particular integration, I believe I found the answer here.  Check out the Readme.md in https://github.com/jenkinsci/atlassian-jira-software-cloud-plugin it indicates that

we use a post step to send deployment data to Jira and the relevant issues. Here, the environmentIdenvironmentName, and environmentType need to be set to whatever you want to appear in Jira.

So those values are not expected to be found in Jira, but rather these are something that you supply about the Jenkins environment that you want to appear within Jira. 

I found some more info on these fields over in https://www.jenkins.io/doc/pipeline/steps/atlassian-jira-software-cloud/ which indicates these are optional fields to begin with.

I hope this helps.

Andy 

Pranav Lonsane August 28, 2023

Hi @Nikita Sevostyanov @Andy Heinzer 
I have done the implementation using below apis to send deployment info on jira using post request. But facing issues while assigning env variable for environmentId parameter in post request. Getting this error.

"acceptedDeployments":  [

                            ],
    "rejectedDeployments":  [
                                {
                                    "key":  {
                                                "deploymentSequenceNumber":  35
                                            },
                                    "errors":  [
                                                   {
                                                       "message":  "Invalid JSON: \u0027environment\u0027 is invalid. Must be of type EnvironmentDetails."
                                                   }
                                               ]
                                }
                            ]



https://developer.atlassian.com/cloud/jira/software/rest/api-group-deployments/#api-rest-deployments-0-1-bulk-post

Thanks in advance, any help on this would be appreciable.

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

@Pranav Lonsane @Nikita Sevostyanov 

The API simply needs an environment that you can specific within your request to the API.

I left out the other bits but your request needs to include the following:

{
"deployments": [
{
...
"environment": {
"id": "unique-env-id",
"displayName": "env-name",
"type": "env-type"
},
...
}
]
}

Simple replace `unique-env-id` with a unique Id of the environment. In my app I use this line to generate an Id based on the name of the environment 

UUID.nameUUIDFromBytes(environmentName.getBytes(StandardCharsets.UTF_8)).toString()

Replace `env-name` with a human readable name of the environment.

And finally replace `env-type` with one of the following possible values unmappeddevelopmenttestingstagingproduction

 Cheers,
Mark
Like Pranav Lonsane likes this
Pranav Lonsane August 29, 2023

Thanks for your inputs @Mark Rekveld - Marvelution 
But I am using powershell script below and unfamilier with 

UUID.nameUUIDFromBytes(environmentName.getBytes(StandardCharsets.UTF_8)).toString()

 this.
can you please explain in detail how and where should I run above line so I can get environment parameters in that format.

Here is my powershell script.

# Set the SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$client_id = "my-client-id"
$client_secret = "my-client-secret"

$body = @{
audience = "api.atlassian.com"
grant_type = "client_credentials"
client_id = $client_id
client_secret = $client_secret
}

$response = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/json" -Body ($body | ConvertTo-Json)

$access_token = $response.access_token
$expires_in = $response.expires_in
$token_type = $response.token_type

# Set the SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# API endpoint

# Headers
$headers = @{
"Accept" = "application/json"
"Content-Type" = "application/json"
"Authorization" = "Bearer $access_token"
"Cookie" = "atlassian.xsrf.token=f636d78bb3157739b8d67f168e23fac87a2ec469_lin"
}

$objForRemote = @{
BUILD_NUMBER = $env:BUILD_NUMBER
EXECUTOR_NUMBER = $env:EXECUTOR_NUMBER
JOB_NAME = $env:JOB_NAME
BUILD_URL = $env:BUILD_URL
BUILD_STATUS = $env:BUILD_STATUS
BUILD_TIMESTAMP = $env:BUILD_TIMESTAMP
BUILD_ID = $env:BUILD_ID
BUILD_TargetEnvironmentType = $env:BUILD_TargetEnvironmentType
BUILD_TargetEnvironment = $env:BUILD_TargetEnvironment
}

$data = @{
deployments = @(
@{
deploymentSequenceNumber = $env:BUILD_NUMBER
updateSequenceNumber = $env:EXECUTOR_NUMBER
associations = @(
@{
associationType = 'issueIdOrKeys'
values = @('KAN-6')
}
)
displayName = $env:JOB_NAME
url = $env:BUILD_URL
description = $env:BUILD_STATUS
lastUpdated = $env:BUILD_TIMESTAMP
state = 'successful'
pipeline = @{
id = $env:BUILD_ID
displayName = $env:JOB_NAME
url = $env:BUILD_URL
}
environment = @{
id = $env:BUILD_ID
displayName = $env:BUILD_TargetEnvironment
type = $env:BUILD_TargetEnvironmentType
}
}
)
}

$jsonData = $data | ConvertTo-Json

$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -Body $jsonData

$response | ConvertTo-Json -Depth 9

# Check if the response contains an "errors" field
if ($response -is [System.Array]) {
Write-Host "Errors found:"
$response.errors
} else {
$acceptedDeployments = $response.acceptedDeployments
$rejectedDeployments = $response.rejectedDeployments
Write-Host "Accepted Deployments:"
$acceptedDeployments
Write-Host "Rejected Deployments:"
$rejectedDeployments
}
Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

Hi @Pranav Lonsane 

Sorry, that line is for Java.

The objective of the line to generate an id based on the input. If the input is gives twice without changing it then the generated id should be the same.

You can also use a checksum like MD5.

But if you already have an unique id that is available in the context of the build, then you can use that as well.

I have never worked with powershell so am not sure if it has the capability to generate a unique string based on another string.

If all else fails, you can always opt to manually specify an id. Especially if the environment is only used with one build, then why generate if you can specify.

Cheers,
Mark

Pranav Lonsane August 29, 2023

Thanks @Mark Rekveld - Marvelution Got it

Pranav Lonsane August 29, 2023

Hi @Mark Rekveld - Marvelution 
what do you think about this ?

"errors":  [
                                                   {
                                                       "message":  "Invalid JSON: \u0027associations\u0027 is invalid. Must be of type Array."
                                                   }
                                               ]


 

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

@Pranav Lonsane that is most likely that the environment object in the json is not formatted correctly, or has missing, invalid values.

Can you print the json send to the console output so you can check?

Pranav Lonsane August 29, 2023

yes printed it is giving 

System.Collections.Hashtable

so do I need to convert it into hashtable ? 

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

Can powershell convert the hashtable to valid json?

Pranav Lonsane August 29, 2023

yes

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

Can you share the json your powershell generates before sending it to the API?

Pranav Lonsane August 29, 2023

yes here it is

{
"associationType": "issueIdOrKeys",
"values[]": "KAN-6"
}

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

That is way less that is needed, far more fields are required.

Are you sure this is the complete JSON generated by the powershell?

Pranav Lonsane August 29, 2023

Yes, have provided you json representaion of only associations field.
And these are the minimum parameters have provided.

Mark Rekveld - Marvelution
Atlassian Partner
August 29, 2023

The association field is formatted incorrectly it needs to be as follows:

{
"associationType": "issueIdOrKeys",
"values": ["KAN-6"]
}

If the script is making this 'error' then it may be inserting errors else where to. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events